www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Old problem with performance

reply Kagamin <spam here.lot> writes:
Weed Wrote:

 Good (I think) use cases have been in this thread

They may be good, but they don't show what can be done in C++ and can't be done in D. You were advised to use structs for value semantic.
Feb 20 2009
next sibling parent reply Bill Baxter <wbaxter gmail.com> writes:
2009/2/21 Weed <resume755 mail.ru>:
 Kagamin =D0=C9=DB=C5=D4:
 Weed Wrote:

 Good (I think) use cases have been in this thread

They may be good, but they don't show what can be done in C++ and can't =


 We are beginning to repeat itself. Okay. :)

 On D you may write anything. Even without cycles! With only "if" and
 "goto" keywords. The question of the costs of such a code.  :)

 An example is unnecessarily complex code here:
 http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/Mat=

 See line 227, template MultReturnType(ArgT) {

 This template is necessary to receive by value a structures containing
 the vectors and matrices as mathematical operations results. Receiving
 the vectors and matrices by value in structures needed to increase
 performance.
 Try to add here another 3 or 4 types of the returned object to this
 template?

I think I answered this already, but here goes again: 1) D2 has auto return type deduction, so this whole template probably isn't necessary in D2, you just use typeof(return) instead, IIRC. 2) A better way to manage the case where you are concerned about supporting any kind of user type is to have the caller supply a "traits" template that describes how the type implements the underlying concept, how to access an element in a generic way, etc. I didn't do that in the above code because A) I hadn't figured that out yet, and B) I moved on to the next thing once I had something working, and the above was doing what I needed it to do.
 In C++ vector, matrix, and all other possible objects would have been
 the class derived from common base class and this problem does not occur.

I'm sure if you look you can also find many C++ matrix classes that are not written that way. Probably fewer are written the way you suggest, actually. But as always you're plenty vague about what you mean here. Do you mean the mult function would return a generic Matrix type? That function does a by-value return. You'd have slicing if you did that with polymorphic classes. Actually from what I've seen, most C++ matrix libraries that support generic types do so by heavy use of argument-dependent lookup (ADL) aka Koenig lookup. Basically you just define your own mult<MyType> specialization in your own header (or maybe you use the generic mult template, but have a specialization GetElement<MyType> in your own header. There are various ways to do it). The way C++ works, these will get used by the generic template if they are the best match. D doesn't have ADL, and I griped about it for a while because it makes porting those kinds of C++ code difficult. But in the end I think that style of programming leads to maintenance headaches. You see GetElement being called in some module you're reading and you want to see its definition -- it could be anywhere! With the D/traits approach I mentioned above such code must be injected explicitly into the template via a parameter of some kind, which gives the maintenance programmer a little bit better chance of finding where it comes from. It may not be so much better, but anyway it seems to allow you to do the same kinds of things you could do with ADL. If you want to go the polymorphic route with D you can. As I think has been pointed out plenty. Why don't you just show us the class in the way you would like to write it in C++, and we'll show you how to write it in D, or finally agree with you that it's not possible. But as long as you continue to be hand-wavy about "common base classes" we're at a bit of an impasse. So far everyone thinks D can do what you want it to do based on your vague descriptions. --bb
Feb 20 2009
parent reply Weed <resume755 mail.ru> writes:
Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.

Yes, it's a good idea
Feb 21 2009
parent reply Weed <resume755 mail.ru> writes:
Weed пишет:
 Bill Baxter пишет:
 
 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


As I said, you can write everything using "goto" and "if". ...But why you do not like the original example of this thread?
 Yes, it's a good idea

Feb 21 2009
parent reply Weed <resume755 mail.ru> writes:
Bill Baxter пишет:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

Please post again. I don't seem to recall any detailed example. --bb

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
Feb 21 2009
next sibling parent reply Weed <resume755 mail.ru> writes:
Bill Baxter пишет:
 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter пишет:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb


You should use a struct there! Your code does not show you doing anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?
Feb 22 2009
parent reply Weed <resume755 mail.ru> writes:
Weed пишет:
 Bill Baxter пишет:
 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter пишет:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

