Re: Expression-local variables and 'let'
[email protected] Sat, 21 Feb 2004 02:39:43 -0600
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
Quoting Daniel Bonniot <[email protected]>: > val balance = currentBalance() in display(balance, warn: balance < 0); > display(val balance = currentBalance(), warn: balance < 0); > > Yes, I think the second one is more readable. The first thing you see is > display, so you know straight away what we are trying to do, while in > the other version it's burried deep down. I find the first one to be more readable than the second. I guess we can just "agree to disagree." But, I will play along with you below. > Let's call balance the current balance. Now, display that balance, and > warn the user if that balance is negative. > > vs > > Display the balance which is the current balance, and warn the user if > that balance is negative. > > The first version is more complex: it starts by posing a binding, and > then goes on with the main part. I think that's akin to first proving a > lemma when what you want is to prove a certain theorem. If the lemma is I found I could understand the first one faster than the second one. Also, according to Flesch-Kincaid readability test as implemented in MS Word, the first one is easier to read (grade 5.5) than the second one (grade 6.6). > With a less serious example: I would say "There is this person who is > the brother in law of the boss of my wife's cousin. Well, i'm going to > play music with him at his place", but I wouldn't say "There is this > person who is my father. Well, i'm going to play music with him at his > place", but "I'm going to play music with my father at his place" (in a > program, you would write "at my father's place", because there is no > implicit binder like it/him/her, unless you program in perl ;-) You are mixing things around. Your first sentence is like: let person = father() in play_music(person, home(person)); Your second sentence is like: play_music(father(), home(father()); It seems the second one should be "I'm going to play music with this person who is my father at that person's place": play_music(val person = father(), home(person)); > >You have to have seperate grammar constructs "single variable declaration" > (for parameters) and "variable declaration" (for the general case). > > > That's a minor annoyance, especially given that their semantics > scope-wise is different. I mean, it makes it harder for someone learning the language to understand the syntax. > >Also, you can't > >say that this is a generalization of the syntax for for(...) because you > can declare multiple variables at once in a for loop: > > > > for (int x = 1, y = 0;;); > > > It's a proper generalization of 'using', still. It also generalizes > 'while' if I'm not mistaken, since you can then also implement it as a > user-defined function. I don't think you can implement 'while' as a user-defined function with the same syntax since 'while' needs to be able to evaluate the expression in the parentheses multiple times. You can do 'if (x) { ... }' (no else) though. > >>If you need to define several variables in a limited scope, you can > >>already use the C-syntax idiom (should I say Algol?): > >> > >>{ > >> let x = ...; > >> let y = ...; > >> ... > >>} // scope ends for x and y > >> > >> > > > >I think everybody hates that syntax, which is why nobody ever uses it. > > > Yes, it's rarely used, but I'm not sure it's because the syntax is ugly. > It can also be because methods should be short anyway, so you probably > write a helper method instead of creating a nested scope. And for what's > left, the extra trouble is probably not worth the extra scoping you get > (if a method is 3-5 lines, then usage is easily spotted). This is an argument against the construct in general, not the "let..in" syntax for it. > >Main Entry : let..in > >Pronunciation: 'let 'in > >Etymology : ML > >Definition : magic solution ;) > > > > > I'm not convinced yet, unfortunately. I know, I hope you saw that this was just a joke. - Brian ------------------------------------------------------- SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with a free DVD software kit from IBM. Click Now! http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click