KBD

Keith Devens .com

Saturday, November 22, 2008 Flag waving
May you live in interesting times – Ancient chinese proverb
← mod_pythonSeason premiere of 24! →

Daily link icon Tuesday, October 28, 2003

Accessors

Yes! This is exactly why I want to rewrite my CMS in Python.

Really, foo['bar'] and foo.bar and foo.bar() and foo('bar')[1] are essentially the same thing. Javascript unifies some of these, other languages like Ruby and Eiffel unify others. Languages like Python (I'll have to check Ruby) give you enough convenience methods that you can make all of these behave identically.

If I were ever to design a language, I'd try to make all of these syntactic sugar for the same construct. And for good templating, it's important to not have to worry about how your smurfs are implemented.

Footnotes:
[1]: and, the horror, (foo 'bar)

← mod_pythonSeason premiere of 24! →

Comments XML gif

George Schlossnagle wrote:

ick. multiple syntaxes for the same thing are not 'sugar' they are syntactic sours. Talk about asking for widely divergent coding styles and high WTF factor in language design.

Python is generally clean. Allowing operator overloading was a real mistake in my opinion. Same with C++: very high WTF factor.

∴ George Schlossnagle | 28-Oct-2003 2:23pm est | #3165

Keith (http://keithdevens.com/) wrote:

I disagree strongly. As for the WTF factor, people will still generally use the syntaxes they're comfortable with for a certain concept. People generally won't use foo('bar') over foo['bar'] when they want array access, but I think you should be able to. This is actually how BASIC does array access IIRC, since accessing an element of an array is essentially a function of the key to the value.

And really, stuff like this already exists in languages like Javascript... foo['bar'] is the same as foo.bar. And in languages like Perl, Ruby, and Eiffel, you can already call no-argument functions or methods without parentheses, which gives them the appearance of being properties. Rather than this leading to incomprehensible code, people use whatever's most natural in the situation and I think it works out pretty well.

Also, I'm surprised you're against operator overloading. To me, a clean and extensible syntax is key to a comfortable language. Python's operator overloading has allowed Python to have some of the most natural XML APIs, for instance. The alternative is to have long-winded method names like you have in Java, and it makes it impossible to develop structures that behave similarly to built-ins. I think allowing operator overloading leads to a much lower cognitive load as well, since you're able to use built-in syntax for familiar operations, rather than having to make up and remember your own method names for things.

(Languages like Haskell even let you define your own operators -- potentially ones that don't exist in the base language -- and I think that's wonderful. It's obviously something to be used carefully, but if it lets you more comfortably represent your problem domain I'm all for it.)

I've written about this before in the context of Perl.

Keith | 29-Oct-2003 12:52pm est | http://keithdevens.com/ | #3171

Simon Willison (http://simon.incutio.com/) wrote:

I'm in two minds over operator overloading. On the one hand, it leads to incredibly expressive and natural feeling APIs. On the other hand, it has the distinct disadvantage that looking at a piece of syntax no longer gives you a fair idea of what it does. Take the following code for example:

obj.name = 'Simon'

You would expect this to assign the string 'Simon' to the name property of an object. BUT... if the object happens to be an overloaded XML wrapper (like XMLTramp) you'll actually be altering the structure of an in-memory DOM style object. Even more confusing, if it's an SQLObject instance the line will have caused an UPDATE statement to be executed against a database table.

In short scripts where you are the only maintainer this is unlikely to cause a problem. In big applications where multiple people are maintaining the code this could get very confusing - especially if you are using SQLObjects and XMLTramps in the same code snippet.

Operator overloading is very neat, but it can have a serious impact on code understandability if used carelessly.

∴ Simon Willison | 29-Oct-2003 5:44pm est | http://simon.incutio.com/ | #3172

Russ (http://blogs.acceleration.net) wrote:

My Response (since that blog wouldnt let me post it):<br />
Operator overloading (or even better definition ala LISP) is wonderful. The problem expressed above about operator overloading is not a problem with the concept but of the implementation. Any sufficiently powerful language construct can be horribly abused. If you don’t want to use anything that is 'dangerous', then you are left with the option of only using Java/C# so that I can always tell exactly what everything does. Unfortunately I don't find these languages terribly expressive. I would much rather work in a more powerful (dangerous) environment that allows me to express myself in as many ways as possible.

The nice thing about the javascript syntax of obj['item'] is that I can retrieve lists of every item on an object and then loop through all of them looking for all methods or what have you. This is much easier/cleaner than eval('obj.item'). The dot operator is nice when coding directly because its one character instead of four.

After working in C# for a very long time, my biggest complaint with the environment is that it is too safe. It's so safe that I cannot express what I want the program to do without coding around any particular problem and casting 6 or 7 times.

∴ Russ | 14-Oct-2004 4:44pm est | http://blogs.acceleration.net | #5868

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

November 2008
SunMonTueWedThuFriSat
 1
2345678
9101112131415
16171819202122
23242526272829
30 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

Calif. Supreme Court to take up gay marriage ban

I would argue the point is not​definitional.  While the word​marriage is su...

Justin: Nov 20, 4:37pm

Java join function

Meh, don't have null strings in​your string arrays imo, but you're​welcome ...

Keith: Nov 19, 7:51pm

Girls, please don't get breast implants

sorry but another thing i have to​make a comment on about you​men...the men...

happynow: Nov 17, 11:36pm

Books by Vincent Cheung

to all Cheung​fans:

read:

http://www.progin​osko.com/aquascum/cheung.h...

Zamir: Nov 16, 9:07am

Generated in about 0.378s.

(Used 8 db queries)

mobile phone