Re: [spam?##] Expression-local variables and 'let'

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

> Hi,
> 
> A new feature that is already implemented in the development version is 
> the ability to introduce local variables inside an expression (while 
> that was only possible as a statement). A benefit is that you can give a 
> name to an argument, and be able to refer to it in another argument. 
> Instead of:
> 
>   int x = foo();
>   bar(x, x > 0);
> 
> you can write:
> 
>   bar(int x = foo(), x > 0);
>
> using(PrintStream s = ...) {
>   s.println("...");
> }

My initial reaction was that the scope of x should not go past the comma, and
the scope of s should not go outside of the parentheses. But, I guess this is
what happens in formal parameter declarations anyway. But, also, don't you think
that "int x = foo(), x > 0" looks a lot like it is saying "let x be the result
of foo(), asserting/assuming that x > 0".

Since "var" and "let" are "new" to Nice (compared to Java), why not just use the
ML/OCL/Haskell "let...in" syntax for them? That is, make all 'let' and 'var'
expressions "expression-local". Before we would write:
    int x = foo();
    bar(x, x > 0);
    rab(x, x < 0);
 
    let z = something();
    bar(z, z >= 0);

Using ML syntax it would be:
    var x = foo() in {
        bar(x, x > 0);
        rab(x, x < 0);
    }
 
    let z = something() in
        bar(z, z >= 0);

    let s = new PrintStream in 
        using (s) {
            s.println(...);
        } 

At least this syntax is familiar to functional programmers, instead of a brand
new one. 


Anyway, if you don't like "let" in your original expression-local let syntax,
why not replace it with "final"? "val" has no meaning to anybody that has never
used ML and it doesn't do much to indicate that the variable cannot be
reassigned a value. Then you'd have:
    using (final s = new PrintStream(...)) { ... }

- 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.