When I write "int x = 7", x is mutable.
Could someone explain why mutable was chosen as the default rather than immutable? I haven't found any record of the discussion behind this particular decision.
I'd personally like to see something like this:var x = 7; // mutablefinal x = 7; // immutableint x = 7; // immutablevar int x = 7; // explicitly mutable
// apply the same rules to method parametersIt seems Java developers are taught to use final everywhere, but tire of typing it and reading it.
JavaScript (and other dynamic language) programmers shouldn't care as long as "var x" creates immutable variables. Functional programmers should prefer immutability. So that you know my biases, my daily programming is 80% Perl, 10% Java, 10% Haskell.Anyway, I'm interested to know what discussions were had on this point before Dart saw the light of day. Is the decision still open to change?
Am 21.10.2011 19:10 schrieb "Bob Nystrom" :
> If anything, I'd be more interested in the second definition of immutable: deep unchangeable objects. That seems a more valuable to me than making sure you can't set a local.
>
Bob, you are confusing me, as I do not consider these two things disjunct.
How can you discuss the deep unchangeability of objects without debating the default immutability of a variable?
OK, it is not so much the method locals but the member variables, but both should be treated consistently.
So the deep immutability of a containment hierarchy is AFAIC so far achieved by the finality of variables.
Every mutability of variables in the structure will make the whole structure mutable. Am I wrong?
KR
Det
KR
Det
Caja and ES5 (I think) have the ability to freeze an object, which makes it deeply immutable. Perhaps this is a good approach?
We should be careful around terminology here because "immutable" can mean a couple of different things depending on who you ask. It can mean:1. Single-assignment: a variable or field can be initialized and that's it.2. Deep transitive immutability: the object and everything it references are unchanging.
Given Dart's very strong syntactic similarity to C/C++/Java/JS/C# in particular, it would be astonishing to lots and lots of programmers if this was an error:int x = 3;x = 4;
I'd personally like to see something like this:var x = 7; // mutablefinal x = 7; // immutableint x = 7; // immutablevar int x = 7; // explicitly mutableThis seems like it punishes with verbosity the common case of a typed but re-assignable variable. Those are used heavily through our code and I'd hate to make them longer.
If your method is so long that you really need that to keep track of what's going on, your method is just too long.
We're OK with that because we can teach new Dart programmers to pay attention to the compiler warnings and to develop with type checking enabled.
It's a new language, so they'll have to learn a few new tricks. The benefits of optional typing are worth it.
In some ways, the above x*y failure is more troublesome because it fails quietly.
The single-assignment failure happens loudly and at compile time. If their code really needs both type information and multiple assignment, we can teach new Dart programmers to type "var int". The benefits are worth it :-)
I'd personally like to see something like this:var x = 7; // mutablefinal x = 7; // immutableint x = 7; // immutablevar int x = 7; // explicitly mutableThis seems like it punishes with verbosity the common case of a typed but re-assignable variable. Those are used heavily through our code and I'd hate to make them longer.I think we have an honest disagreement about which is the common case.
A quick analysis of my Java code shows that only 10% of variables are assigned more than once.
I've tried to leave my functional programming hat at home this time because this is actually an issue I encounter in large scale, imperative programming. Hopefully we can get some data to settle exactly how surprising single-assignment would be to Java/JavaScript/C# programmers.
main() {int x = 7;String y = "7";print( x*y );}
In some ways, the above x*y failure is more troublesome because it fails quietly.
I think it will throw a NoSuchMethod exception, so it's not very quiet.
This is a bug in dartc. It should cause a runtime error. This is part
of the challenge around compiling to JavaScript.
Cheers,
Peter
On Fri, Oct 21, 2011 at 1:21 PM, Michael Hendricks <mic...@ndrix.org> wrote:The single-assignment failure happens loudly and at compile time. If their code really needs both type information and multiple assignment, we can teach new Dart programmers to type "var int". The benefits are worth it :-)Well... you have to admit that's debatable. I like single-assignment but if that was the default in Dart, even though all of the languages that Dart is based on don't work that way, there would be a pretty high burden of proof that's it worth the change. I could be wrong, but I don't think single-assignment has shown it's worth to that degree yet.