digitalmars.D - Sturcts with constructor (dmd1.x)
- Nrgyzer (22/22) Jan 11 2010 Hello everyone,
- dsimcha (18/40) Jan 11 2010 allowed for structs
- bearophile (4/6) Jan 11 2010 Struct constructors are probably the D2 feature I miss more in D1 :-)
- grauzone (2/9) Jan 11 2010
- bearophile (4/7) Jan 12 2010 During optimization phases I sometimes want to convert classes into stru...
- Ellery Newcomer (5/28) Jan 11 2010 with
- Strtr (2/8) Jan 12 2010 How much of an overhead is a call to a struct instance compared to a cal...
- bearophile (6/7) Jan 12 2010 The situation is more complex than that. Structs don't have a pointer to...
- Strtr (3/11) Jan 12 2010 Thank you.
- Nrgyzer (2/47) Jan 12 2010 Thanks - that solution works great :)
Hello everyone, I currently try to create a structure with a construtor, but I always get the following messages: Error: Foo.this constructors are only for class definitions Error: constructor lisim.lsResult.lsResult.this special member functions not allowed for structs By compiling the follwing code: struct Foo { int len; bool len_done; const char* str; int length() { if (!len_done) { len = strlen(str); len_done = true; } return len; } this(char* str) { this.str = str; } } const Foo f = Foo("hello"); bar(f.length); I found that source on http://www.digitalmars.com/d/2.0/const-faq.html#logical-const. I know that this source is for d2 and not for d1 but does d1 support similar operations (or is there no support for struct-constructors)? Thanks in advance :)
Jan 11 2010
== Quote from Nrgyzer (nrgyzer googlemail.com)'s articleHello everyone, I currently try to create a structure with a construtor, but I always get thefollowing messages:Error: Foo.this constructors are only for class definitions Error: constructor lisim.lsResult.lsResult.this special member functions notallowed for structsBy compiling the follwing code: struct Foo { int len; bool len_done; const char* str; int length() { if (!len_done) { len = strlen(str); len_done = true; } return len; } this(char* str) { this.str = str; } } const Foo f = Foo("hello"); bar(f.length); I found that source onhttp://www.digitalmars.com/d/2.0/const-faq.html#logical-const. I know that this source is for d2 and not for d1 but does d1 support similar operations (or is there no support for struct-constructors)?Thanks in advance :)Struct constructors are D2 only. That said, you can fake them in D1 by overloading static opCall: struct Foo { uint field; static Foo opCall(SomeType someArgument) { Foo foo; // Do stuff. foo.field = someValue; return foo; } } auto foo = Foo(someArgument);
Jan 11 2010
dsimcha:Struct constructors are D2 only. That said, you can fake them in D1 by overloading static opCall:Struct constructors are probably the D2 feature I miss more in D1 :-) Bye, bearophile
Jan 11 2010
bearophile wrote:dsimcha:Why?Struct constructors are D2 only. That said, you can fake them in D1 by overloading static opCall:Struct constructors are probably the D2 feature I miss more in D1 :-)Bye, bearophile
Jan 11 2010
grauzone:During optimization phases I sometimes want to convert classes into structs, and then I want to allocate some of those struct instances on the heap and others on the stack. A struct constructor allows me to change as little as possible to the code that uses those new structs. Bye, bearophileStruct constructors are probably the D2 feature I miss more in D1 :-)Why?
Jan 12 2010
On 01/11/2010 04:06 PM, Nrgyzer wrote:Hello everyone, I currently try to create a structure with a construtor, but I always get the following messages: Error: Foo.this constructors are only for class definitions Error: constructor lisim.lsResult.lsResult.this special member functions not allowed for structs By compiling the follwing code: struct Foo { int len; bool len_done; const char* str; int length() { if (!len_done) { len = strlen(str); len_done = true; } return len; } this(char* str) { this.str = str; } } const Foo f = Foo("hello"); bar(f.length); I found that source on http://www.digitalmars.com/d/2.0/const-faq.html#logical-const. I know that this source is for d2 and not for d1 but does d1 support similar operations (or is there no support for struct-constructors)? Thanks in advance :)get rid of the constructor and replaceconst Foo f = Foo("hello");with const Foo f = Foo(0,false,"hello"); and it will work, if you can forgive how ugly it is.
Jan 11 2010
bearophile Wrote:grauzone:How much of an overhead is a call to a struct instance compared to a call to a object?During optimization phases I sometimes want to convert classes into structs, and then I want to allocate some of those struct instances on the heap and others on the stack. A struct constructor allows me to change as little as possible to the code that uses those new structs.Struct constructors are probably the D2 feature I miss more in D1 :-)Why?
Jan 12 2010
Strtr:How much of an overhead is a call to a struct instance compared to a call to a object?The situation is more complex than that. Structs don't have a pointer to the virtual table and monitor, that saves memory and time. Less memory saves you time if you have to allocate a large number of structs, for cache effects. They can be allocated inside arrays (placement new allows you to do the same with objects too), and generally LDC seems able to inline and manage them more efficiently, it doesn't call a struct destructor. If you for example write a ray tracer in D and you compile it with the quite good LDC compiler, you can see a significant performance difference between using a struct to represent a 3D vector (3 doubles, 24 bytes, always stack-allocated) than using objects (even scoped objects, always allocated on the stack). In D there are structs for a purpose. D compilers aren't currently able to de-virtualize calls, so if you have a tree made with two kinds of nodes (internal ones and leaves) and you need to transverse it a very large number of times, you may end doing a large number of virtual calls (you have two versions of the walking methods). In this situation I've seen that using 3 tagged structs, one "virtual" little struct that gets never instantiated, plus the two different nodes, the LDC compiler is quite able to partially inline such recursive calls (it can be done with an iterative stack too, but the final performance is about the same and the code gets uglier), and the performance is visibly higher. Sometimes D structs allow you to optimize in ways that Python doesn't allow you to :-) (Java HotSpot is able to partially optimize this by itself, de-virtualizing and then partially inlining the virtual recursive calls. But D language is designed to not need such advanced compiler technology). Bye, bearophile
Jan 12 2010
bearophile Wrote:Strtr:Thank you. I will read wikipedia about the virtual method table.How much of an overhead is a call to a struct instance compared to a call to a object?The situation is more complex than that. Structs don't have a pointer to the virtual table and monitor, that saves memory and time. Less memory saves you time if you have to allocate a large number of structs, for cache effects. They can be allocated inside arrays (placement new allows you to do the same with objects too), and generally LDC seems able to inline and manage them more efficiently, it doesn't call a struct destructor. If you for example write a ray tracer in D and you compile it with the quite good LDC compiler, you can see a significant performance difference between using a struct to represent a 3D vector (3 doubles, 24 bytes, always stack-allocated) than using objects (even scoped objects, always allocated on the stack). In D there are structs for a purpose. D compilers aren't currently able to de-virtualize calls, so if you have a tree made with two kinds of nodes (internal ones and leaves) and you need to transverse it a very large number of times, you may end doing a large number of virtual calls (you have two versions of the walking methods). In this situation I've seen that using 3 tagged structs, one "virtual" little struct that gets never instantiated, plus the two different nodes, the LDC compiler is quite able to partially inline such recursive calls (it can be done with an iterative stack too, but the final performance is about the same and the code gets uglier), and the performance is visibly higher. Sometimes D structs allow you to optimize in ways that Python doesn't allow you to :-) (Java HotSpot is able to partially optimize this by itself, de-virtualizing and then partially inlining the virtual recursive calls. But D language is designed to not need such advanced compiler technology).
Jan 12 2010
dsimcha Wrote:== Quote from Nrgyzer (nrgyzer googlemail.com)'s articleThanks - that solution works great :)Hello everyone, I currently try to create a structure with a construtor, but I always get thefollowing messages:Error: Foo.this constructors are only for class definitions Error: constructor lisim.lsResult.lsResult.this special member functions notallowed for structsBy compiling the follwing code: struct Foo { int len; bool len_done; const char* str; int length() { if (!len_done) { len = strlen(str); len_done = true; } return len; } this(char* str) { this.str = str; } } const Foo f = Foo("hello"); bar(f.length); I found that source onhttp://www.digitalmars.com/d/2.0/const-faq.html#logical-const. I know that this source is for d2 and not for d1 but does d1 support similar operations (or is there no support for struct-constructors)?Thanks in advance :)Struct constructors are D2 only. That said, you can fake them in D1 by overloading static opCall: struct Foo { uint field; static Foo opCall(SomeType someArgument) { Foo foo; // Do stuff. foo.field = someValue; return foo; } } auto foo = Foo(someArgument);
Jan 12 2010