digitalmars.D - Const system names
- bearophile (19/19) Feb 15 2010 Alternative names for the transitive const regime of D2, the main differ...
- Lars T. Kyllingstad (17/25) Feb 16 2010 I don't agree that immutable should be the the default. The default
- retard (17/33) Feb 16 2010 BTW, have you got some numbers from concrete examples such as real world...
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= (4/37) Feb 16 2010 Only if you're assuming that the assign() doesn't alter the integer, but...
- retard (12/53) Feb 16 2010 Yea, I don't really remember how the d2 const/immutability system works
- bearophile (16/20) Feb 16 2010 Is it true that mutables are the most common?
- retard (6/33) Feb 16 2010 Note that also function parameters are val in scala unlike in java or d....
- retard (23/58) Feb 16 2010 Some common examples:
- dennis luehring (5/7) Feb 16 2010 no - but (as we all know very well) most programmers do not pay any
- Simen kjaeraas (10/19) Feb 16 2010 I like the idea, and would like to try D with a system like this.
Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this). This is not compatible with C, but if you try to port C code to D2 with this const regime, the code can't assign new values to variables that lack the "var" attribute, it produce compile errors, so such porting doesn't look bug-prone to me. So this code: immutable int x1 = 1; const int x2 = 2; int x3 = 3; enum int x4 = 4; Becomes: int x1 = 1; valview int x2 = 2; var int x3 = 3; int x4 = 4; With such immutability by default, thread-local by default, notnull references by default, the language starts to look quite different from C++ ;-) Bye, bearophile
Feb 15 2010
bearophile wrote:Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this). [...]I don't agree that immutable should be the the default. The default should be the most common case, which is mutable. Regarding names; the fact that we currently have to write "enum x = 123;", which to me reads like "enumerate this single constant for me please, and BTW when I say 'enumerate' I really mean that I want to assign 123 to it", is one of the last remaining things in D that really bugs me. I have suggested the following scheme at least twice: const: manifest constants, no storage (like const in D1, enum in D2) readonly: a read-only view of mutable data (like const in D2) immutable: immutable data (like now) (Both times Yigal Chiprun was the first to reply, saying he thinks there should only be const and non-const, and the rest is up to the compiler. I'd almost be disappointed if that doesn't happen now. :) I still disagree, though.) -Lars
Feb 16 2010
Tue, 16 Feb 2010 10:28:28 +0100, Lars T. Kyllingstad wrote:bearophile wrote:BTW, have you got some numbers from concrete examples such as real world programming projects. Note that many variables are not immutable in real projects, but they also could be: Integer boxedIntegerSum(Integer a, Integer b) { Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; } Here you have three immutable variables a, b, and c. In reality they all could as well be constant references, a and b also could be immutable views: Integer boxedIntegerSum(immutable Integer a, immutable Integer b) { const Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; }Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this). [...]I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.
Feb 16 2010
On 02/16/2010 04:27 PM, retard wrote:Tue, 16 Feb 2010 10:28:28 +0100, Lars T. Kyllingstad wrote:Only if you're assuming that the assign() doesn't alter the integer, but in that case it's kind of meaningless. Also, requiring immutability for the input is not what you usually want.bearophile wrote:BTW, have you got some numbers from concrete examples such as real world programming projects. Note that many variables are not immutable in real projects, but they also could be: Integer boxedIntegerSum(Integer a, Integer b) { Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; } Here you have three immutable variables a, b, and c. In reality they all could as well be constant references, a and b also could be immutable views: Integer boxedIntegerSum(immutable Integer a, immutable Integer b) { const Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; }Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this). [...]I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.
Feb 16 2010
Tue, 16 Feb 2010 17:05:23 +0100, Pelle MÃ¥nsson wrote:On 02/16/2010 04:27 PM, retard wrote:Tue, 16 Feb 2010 10:28:28 +0100, Lars T. Kyllingstad wrote:Only if you're assuming that the assign() doesn't alter the integer, but in that case it's kind of meaningless.bearophile wrote:BTW, have you got some numbers from concrete examples such as real world programming projects. Note that many variables are not immutable in real projects, but they also could be: Integer boxedIntegerSum(Integer a, Integer b) { Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; } Here you have three immutable variables a, b, and c. In reality they all could as well be constant references, a and b also could be immutable views: Integer boxedIntegerSum(immutable Integer a, immutable Integer b) { const Integer c = new Integer(); c.assign(a.getValue() + b.getValue()); return c; }Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this). [...]I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.Also, requiring immutability for the input is not what you usually want.Yea, I don't really remember how the d2 const/immutability system works so that's why I said immutable view and not plain immutable. Basically a function usually shouldn't have an 'and' in its description so modifying the inputs is often not recommended. Anyways in other cases the references should be at least const if the objects aren't. This way the common noob bug: class foo { int a; this(int a) { a = a; } } can be avoided.
Feb 16 2010
Lars T. Kyllingstad:I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.<Is it true that mutables are the most common? Is it true that mutables will be the most common in D programs written few years from now? You must design a language for tomorrow, not for yesterday. Typing var " in the code is not that costly. While writing "immutable " often in the code is a lot of typing. Scala e Clojure like immutables, they are good for multicores :-) I've done a small search of the "val" and "var" attributes in Scala language: val (immutables): http://www.google.com/codesearch?q=%22val%22+lang%3Ascala 29_300 var (variables): http://www.google.com/codesearch?q=%22var%22+lang%3Ascala 15_200 It's not a scientific search, but shows that immutables are not that uncommon in Scala.const: manifest constants, no storage (like const in D1, enum in D2) readonly: a read-only view of mutable data (like const in D2) immutable: immutable data (like now)"const" and "readonly" are synonims, so I don't like those names, I prefer names that mean something different (related to their different semantics). (I agree that "valview" is not nice looking). Bye, bearophile
Feb 16 2010
Tue, 16 Feb 2010 12:34:15 -0500, bearophile wrote:Lars T. Kyllingstad:Note that also function parameters are val in scala unlike in java or d. From my experiences with various kinds of (functional) languages with first class functional features, const values become the common case. Why? Because everything can return a value, you don't need uninitialized references.I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.<Is it true that mutables are the most common? Is it true that mutables will be the most common in D programs written few years from now? You must design a language for tomorrow, not for yesterday. Typing var " in the code is not that costly. While writing "immutable " often in the code is a lot of typing. Scala e Clojure like immutables, they are good for multicores :-) I've done a small search of the "val" and "var" attributes in Scala language: val (immutables): http://www.google.com/codesearch?q=%22val%22+lang%3Ascala 29_300 var (variables): http://www.google.com/codesearch?q=%22var%22+lang%3Ascala 15_200 It's not a scientific search, but shows that immutables are not that uncommon in Scala.
Feb 16 2010
Tue, 16 Feb 2010 17:45:06 +0000, retard wrote:Tue, 16 Feb 2010 12:34:15 -0500, bearophile wrote:Some common examples: "mutable" int a = initial_value; switch(some_condition) { case case_1: a = something; break; case case_2: a = something_else; break; default: a = default_value; } -- -- val a = some_condition match { case case_1 => something case case_2 => something_else case _ => default_value } "mutable" string s = ""; foreach(elem; collection[0..collection.length-1]) { s ~= elem ~ ", "; } s ~= collection[collection.length-1]; -- -- val s = collection.head + collection.tail.foldLeft("")(_+", "+_)Lars T. Kyllingstad:Note that also function parameters are val in scala unlike in java or d. From my experiences with various kinds of (functional) languages with first class functional features, const values become the common case. Why? Because everything can return a value, you don't need uninitialized references.I don't agree that immutable should be the the default. The default should be the most common case, which is mutable.<Is it true that mutables are the most common? Is it true that mutables will be the most common in D programs written few years from now? You must design a language for tomorrow, not for yesterday. Typing var " in the code is not that costly. While writing "immutable " often in the code is a lot of typing. Scala e Clojure like immutables, they are good for multicores :-) I've done a small search of the "val" and "var" attributes in Scala language: val (immutables): http://www.google.com/codesearch?q=%22val%22+lang%3Ascala 29_300 var (variables): http://www.google.com/codesearch?q=%22var%22+lang%3Ascala 15_200 It's not a scientific search, but shows that immutables are not that uncommon in Scala.
Feb 16 2010
no - but (as we all know very well) most programmers do not pay any attention to (i)mutable or const, final and whatever at all so i would also vote for immutable everywhere possible - the few cases where mutables are needed - not based on programmers lazyness - are no-counters for meI don't agree that immutable should be the the default. The default should be the most common case, which is mutable.<Is it true that mutables are the most common?
Feb 16 2010
bearophile <bearophileHUGS lycos.com> wrote:Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default: - "immutable" ==> (nothing, it's the default). - "const" ==> "valview" (a local immutable value view of something). - mutable variable ==> "var" - "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this).I like the idea, and would like to try D with a system like this. However, I disagree with the names. See, with my beautiful collection of bike shed colors, I would keep 'const' for 'const' (for backward- compatibility and familiarity, and I think valview is an abomination), and use 'alias' instead of 'enum' (which I still consider the most glaring error in the history of D). For transition purposes, 'immutable' could of course be a no-op. -- Simen
Feb 16 2010