Re: Expression-local variables and 'let'
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
Quoting Isaac Gouy <[email protected]>: > > You need "let" so that you can have type inference for local > > variables: > > let x = 5; // okay, infer x's type > > y = 5; // error, y is undeclared > > var x = 5; // okay, infer x's type Okay, but I prefer a more functional style so I use "let"/"val" more than var. I rarely use "var," in fact. In fact, I wish that formal parameters were immutable by default so that code like this would not compile: void f(int x) { for (int i = 0; i < x; ++x) { ... } } Where, I had meant "++i" instead of "++x." The rationale being that functions rarely modify their parameters intentionally. I also asked for fields to be final by default: class X { int x; } void main(String[] args) { (new X(x:1).x = 2; // error: X.x is immutable } If you wanted a field to be mutable, you would say: class X { var int x; } These two changes would get rid of the last cases where "final" means "immutable" in the language and promote a more functional style. That idea was shot down a long time ago but maybe it can be revived considering all the proposed syntax changes in the pipeline. - 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