Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!CS.ARIZONA.EDU!kwalker From: kwalker@CS.ARIZONA.EDU ("Kenneth Walker") Newsgroups: comp.lang.icon Subject: RE: Icon Ideas? (operators) Message-ID: <9003271645.AA17945@megaron.cs.arizona.edu> Date: 27 Mar 90 16:45:16 GMT References: Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 50 > Date: Mon, 26 Mar 90 23:15 CST > From: nowlin@iwtqg.att.COM > > You can add two strings of digits in Icon with the "+" operator but you get > a numeric result, not a longer string of digits. You can also concatenate > two numbers into a string of digits with the "||" operator. To allow > overloaded operators would violate this scheme. How would Icon know > whether to do automatic type conversion or try for another version of the > operator that was a better fit to the given operands? Someone with > experience in the implementation could shed more light on this. "+" does give a numeric result, but numeric is either integer or real. The decision about whether to do integer arithmetic or real arithmetic is made at run-time. If you could replace "+" with with your own operation and somehow invoke the old "+" within your operation, you could get the effect of overloading. Assuming the function old_op() gets you the built-in version, the following operation would enhance "+" to do pair wise addition of lists. operator +(a,b) if type(a) == type(b) == "list" then { r := [] every i := 1 to *a do put(r, old_op(a[i], b[i])) return r } else return old_op(a,b) end I don't necessarily think being able to arbitrarily redefine operators is a good idea. It leaves you with too few features in the language whose meaning you can "trust" while reading a program. The idea of being able to add new operators does not have this problem. However, no one has brought up the problems of precedence and associtivity. Does a $- b - c mean (a $- b) - c or a $- (b - c)? You need something in your definition of an operator to deal with this. Currently, the organization of icont does not allow you to add new operators. With the tools we use to make icont, it is possible to organize a translator so that adding new operators can be done, but you must decide on a fixed set of precedences. If you decide "+" is at precedence 12 and "*" is at precedence 13, you will not be able to add operators with intermediate precedences. If you fix them at 12 and 15, you are limited to 2 levels of precedence between them. To get around these problems (which are not particularly serious), you would need a different kind of parser within icont. Ken Walker / Computer Science Dept / Univ of Arizona / Tucson, AZ 85721 +1 602 621 2858 kwalker@cs.arizona.edu {uunet|allegra|noao}!arizona!kwalker