val vs. let syntax
[email protected] Sat, 21 Feb 2004 20:07:01 -0600
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
I am writing some code and I noticed I wanted to have a top-level private
constant in my package. It seems that "let" is awkard for this:
private let EOS = -1;
Using "val" seems clearer:
private val EOS = -1;
But, still, I think that "val" and "var" look too much alike.
Also, I still don't like the following:
public final class X {
final Y someY = new Y(...);
Y changeableY = new Y(...);
}
private val Y someY = new Y(...);
private Y changeableY = new Y();
private Y f(X x) {
val someY = x.someY;
var changeableY = x.changeableY;
...
return someY;
}
Specifically, I don't like:
1. Sometimes you use "final Y someY" and other times "val Y someY"
2. "final" still means two different things.
3. The function's parameter is assignable, and there is no way
to stop it from being assignable ("final" isn't recognized there).
4. Type inference only works for local variables, not fields and
package-level variables.
5. It seems strange to use the terms "local variables/package variables"
to refer to both "var" and "val" when "var" is short for "variable."
In other words, having "var" as a keyword is implying that variables
declared with "val" are not "variables."
What are you going to call them in the manual?
6. It is not intuitive in which situations "var" can be left implicit.
For #1 and #2, I wonder if "val" and "var" (or whatever is chosen) can be used
for fields too, instead of "final":
public final class X {
val Y someY = new Y(...);
var Y changeableY = new Y(...);
}
private val Y someY = new Y(...);
private var changeableY = new Y(...);
private Y f(X x) {
val Y someY = x.someY;
var changeableY = x.changeableY;
...
return someY;
}
For #3, I don't think that there is a pretty solution, although you could allow
"var X x" and "val X x" as parameter declarations. But, this isn't too helpful
since "var" is the default but the sensible default for parameters is "val."
for #4, I understand that the rationale was that that the type is too important
to be implicit when it helps define the public interface of the package. But,
this isn't the case for private top-level variables. But, this isn't a huge
issue either.
- 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