Damn C++ and damn D!

Jose Armando Garcia jsancio at gmail.com
Sun Feb 5 07:52:27 PST 2012


On Sun, Feb 5, 2012 at 1:24 PM, so <so at so.so> wrote:
> On Sunday, 5 February 2012 at 15:17:39 UTC, Jose Armando Garcia wrote:
>
>> What I would really like to see in D is:
>>
>> immutable variable = if (boolean condition)
>> {
>> // initialize based on boolean condition being true
>> }
>> else
>> {
>> // initialize based on boolean condition being false
>> }
>>
>> Scala has this and find it indispensable for functional and/or
>> immutable programming. Yes, I have been programming with Scala a lot
>> lately. It has a lot of problem but it has some really cool constructs
>> like the one above. Scala also has pattern matching and structural
>> typing but that may be asking too much ;).
>>
>> I am not sure what it would take to implement this in D but I am
>> thinking we need the concept of a void type (Unit in scala). Thoughts?
>
>
> What am i missing?
> I can't see the difference between that and "static if".
>
> static if (boolean condition)
> {
>  // initialize based on boolean condition being true
>  immutable variable = ...
> }
> else
> {
>  // initialize based on boolean condition being false
>  immutable variable = ...
> }
>

First, 'static if' is evaluated at compile time while 'if' is valuated
at runtime. Second 'if' in D is a statement while in Scala it is a
expression think of it as a more powerful (more readable) version of
the ternary operator ?:. In theory in Scala you need to write:

auto variable = if (true)
{
  1
}
else
{
  2
};

but Scala can implicitly derive the ';'.

Thanks,
-Jose
PS. static if works in your case because "... static if is not only a
statement but also a declaration." -The D Programming Language


More information about the Digitalmars-d mailing list