Const system names

retard re at tard.com.invalid
Tue Feb 16 09:55:06 PST 2010


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("")(_+", "+_)



More information about the Digitalmars-d mailing list