Explains why the question is given in this form: I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?
Feb 23 2009
next sibling parent reply Don <nospam nospam.com> writes:
Weed wrote:
 Weed пишет:
 Bill Baxter пишет:
 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter пишет:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

Explains why the question is given in this form: I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?

Actually, in D, it's really difficult to give a class value semantics. If a, b are members of some class, consider (1) a = a + b; (2) a += b; It is nearly impossible to efficiently make both of these have the same effect!! You can only do it with either copy-on-write, ie, even case (2) always allocates; or by using a proxy class. This is something which I consider to be a serious problem. Much more serious than the speed issue.
Feb 23 2009
parent Weed <resume755 mail.ru> writes:
Don пишет:
 Weed wrote:
 Weed пишет:
 Bill Baxter пишет:
 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter пишет:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or
 finally
 agree with you that it's not possible.   But as long as you
 continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to
 do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

Explains why the question is given in this form: I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?

Actually, in D, it's really difficult to give a class value semantics. If a, b are members of some class, consider (1) a = a + b; (2) a += b; It is nearly impossible to efficiently make both of these have the same effect!! You can only do it with either copy-on-write, ie, even case (2) always allocates; or by using a proxy class. This is something which I consider to be a serious problem. Much more serious than the speed issue.

We already talked about this idea: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81432 (see text "It is possible to think up other example where there is no overload:")
Feb 24 2009
prev sibling parent reply naryl <cy ngs.ru> writes:
Weed Wrote:

 Weed О©╫О©╫О©╫О©╫О©╫:
 Bill Baxter О©╫О©╫О©╫О©╫О©╫:
 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter О©╫О©╫О©╫О©╫О©╫:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed О©╫О©╫О©╫О©╫О©╫:
 Bill Baxter О©╫О©╫О©╫О©╫О©╫:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

Explains why the question is given in this form: I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?

If you pass it by value you'll lose polymorphism. That must mean that you inherit that class only to avoid duplicating code. And that is easily done with template mixins.
Feb 23 2009
parent reply Weed <resume755 mail.ru> writes:
naryl пишет:

 --bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?

If you pass it by value you'll lose polymorphism.

Debatable By the way, in my example the transfer class by value is not important(!), it is important to create a temporary class on the stack. Then we can pass a reference without problem (C++ allows this through the "&")
 That must mean that you inherit that class only to avoid duplicating code. And
that is easily done with template mixins.

It is possible that this polymorphism is not needed and should be prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.
Feb 23 2009
next sibling parent reply Don <nospam nospam.com> writes:
Weed wrote:
 naryl пишет:
 
 --bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?


Debatable By the way, in my example the transfer class by value is not important(!), it is important to create a temporary class on the stack. Then we can pass a reference without problem (C++ allows this through the "&")

Sure, but you're not using polymorphism.
 
 That must mean that you inherit that class only to avoid duplicating code. And
that is easily done with template mixins.

It is possible that this polymorphism is not needed and should be prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

This is the fundamental tradeoff at the heart of the matter. In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision. D gets significant benefits from this. C++ allows you to defer the decision, but it doesn't come for free. (Generally speaking, statically typed languages do force you to make more decisions at design time). Notice that you have little gotchas in C++, such as the need to declare a virtual destructor on every struct you think you might "someday" use polymorphically. It sticks around, even if it never gets used. One of the nice things about a D struct, compared to a C++ struct, is that you *know* it's simple, it never has that kind of baggage. D does choose different trade-offs from C++. If it was always the same, it'd be the same language! BTW, if you're concerned about performance, you'd do well to use compile-time polymorphism rather than run-time, when possible. D's metaprogramming support leaves C++ for dead.
Feb 23 2009
parent reply Weed <resume755 mail.ru> writes:
Don пишет:
 Weed wrote:
 naryl пишет:

 --bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?


Debatable By the way, in my example the transfer class by value is not important(!), it is important to create a temporary class on the stack. Then we can pass a reference without problem (C++ allows this through the "&")

Sure, but you're not using polymorphism.

There is no, but the same object can be used in other places, including the polymorphism
 That must mean that you inherit that class only to avoid duplicating
 code. And that is easily done with template mixins.

It is possible that this polymorphism is not needed and should be prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

