Hi,
Do you think a final static field _must_ be declared like
"static final MyClass SOME_RUNTIME_CONSTANT = ...;"
and not
"final static MyClass SOME_RUNTIME_CONSTANT = ...;"
?
It is like that in spec.
My main language is Java and in there, both ways are supported. I've
always used the second one.
When I think about it, first one makes more sense. IMHO being static
is more important than being final when you first read the code (I am
gonna use first way in Java now on :) ).
So, is this case overlooked, or is it a best practice thus it is in
the spec?
It took me some some time to figure out what was wrong in my code with
compiler message :)
BlockType.dart/BlockType.dart:6: Unexpected token
'IDENTIFIER' (expected ';')
final static BlockType THREES = new BlockType.withSize(3);
Am 21.10.2011 00:05 schrieb "Bob Nystrom" :
> It's a little different in Java. In Java, every variable has to have a type declaration. In Dart, you can do:
>
> int foo; // just the type
> var foo; // var instead of type
> final foo; // final instead of type
> final int foo; // final + type
>
> So "final" is (sort of) part of the type declaration in Dart. From that angle, it makes sense that you can't jam a "static" in the middle of it.
>
Hmmm. Put static into account and you have:
static int foo // static + type
So I assume static foo is not possible, but must be static var foo?
> For what it's worth, our convention is to omit the explicit type annotation for final variables:
>
> static final THREES = new BlockType.withSize(3);
>
> Our reasoning is that since it requires an initializer and the variable can't change, the type is obvious from the declaration.
FWIW, I find it annoying that Dart has the javaish static final again. Even in java land has been discussed to find a single keyword to express the donated semantic instead of the more technical notion of a 'static' and 'final' "variable" (psaw!)
When I read that Dart has the keyword 'const' I hoped that this would be exactly this...
So please get rid of those 'static final' noise.
It is :
const PI = 3.14;
KR
Det
This is debatable and may be true much more in Java.
But IIRC in Dart statics can only be initialized with literals, so I assume the main purpose will be to declare constant values.
So a final variable pointing to a mutable object ist not const in the sense of constant. An immutable value (literal) assigned to a final variable which is after all even static, is semantically much more a constant than anything else.
KR
Det
So a final variable pointing to a mutable object ist not const in the sense of constant.
Const objects have a couple of interesting properties and restrictions:1. They must be created from data that can be calculate at compile time: no access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not.2. They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively.3. They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated. In other words:
Thanks for the declaration, Bob.
Just some thoughts:
- If you often combine distinct concepts to express a specific semantic notion, it is of much value to give it a specific name and so go to the next level of abstraction.
For me 'static final' is such a combination: the semantic is: named value. (See PI)
- Using OTOH the const keyword on the right side of an assignment is not to typical, at least for me.
The semantic is: immutable data, i.e. value object.
I all data structure would be deeply immutable, like debated somwhere else, you wouldn't need a const keyword on the value side, but it would be even right on the named-ref side:
Const somename = Point(3,4); // immutable anyway.
So it feels like const is reserved for the wrong part of the expression.
I all data structure would be deeply immutable, like debated somwhere else, you wouldn't need a const keyword on the value side, but it would be even right on the named-ref side:
Thanks for the declaration, Bob.
Just some thoughts:
- If you often combine distinct concepts to express a specific semantic notion, it is of much value to give it a specific name and so go to the next level of abstraction.
For me 'static final' is such a combination: the semantic is: named value. (See PI)
- Using OTOH the const keyword on the right side of an assignment is not to typical, at least for me.
The semantic is: immutable data, i.e. value object.
I all data structure would be deeply immutable, like debated somwhere else, you wouldn't need a const keyword on the value side, but it would be even right on the named-ref side:
Const somename = Point(3,4); // immutable anyway.
So it feels like const is reserved for the wrong part of the expression.
My hunch is that we're worrying about this more than we need to. There are a decent amount of static finals in current Dart code, but I think that's a holdover from its early history from before we had top-level definitions. Now that we do, I'd like most of those static finals to just be top level variables. Why do:class Math {static final PI = 3.1415926535;}And then do Math.PI everywhere, when you can just do:final PI = 3.1415926535;and then just use PI.Not that I'm saying PI will be moved to the top level, but I'd personally like a lot of our statics to become top level variables.
On Fri, Oct 21, 2011 at 2:04 PM, Bob Nystrom <rnys...@google.com> wrote:My hunch is that we're worrying about this more than we need to. There are a decent amount of static finals in current Dart code, but I think that's a holdover from its early history from before we had top-level definitions. Now that we do, I'd like most of those static finals to just be top level variables. Why do:class Math {static final PI = 3.1415926535;}And then do Math.PI everywhere, when you can just do:final PI = 3.1415926535;and then just use PI.Not that I'm saying PI will be moved to the top level, but I'd personally like a lot of our statics to become top level variables.The problem is there is only one top level, and if everything useful goes there so you don't have to disambiguate it, you will wind up having to disambiguate it with a library prefix to avoid collisions and then you have to prefix everything in that library, not just the constants.
It is definitely a tradeoff, but I suspect if this approach were heavily used we would need to add a finer-granularity way of prefixing imports.
I agree with your perspective but I don't think static final really does make that distinction. You can have static finals that are mutable (and we do, in practice) so it's not like they're really just constant named values.