View mode: basic / threaded / horizontal-split · Log in · Help
February 05, 2012
Damn C++ and damn D!
After some time with D, C++ is now a nightmare for me. (especially on
generic coding)
Think about replicating this simple code in C++.

void fun(T)(T a)
{
    static if(cond(T))
    {
       auto var = make(a);
    }
    else
    {
       auto tmp = make(a);
       auto var = make_proxy(tmp);
    }
    ...
}

And this is just "one" condition.
Damn D for introducing these and damn C++ committee for not adopting.
February 05, 2012
Re: Damn C++ and damn D!
On 02/05/2012 02:49 PM, so wrote:
> After some time with D, C++ is now a nightmare for me. (especially on
> generic coding)
> Think about replicating this simple code in C++.
>
> void fun(T)(T a)
> {
> static if(cond(T))
> {
> auto var = make(a);
> }
> else
> {
> auto tmp = make(a);
> auto var = make_proxy(tmp);
> }
> ...
> }
>
> And this is just "one" condition.
> Damn D for introducing these and damn C++ committee for not adopting.

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;
    }
}
February 05, 2012
Re: Damn C++ and damn D!
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. And 
this grows exponentially for every new condition you have.
February 05, 2012
Re: Damn C++ and damn D!
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.
February 05, 2012
Re: Damn C++ and damn D!
On Sun, Feb 5, 2012 at 12:57 PM, Timon Gehr <timon.gehr@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?
February 05, 2012
Re: Damn C++ and damn D!
On Sunday, 5 February 2012 at 14:57:58 UTC, Timon Gehr wrote:
> On 02/05/2012 03:53 PM, so wrote:
> You just maintain the macro.

1. When actual part of the work lies in the macro.

#define DETAIL \
... \
... \
...

You now have an enigma and compiler won't help you.

2. And if you need another condition.
Another macro? Just imagine the situation!

Maybe small but branching also creates overhead.
February 05, 2012
Re: Damn C++ and damn D!
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 = ...
}
February 05, 2012
Re: Damn C++ and damn D!
On 02/05/2012 04:17 PM, Jose Armando Garcia wrote:
> On Sun, Feb 5, 2012 at 12:57 PM, Timon Gehr<timon.gehr@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
}();
February 05, 2012
Re: Damn C++ and damn D!
On Sun, Feb 5, 2012 at 1:24 PM, so <so@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
February 05, 2012
Re: Damn C++ and damn D!
On Sun, Feb 5, 2012 at 1:44 PM, Timon Gehr <timon.gehr@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@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!

>
>
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home