This is the fundamental tradeoff at the heart of the matter. In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision.

This is awful! Much easier to give to the application programmer to decide how it will use the derived class somewhere. Such a division should be a basic principle of designing a complex program.
 D gets significant benefits
 from this.

Benefits: 1. Disappeared slicing 2. ?
 C++ allows you to defer the decision, but it doesn't come for
 free.

Clarify what you mean?
 (Generally speaking, statically typed languages do force you to make
 more decisions at design time). Notice that you have little gotchas in
 C++, such as the need to declare a virtual destructor on every struct
 you think you might "someday" use polymorphically. It sticks around,
 even if it never gets used.
 One of the nice things about a D struct, compared to a C++ struct, is
 that you *know* it's simple, it never has that kind of baggage.
 
 D does choose different trade-offs from C++. If it was always the same,
 it'd be the same language!

There is no need to compare the structs from C++ and D. In fact, in C++ classes and structures are the same. I like D's idea of POD structs + without them you can not ensure compatibility with C, but it is very important. Now we are talking about classes. In C++ classes with the same problems, such as what they do not have a common base class (Object in D). Passing classes by value is not a problem - is an advantage. As I said earlier, the presence of pointers is also an advantage, although they are dangerous and can lead to complex bugs.
 
 BTW, if you're concerned about performance, you'd do well to use
 compile-time polymorphism rather than run-time, when possible. D's
 metaprogramming support leaves C++ for dead.
 

I think if there was a language with all the features as in D but with the model objects from C++, he would have won great popularity. Mainly it would have moved C++ developers who are satisfied with the model classes of C++ but not satisfied with the absence of other modern features: metaprogramming, contracts, delegates, closures etc.
Feb 24 2009
parent reply Don <nospam nospam.com> writes:
Weed wrote:
 Don пишет:
 Weed wrote:
 naryl пишет:

 --bb


anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for.

Why? What if I have not substantiated the fact that c1 is a class I should use there a structure? Used in D a model of placement classes only in heap have a rule "if you made the class and trying to pass it by value is somewhere in your code, there is a design error?

I am received or wrote a classes. Is it right to overload the operator opAdd and use them? I think yes. But why not allow this operation at the same speed that allows C++?


By the way, in my example the transfer class by value is not important(!), it is important to create a temporary class on the stack. Then we can pass a reference without problem (C++ allows this through the "&")


There is no, but the same object can be used in other places, including the polymorphism
 That must mean that you inherit that class only to avoid duplicating
 code. And that is easily done with template mixins.

prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision.

This is awful! Much easier to give to the application programmer to decide how it will use the derived class somewhere. Such a division should be a basic principle of designing a complex program.

Even in C++, all of your base classes should be abstract. If you want to move between polymorphism and non-polymorphism in C++, it's a non-trivial refactoring.
 D gets significant benefits
 from this.

Benefits: 1. Disappeared slicing 2. ?

That's not what I had in mind at all. I don't think slicing is such a big deal in itself; it's just a symptom.
 C++ allows you to defer the decision, but it doesn't come for
 free.

Clarify what you mean?
 (Generally speaking, statically typed languages do force you to make
 more decisions at design time). Notice that you have little gotchas in
 C++, such as the need to declare a virtual destructor on every struct
 you think you might "someday" use polymorphically. It sticks around,
 even if it never gets used.
 One of the nice things about a D struct, compared to a C++ struct, is
 that you *know* it's simple, it never has that kind of baggage.

 D does choose different trade-offs from C++. If it was always the same,
 it'd be the same language!

There is no need to compare the structs from C++ and D. In fact, in C++ classes and structures are the same.

Well, although the keywords are identical, there are two different varieties of C++ objects muddied together: PODs, and polymorphic types. You declare a type to be polymorphic by declaring a virtual function inside it. In D, you do it with the 'class' keyword.
 I like D's idea of POD structs + without them you can not ensure
 compatibility with C, but it is very important.
 
 Now we are talking about classes.
 
 In C++ classes with the same problems, such as what they do not have a
 common base class (Object in D). Passing classes by value is not a
 problem - is an advantage.

