www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Const system names

reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent reply retard <re tard.com.invalid> writes:
Tue, 16 Feb 2010 12:34:15 -0500, bearophile wrote:

 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.

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.
Feb 16 2010
parent retard <re tard.com.invalid> writes:
Tue, 16 Feb 2010 17:45:06 +0000, retard wrote:

 Tue, 16 Feb 2010 12:34:15 -0500, bearophile wrote:
 
 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.

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.

Some common examples: --- #1 switch --- "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 } --- #2 foreach --- "mutable" string s = ""; foreach(elem; collection[0..collection.length-1]) { s ~= elem ~ ", "; } s ~= collection[collection.length-1]; -- -- val s = collection.head + collection.tail.foldLeft("")(_+", "+_)
Feb 16 2010
prev sibling parent dennis luehring <dl.soluz gmx.net> writes:
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?

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 me
Feb 16 2010