www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Sturcts with constructor (dmd1.x)

reply Nrgyzer <nrgyzer googlemail.com> writes:
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
next sibling parent reply dsimcha <dsimcha yahoo.com> writes:
== Quote from Nrgyzer (nrgyzer googlemail.com)'s article
 Hello everyone,
 I currently try to create a structure with a construtor, but I always get the

 Error: Foo.this constructors are only for class definitions
 Error: constructor lisim.lsResult.lsResult.this special member functions not

 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

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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply grauzone <none example.net> writes:
bearophile wrote:
 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 :-)

Why?
 Bye,
 bearophile

Jan 11 2010
parent bearophile <bearophileHUGS lycos.com> writes:
grauzone:
 Struct constructors are probably the D2 feature I miss more in D1 :-)

Why?

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, bearophile
Jan 12 2010
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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 replace
 const 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