Passing a polymorphic class by value rarely makes sense. You can't get (runtime) polymorphism unless you go through a pointer.
 As I said earlier, the presence of pointers is also an advantage,
 although they are dangerous and can lead to complex bugs.

I don't think that is analagous. The issue is not primarily about with the 'danger' of passing classes by value. It's about clear separation of concepts.
 BTW, if you're concerned about performance, you'd do well to use
 compile-time polymorphism rather than run-time, when possible. D's
 metaprogramming support leaves C++ for dead.

I think if there was a language with all the features as in D but with the model objects from C++, he would have won great popularity. Mainly it would have moved C++ developers who are satisfied with the model classes of C++ but not satisfied with the absence of other modern features: metaprogramming, contracts, delegates, closures etc.

I think D will get adequate popularity once it has a good library situation. It only needs language changes inasfaras they are necessary for library design.
Feb 24 2009
parent reply Weed <resume755 mail.ru> writes:
Don пишет:

 That must mean that you inherit that class only to avoid duplicating
 code. And that is easily done with template mixins.

prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision.

This is awful! Much easier to give to the application programmer to decide how it will use the derived class somewhere. Such a division should be a basic principle of designing a complex program.

Even in C++, all of your base classes should be abstract.

Why?
 If you want to move between polymorphism and non-polymorphism in C++,
 it's a non-trivial refactoring.
 
 
 D gets significant benefits
 from this.

Benefits: 1. Disappeared slicing 2. ?

That's not what I had in mind at all. I don't think slicing is such a big deal in itself; it's just a symptom.

What do you mean?
 C++ allows you to defer the decision, but it doesn't come for
 free.

Clarify what you mean?
 (Generally speaking, statically typed languages do force you to make
 more decisions at design time). Notice that you have little gotchas in
 C++, such as the need to declare a virtual destructor on every struct
 you think you might "someday" use polymorphically. It sticks around,
 even if it never gets used.
 One of the nice things about a D struct, compared to a C++ struct, is
 that you *know* it's simple, it never has that kind of baggage.

 D does choose different trade-offs from C++. If it was always the same,
 it'd be the same language!

There is no need to compare the structs from C++ and D. In fact, in C++ classes and structures are the same.

Well, although the keywords are identical, there are two different varieties of C++ objects muddied together: PODs, and polymorphic types. You declare a type to be polymorphic by declaring a virtual function inside it. In D, you do it with the 'class' keyword.
 I like D's idea of POD structs + without them you can not ensure
 compatibility with C, but it is very important.

 Now we are talking about classes.

 In C++ classes with the same problems, such as what they do not have a
 common base class (Object in D). Passing classes by value is not a
 problem - is an advantage.

Passing a polymorphic class by value rarely makes sense. You can't get (runtime) polymorphism unless you go through a pointer.

Or reference (&). Thus, even polymorphic class on stack can be used safely in C++.
 
 As I said earlier, the presence of pointers is also an advantage,
 although they are dangerous and can lead to complex bugs.

I don't think that is analagous. The issue is not primarily about with the 'danger' of passing classes by value. It's about clear separation of concepts.
 BTW, if you're concerned about performance, you'd do well to use
 compile-time polymorphism rather than run-time, when possible. D's
 metaprogramming support leaves C++ for dead.

I think if there was a language with all the features as in D but with the model objects from C++, he would have won great popularity. Mainly it would have moved C++ developers who are satisfied with the model classes of C++ but not satisfied with the absence of other modern features: metaprogramming, contracts, delegates, closures etc.

I think D will get adequate popularity once it has a good library situation. It only needs language changes inasfaras they are necessary for library design.

What D better than Java or C#?
Feb 24 2009
parent reply Don <nospam nospam.com> writes:
Weed wrote:
 Don пишет:
 
 That must mean that you inherit that class only to avoid duplicating
 code. And that is easily done with template mixins.

prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision.

decide how it will use the derived class somewhere. Such a division should be a basic principle of designing a complex program.


Why?

See, for example, http://www.artima.com/intv/modern.html
 
 If you want to move between polymorphism and non-polymorphism in C++,
 it's a non-trivial refactoring.


 D gets significant benefits
 from this.

