Re: Expression-local variables and 'let'

[email protected]
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Quoting Daniel Bonniot <[email protected]>:

> >In fact, if you look at the examples you will see that the conciceness
> >is questionable:
> >
> >     int x = foo(); bar(x, x > 0);
> >     bar(int x = foo(), x > 0);
> >
> >You saved three characters, one of which is a space. 
> >
> Yes, but you cheated a bit! ;-)
> In the first version x escapes the scope. You could add braces, but that 

Yes, but what about the following?:
    val x = foo() in bar(x, x > 0);
    bar(val x = foo(), x > 0);

You save five characters (again, one is a space). Do you really think that the
second line is more readable? I think the first line is notably more readable
because the declaration/initialization of x is clearly seperated from the usage
of its value, and also it makes the order of execution explicit. For even more
readability, you can break the first line logically into two. You can't do that
with the second one.

> >What does the following mean? I assume it passes two arguments to bar but
> >it could also be misread as declaring two arguments:
> >
> >    bar(int x = 1, y = 0);
> >  
> >
> I would say you assume right. You will get a 'unused variable' for x, 
> and y needs to be declared previously.

I think that is not intuitive, considering that we can do:
     int x = 1, y = 1;
     bar(x);

You have to have seperate grammar constructs "single variable declaration" (for
parameters) and "variable declaration" (for the general case). 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;;);

> >The "let...in" syntax limits scope in the same way that the proposed syntax
> >does. "let...in" lets you limit the scope of multiple variables at once. It
> >seems like the proposed syntax only lets you limit the scope of a single
> >variable at once. 
> >  
> 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. let...in"
is an improvement in both the single-variable and multi-variable cases.

> >>Expression-local variables basically take this special handling, and 
> >>generalize it to a general-purpose construct. This means that the new 
> >>construct is not alien, it's a generalization. This also means you can 

This is not true, see the above comparison of "bar(int x = 1, y = 0);" and 
"for (int x = 1, y = 0;;);"

> >>implement "new syntax" like the 'using' statement purely in the language 
> >>instead of hard-wiring it in the language definition and in the parser. 
> >>This means the language is both simpler (less special cases) and more 
> >>expressive.

"let...in" does this too, in a more readable way.

> My plan was to replace it by 'val', because
>   1) let inside an expression hurts my linguistic feelings (Bryn's too,
>      from private email)

"let...in" lets you keep "let" and prevents this awkward situation in almost
every case.

>   2) we should avoid two keywords for the same concept

I agree that you shouldn't have two keywords for the same concept.

> I note that Isaac is worried about var and val being too similar. That 
> people object to "overloading" the 'final' keyword.

I prefer "let" over "val" since it is easier to distinguish from "var" than
"val" is.

BTW, did you know that "val" and "var" are written and pronounced the same in a
few languages, including Japanese ("l"="r" in Japanese; e.g. "erection" and
"election" are written and pronounced the same). 

> It's hard to please everyone. Who will come up with the magic solution 
> to please them all? ;-)

Main Entry   : let..in
Pronunciation: 'let 'in
Etymology    : ML
Definition   : magic solution ;)

- 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.