Damn C++ and damn D!

Jose Armando Garcia jsancio at gmail.com
Sun Feb 5 07:57:20 PST 2012


On Sun, Feb 5, 2012 at 1:44 PM, Timon Gehr <timon.gehr at gmx.ch> wrote:
> On 02/05/2012 04:17 PM, Jose Armando Garcia wrote:
>>
>> On Sun, Feb 5, 2012 at 12:57 PM, Timon Gehr<timon.gehr at gmx.ch>  wrote:
>>>
>>> On 02/05/2012 03:53 PM, so wrote:
>>>>
>>>>
>>>> On Sunday, 5 February 2012 at 14:24:20 UTC, Timon Gehr wrote:
>>>>
>>>>> This should work:
>>>>>
>>>>> #define DOTDOTDOT ...
>>>>>
>>>>> template<class T>  void fun(T a){
>>>>> if(cond<T>::value) {
>>>>> auto var = make(a);
>>>>> DOTDOTDOT;
>>>>> }else{
>>>>> auto tmp = make(a);
>>>>> auto var = make_proxy(tmp);
>>>>> DOTDOTDOT;
>>>>> }
>>>>> }
>>>>
>>>>
>>>>
>>>> It won't work.
>>>> You now have two scopes and you have to repeat every line after "var"
>>>> for both scopes.  Now you have to maintain both of them.
>>>
>>>
>>>
>>> You just maintain the macro.
>>>
>>>
>>>> And this grows
>>>> exponentially for every new condition you have.
>>>>
>>>
>>> It certainly has limits. I completely agree that C++s generic programming
>>> facilities are severely underpowered.
>>>
>>
>> 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?
>
>
> immutable variable = (boolean_condition) ? {
>    // initialize based on boolean_condition being true
> }():{
>    // initialize based on boolean_condition being false
> }();
>
>

Cool, now I want some syntactic sugar ;). I write this all the time!

>
>


More information about the Digitalmars-d mailing list