1. Disappeared slicing 2. ?

big deal in itself; it's just a symptom.

What do you mean?

Language complexity.
 
 C++ allows you to defer the decision, but it doesn't come for
 free.

 (Generally speaking, statically typed languages do force you to make
 more decisions at design time). Notice that you have little gotchas in
 C++, such as the need to declare a virtual destructor on every struct
 you think you might "someday" use polymorphically. It sticks around,
 even if it never gets used.
 One of the nice things about a D struct, compared to a C++ struct, is
 that you *know* it's simple, it never has that kind of baggage.

 D does choose different trade-offs from C++. If it was always the same,
 it'd be the same language!

classes and structures are the same.

varieties of C++ objects muddied together: PODs, and polymorphic types. You declare a type to be polymorphic by declaring a virtual function inside it. In D, you do it with the 'class' keyword.
 I like D's idea of POD structs + without them you can not ensure
 compatibility with C, but it is very important.

 Now we are talking about classes.

 In C++ classes with the same problems, such as what they do not have a
 common base class (Object in D). Passing classes by value is not a
 problem - is an advantage.

You can't get (runtime) polymorphism unless you go through a pointer.

Or reference (&). Thus, even polymorphic class on stack can be used safely in C++.

Same thing. You're either not using value semantics, or not using polymorphism. Not both at once.
 
 As I said earlier, the presence of pointers is also an advantage,
 although they are dangerous and can lead to complex bugs.

the 'danger' of passing classes by value. It's about clear separation of concepts.
 BTW, if you're concerned about performance, you'd do well to use
 compile-time polymorphism rather than run-time, when possible. D's
 metaprogramming support leaves C++ for dead.

the model objects from C++, he would have won great popularity. Mainly it would have moved C++ developers who are satisfied with the model classes of C++ but not satisfied with the absence of other modern features: metaprogramming, contracts, delegates, closures etc.

situation. It only needs language changes inasfaras they are necessary for library design.

What D better than Java or C#?

I'm wasting my time here. I'll not make any further comments on this subject.
Feb 25 2009
parent reply Weed <resume755 mail.ru> writes:
Don пишет:
 Weed wrote:
 Don пишет:

 That must mean that you inherit that class only to avoid duplicating
 code. And that is easily done with template mixins.

prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

In D, the choice of whether an object will use polymorphism or not is considered a fundamental design decision.

decide how it will use the derived class somewhere. Such a division should be a basic principle of designing a complex program.


Why?

See, for example, http://www.artima.com/intv/modern.html

It did not say how that all base classes should be abstract
 
 If you want to move between polymorphism and non-polymorphism in C++,
 it's a non-trivial refactoring.


 D gets significant benefits
 from this.

1. Disappeared slicing 2. ?

big deal in itself; it's just a symptom.

What do you mean?

Language complexity.

We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.
 C++ allows you to defer the decision, but it doesn't come for
 free.

 (Generally speaking, statically typed languages do force you to make
 more decisions at design time). Notice that you have little gotchas in
 C++, such as the need to declare a virtual destructor on every struct
 you think you might "someday" use polymorphically. It sticks around,
 even if it never gets used.
 One of the nice things about a D struct, compared to a C++ struct, is
 that you *know* it's simple, it never has that kind of baggage.

 D does choose different trade-offs from C++. If it was always the
 same,
 it'd be the same language!

classes and structures are the same.

varieties of C++ objects muddied together: PODs, and polymorphic types. You declare a type to be polymorphic by declaring a virtual function inside it. In D, you do it with the 'class' keyword.
 I like D's idea of POD structs + without them you can not ensure
 compatibility with C, but it is very important.

 Now we are talking about classes.

 In C++ classes with the same problems, such as what they do not have a
 common base class (Object in D). Passing classes by value is not a
 problem - is an advantage.

You can't get (runtime) polymorphism unless you go through a pointer.

Or reference (&). Thus, even polymorphic class on stack can be used safely in C++.

Same thing. You're either not using value semantics, or not using polymorphism. Not both at once.

If you needed polymorphism you should use references or pointers, where necessary performance you use value semantics. C++ allows both options, D - no.
 
 As I said earlier, the presence of pointers is also an advantage,
 although they are dangerous and can lead to complex bugs.

