www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Damn C++ and damn D!

reply so <so so.so> writes:
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.
Feb 05 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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; } }
Feb 05 2012
parent reply "so" <so so.so> writes:
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.
Feb 05 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Feb 05 2012
next sibling parent reply Jose Armando Garcia <jsancio gmail.com> writes:
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 =3D make(a);
 DOTDOTDOT;
 }else{
 auto tmp =3D make(a);
 auto var =3D 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. =A0Now 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 =3D 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?
Feb 05 2012
next sibling parent reply "so" <so so.so> writes:
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 = ... }
Feb 05 2012
parent Jose Armando Garcia <jsancio gmail.com> writes:
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 =3D 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) { =A0// initialize based on boolean condition being true =A0immutable variable =3D ... } else { =A0// initialize based on boolean condition being false =A0immutable variable =3D ... }
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 =3D 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
Feb 05 2012
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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 }();
Feb 05 2012
parent reply Jose Armando Garcia <jsancio gmail.com> writes:
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> =A0wrote:
 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> =A0void fun(T a){
 if(cond<T>::value) {
 auto var =3D make(a);
 DOTDOTDOT;
 }else{
 auto tmp =3D make(a);
 auto var =3D 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. =A0Now 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 programmi=
ng
 facilities are severely underpowered.
What I would really like to see in D is: immutable variable =3D if (boolean_condition) { =A0 // initialize based on boolean_condition being true } else { =A0 // 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 =3D (boolean_condition) ? { =A0 =A0// initialize based on boolean_condition being true }():{ =A0 =A0// initialize based on boolean_condition being false }();
Cool, now I want some syntactic sugar ;). I write this all the time!

Feb 05 2012
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/5/12 9:57 AM, Jose Armando Garcia wrote:
 On Sun, Feb 5, 2012 at 1:44 PM, Timon Gehr<timon.gehr gmx.ch>  wrote:
 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!
Make it a function. Andrei
Feb 05 2012
parent reply Jose Armando Garcia <jsancio gmail.com> writes:
On Sun, Feb 5, 2012 at 2:13 PM, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:
 On 2/5/12 9:57 AM, Jose Armando Garcia wrote:
 On Sun, Feb 5, 2012 at 1:44 PM, Timon Gehr<timon.gehr gmx.ch> =A0wrote:
 immutable variable =3D (boolean_condition) ? {
 =A0 =A0// initialize based on boolean_condition being true
 }():{
 =A0 =A0// initialize based on boolean_condition being false
 }();
Cool, now I want some syntactic sugar ;). I write this all the time!
Make it a function.
I agree that in theory that you can do all this stuff in D but the reality is that developers are lazy and end up fighting the language. It is probably true that in a well structure/abstracted program this is not a problem but the reality is that a lot programs don't start this way. I listen a very enlightening talk by Gilad Bracha the other day: https://www.youtube.com/watch?v=3D-IavVtOE_Fg. I still not ready to completely agree with him but I do see his point about a language getting out a programers way... -Jose
 Andrei
Feb 05 2012
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/5/12 10:25 AM, Jose Armando Garcia wrote:
 On Sun, Feb 5, 2012 at 2:13 PM, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org>  wrote:
 On 2/5/12 9:57 AM, Jose Armando Garcia wrote:
 On Sun, Feb 5, 2012 at 1:44 PM, Timon Gehr<timon.gehr gmx.ch>    wrote:
 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!
Make it a function.
I agree that in theory that you can do all this stuff in D but the reality is that developers are lazy and end up fighting the language. It is probably true that in a well structure/abstracted program this is not a problem but the reality is that a lot programs don't start this way.
I agree with the general argument but I find it disconnected from the case in point. It's not "all this stuff", it's one little local thing, and I'd find it tenuous to claim that such a small difference in syntax actually matters for anything.
 I listen a very enlightening talk by Gilad Bracha the other
 day: https://www.youtube.com/watch?v=-IavVtOE_Fg. I still not ready to
 completely agree with him but I do see his point about a language
 getting out a programers way...
I'll watch this, thanks. Andrei
Feb 05 2012
prev sibling parent Michel Fortin <michel.fortin michelf.com> writes:
On 2012-02-05 16:25:33 +0000, Jose Armando Garcia <jsancio gmail.com> said:

 On Sun, Feb 5, 2012 at 2:13 PM, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:
 Make it a function.
I agree that in theory that you can do all this stuff in D but the reality is that developers are lazy and end up fighting the language.
Make it a function literal: immutable variable = { if (boolean_condition) return 1 else return 2; }(); The extra () at the end calls the literal immediately so that variable is assigned the result. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2012
prev sibling next sibling parent "so" <so so.so> writes:
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.
Feb 05 2012
prev sibling parent reply Kai Meyer <kai unixlords.com> writes:
lambda instead of macro?

On 02/05/2012 07:57 AM, Timon Gehr 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.
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 06:49 PM, Kai Meyer wrote:
 On 02/05/2012 07:57 AM, Timon Gehr 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.
lambda instead of macro?
Won't work. Or am I missing something?
Feb 10 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, February 05, 2012 15:49:04 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.
So, basically, D does it way better than C++, but you're forced to continue to use C++ after seeing how well D does it, and it's driving you crazy. Yeah, that happens to me a lot too. - Jonathan M Davis
Feb 05 2012
parent deadalnix <deadalnix gmail.com> writes:
Le 06/02/2012 01:09, Jonathan M Davis a écrit :
 On Sunday, February 05, 2012 15:49:04 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.
So, basically, D does it way better than C++, but you're forced to continue to use C++ after seeing how well D does it, and it's driving you crazy. Yeah, that happens to me a lot too. - Jonathan M Davis
Hopefully, we have compiler/phobos bugs to force us to get back to C++.
Feb 06 2012