the 'danger' of passing classes by value. It's about clear separation of concepts.
 BTW, if you're concerned about performance, you'd do well to use
 compile-time polymorphism rather than run-time, when possible. D's
 metaprogramming support leaves C++ for dead.

the model objects from C++, he would have won great popularity. Mainly it would have moved C++ developers who are satisfied with the model classes of C++ but not satisfied with the absence of other modern features: metaprogramming, contracts, delegates, closures etc.

situation. It only needs language changes inasfaras they are necessary for library design.

What D better than Java or C#?

I'm wasting my time here. I'll not make any further comments on this subject.

Okay
Feb 25 2009
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.

I like D1 mostly because it's quite less complex that C++, that's the first thing I ask to a new language like D. Complexity "kills". You probably don't want D, you want ATS: http://www.ats-lang.org/ Bye, bearophile
Feb 25 2009
next sibling parent reply Weed <resume755 mail.ru> writes:
bearophile пишет:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.

I like D1 mostly because it's quite less complex that C++, that's the first thing I ask to a new language like D. Complexity "kills".

I am cite myself: "That is actually a model of classes D harder than C++."
 You probably don't want D, you want ATS:
 http://www.ats-lang.org/

There uses preprocessor. It is wrong.
Feb 25 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Weed wrote:
 bearophile пишет:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.


I am cite myself: "That is actually a model of classes D harder than C++."

Man, wish I could have done that when writing my thesis...
 You probably don't want D, you want ATS:
 http://www.ats-lang.org/

There uses preprocessor. It is wrong.

Wow. I dare say you're missing the forest for the trees. -- daniel
Feb 25 2009
parent Weed <resume755 mail.ru> writes:
Daniel Keep пишет:
 
 Weed wrote:
 bearophile пишет:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.


"That is actually a model of classes D harder than C++."

Man, wish I could have done that when writing my thesis...

:) Sorry, this has already been discussed here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=84842
Feb 25 2009
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.

I like D1 mostly because it's quite less complex that C++, that's the first thing I ask to a new language like D. Complexity "kills". You probably don't want D, you want ATS: http://www.ats-lang.org/ Bye, bearophile

http://www.ats-lang.org/EXAMPLE/MISC/listquicksort.dats Dear god. I think... I think I'm going to go cry in the corner... -- Daniel
Feb 25 2009
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Bill Baxter wrote:
 On Wed, Feb 25, 2009 at 11:47 PM, Daniel Keep
 <daniel.keep.lists gmail.com> wrote:
 bearophile wrote:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BASIC.

You probably don't want D, you want ATS: http://www.ats-lang.org/ Bye, bearophile

Dear god. I think... I think I'm going to go cry in the corner...

Even the helpful explanatory comment is baffling. Sounds like the guy is boasting because he not only implemented Quicksort but managed to do it in such a way that the output not only has the same length as the input, but *also* is a permutation of the input! Whoa! Sheer genius! --bb

Is it one of those deals where he somehow explained to the compiler what he wants his algorithm to do, and then the compiler goes off and proves to itself that the algorithm actually does it or else barfs?
Feb 25 2009
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Weed wrote:
 Don п©п╦я┬п╣я┌:
 Language complexity.

We do not schoolgirls! :)

I guess superdan might disagree here :o). Andrei
Feb 25 2009
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
Weed Wrote:

 But why not allow this operation at the same speed that allows C++?

If you pass it by value you'll lose polymorphism.

It is possible that this polymorphism is not needed and should be prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

It's probably already designed to be reference-type. I think, you'll have big troubles trying to change its semantic to value-type and effectively you'll have to redo it.
Feb 24 2009
parent reply Christopher Wright <dhasenan gmail.com> writes:
Kagamin wrote:
 Weed Wrote:
 
 But why not allow this operation at the same speed that allows C++?


prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

It's probably already designed to be reference-type. I think, you'll have big troubles trying to change its semantic to value-type and effectively you'll have to redo it.

In other words, it is not always easy to translate C++ to D. It's probably not worthwhile to translate any large codebase into another programming language, in most cases, and if you do, you will probably need to rewrite large portions of it anyway.
Feb 24 2009
parent reply Weed <resume755 mail.ru> writes:
Christopher Wright пишет:
 Kagamin wrote:
 Weed Wrote:

 But why not allow this operation at the same speed that allows C++?


prohibited for operations by value. The class is ready, why it should not be used and have to redo it? Can not think about changing when the class is ready - he could have enormous complexity and hierarchy.

It's probably already designed to be reference-type. I think, you'll have big troubles trying to change its semantic to value-type and effectively you'll have to redo it.

In other words, it is not always easy to translate C++ to D. It's probably not worthwhile to translate any large codebase into another programming language, in most cases, and if you do, you will probably need to rewrite large portions of it anyway.

As a result, classes will be slow, or require more code to achieve speeds comparable to C++. That is actually a model of classes D harder than C++.
Feb 24 2009
parent reply Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 As a result, classes will be slow, or require more code to achieve
 speeds comparable to C++.
 
 That is actually a model of classes D harder than C++.

Yes, C++ offers more unsafe optimizations than D.
Feb 24 2009
parent reply Weed <resume755 mail.ru> writes:
Christopher Wright пишет:
 Weed wrote:
 As a result, classes will be slow, or require more code to achieve
 speeds comparable to C++.

 That is actually a model of classes D harder than C++.

Yes, C++ offers more unsafe optimizations than D.

Straight to the point! I am choosing unsafe but fast code. Unsafe at so much on how much is unsafe any code on C++. I think the issue is closed. :)
Feb 25 2009
parent Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 Christopher Wright пишет:
 Weed wrote:
 As a result, classes will be slow, or require more code to achieve
 speeds comparable to C++.

 That is actually a model of classes D harder than C++.


Straight to the point! I am choosing unsafe but fast code. Unsafe at so much on how much is unsafe any code on C++. I think the issue is closed. :)

I spoke poorly. C++ enshrines unsafe optimizations as essential language features. D simply provides enough metaprogramming and low-level stuff that you can do it with a library. C++ gives you a revolver pointed at your foot, and you maneuver your target between the revolver and your foot. D gives you a kit to make a gun in 5 easy steps!!! and lets you point it at your foot if you want. For instance, you can come up with templates that give you struct polymorphism. But you yourself said that that isn't a good idea, and that you should not use value types in a polymorphic manner.
Feb 25 2009
prev sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Feb 25, 2009 at 11:47 PM, Daniel Keep
<daniel.keep.lists gmail.com> wrote:
 bearophile wrote:
 Weed:
 We do not schoolgirls! :) Who is afraid of the complexity should use BA=



 I like D1 mostly because it's quite less complex that C++, that's the fi=


 You probably don't want D, you want ATS:
 http://www.ats-lang.org/

 Bye,
 bearophile

http://www.ats-lang.org/EXAMPLE/MISC/listquicksort.dats Dear god. =A0I think... I think I'm going to go cry in the corner...

Even the helpful explanatory comment is baffling. Sounds like the guy is boasting because he not only implemented Quicksort but managed to do it in such a way that the output not only has the same length as the input, but *also* is a permutation of the input! Whoa! Sheer genius! --bb
Feb 25 2009
prev sibling parent Yigal Chripun <yigal100 gmail.com> writes:
Bill Baxter wrote:
 On Sun, Feb 22, 2009 at 5:10 AM, Denis Koroskin<2korden gmail.com>  wrote:
 On Sat, 21 Feb 2009 21:49:46 +0300, Bill Baxter<wbaxter gmail.com>  wrote:

 On Sun, Feb 22, 2009 at 1:02 AM, Weed<resume755 mail.ru>  wrote:
 Bill Baxter пишет:
 2009/2/21 Weed<resume755 mail.ru>:
 Weed пишет:
 Bill Baxter пишет:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do
 based
 on your vague descriptions.


...But why you do not like the original example of this thread?

--bb

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506

anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for. --bb


I am vaguely open to the possibility that he has a point. FLENS uses inheritance, for instance. But I don't recall what the intent of that inheritance was in FLENS, exactly. I more or less ported it to D without that, though, so I guess I didn't find it to be essential. See this: http://flens.cvs.sourceforge.net/viewvc/flens/FLENS/matvec/densevector.h?revision=1.36&view=markup Versus this: http://www.dsource.org/projects/multiarray/browser/trunk/multiarray/dflat/DenseVector.d Weed, if you want to make a real argument, that would be a good place to start. Investigate why and how FLENS uses inheritance. --bb

From a quick look at the flens DenseVector class: it inherits from a Vector Interface which only declares the destructor as virtual. My educated guess is that this interface has a similar purpose to the "struct interfaces" feature discussed here before - similar to C++ concepts. I don't see any polymorphism here - the functions aren't virtual.
Feb 22 2009
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
2009/2/21 Weed <resume755 mail.ru>:
 Weed =D0=C9=DB=C5=D4:
 Bill Baxter =D0=C9=DB=C5=D4:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do based
 on your vague descriptions.


As I said, you can write everything using "goto" and "if". ...But why you do not like the original example of this thread?

Please post again. I don't seem to recall any detailed example. --bb
Feb 21 2009
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter =D0=C9=DB=C5=D4:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed =D0=C9=DB=C5=D4:
 Bill Baxter =D0=C9=DB=C5=D4:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do base=





 on your vague descriptions.


...But why you do not like the original example of this thread?

Please post again. I don't seem to recall any detailed example. --bb

http://www.digitalmars.com/pnews/read.php?server=3Dnews.digitalmars.com&g=

You should use a struct there! Your code does not show you doing anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for. --bb
Feb 21 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 21 Feb 2009 21:49:46 +0300, Bill Baxter <wbaxter gmail.com> wrote:

 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter п©п╦я┬п╣я┌:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed п©п╦я┬п╣я┌:
 Bill Baxter п©п╦я┬п╣я┌:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finally
 agree with you that it's not possible.   But as long as you continue
 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do  
 based
 on your vague descriptions.


...But why you do not like the original example of this thread?

Please post again. I don't seem to recall any detailed example. --bb

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506

You should use a struct there! Your code does not show you doing anything that would even remotely suggest using a class is worthwhile. You're doing value operations on value types. That's what structs are for. --bb

That's what *everyone* tells him!
Feb 21 2009
prev sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Sun, Feb 22, 2009 at 5:10 AM, Denis Koroskin <2korden gmail.com> wrote:
 On Sat, 21 Feb 2009 21:49:46 +0300, Bill Baxter <wbaxter gmail.com> wrote=

 On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755 mail.ru> wrote:
 Bill Baxter =D0=C9=DB=C5=D4:
 2009/2/21 Weed <resume755 mail.ru>:
 Weed =D0=C9=DB=C5=D4:
 Bill Baxter =D0=C9=DB=C5=D4:

 Why don't you just show us the class in the way you would like to
 write it in C++, and we'll show you how to write it in D, or finall=







 agree with you that it's not possible.   But as long as you continu=







 to be hand-wavy about "common base classes" we're at a bit of an
 impasse.  So far everyone thinks D can do what you want it to do
 based
 on your vague descriptions.


As I said, you can write everything using "goto" and "if". ...But why you do not like the original example of this thread?

Please post again. I don't seem to recall any detailed example. --bb

http://www.digitalmars.com/pnews/read.php?server=3Dnews.digitalmars.com=



 You should use a struct there!   Your code does not show you doing
 anything that would even remotely suggest using a class is worthwhile.
  You're doing value operations on value types.  That's what structs
 are for.

 --bb

That's what *everyone* tells him!

I am vaguely open to the possibility that he has a point. FLENS uses inheritance, for instance. But I don't recall what the intent of that inheritance was in FLENS, exactly. I more or less ported it to D without that, though, so I guess I didn't find it to be essential. See this: http://flens.cvs.sourceforge.net/viewvc/flens/FLENS/matvec/densevector.h?= revision=3D1.36&view=3Dmarkup Versus this: http://www.dsource.org/projects/multiarray/browser/trunk/multiarray/dflat= /DenseVector.d Weed, if you want to make a real argument, that would be a good place to start. Investigate why and how FLENS uses inheritance. --bb
Feb 21 2009