www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Type properties

reply ric maicle <rmaicle gmail.com> writes:
Why is init allowed to be redefined but not sizeof?

dmd 2.069

import std.stdio;

struct Foo {
     static int init = 5;
     static int sizeof = 0;
}

void main()
{
     writeln(Foo.init);
     writeln(Foo.sizeof);
}

Error: variable integer.Foo.sizeof .sizeof property cannot be redefined
Jan 06 2016
next sibling parent ric maicle <rmaicle gmail.com> writes:
On Wednesday, 06 January, 2016 04:01 PM, ric maicle wrote:
 Why is init allowed to be redefined but not sizeof?

 dmd 2.069

 import std.stdio;

 struct Foo {
      static int init = 5;
      static int sizeof = 0;
 }

 void main()
 {
      writeln(Foo.init);
      writeln(Foo.sizeof);
 }

 Error: variable integer.Foo.sizeof .sizeof property cannot be redefined
Found this https://issues.dlang.org/show_bug.cgi?id=7066
Jan 06 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/6/16 3:01 AM, ric maicle wrote:
 Why is init allowed to be redefined but not sizeof?

 dmd 2.069

 import std.stdio;

 struct Foo {
      static int init = 5;
      static int sizeof = 0;
 }

 void main()
 {
      writeln(Foo.init);
      writeln(Foo.sizeof);
 }

 Error: variable integer.Foo.sizeof .sizeof property cannot be redefined
At the moment, we need this. The TypeInfo member that contains the initialization data was called init. It was very recently switched to initializer, but we cannot get rid of that alias for a few releases. After that, I think we can consider removing that ability from the language. One thing that just occurred to me though, Objective-C makes use of the init function quite a bit. I wonder how getting rid of init as a customizable member would affect that. -Steve
Jan 07 2016
parent Jacob Carlborg <doob me.com> writes:
On 2016-01-07 15:30, Steven Schveighoffer wrote:

 One thing that just occurred to me though, Objective-C makes use of the
 init function quite a bit. I wonder how getting rid of init as a
 customizable member would affect that.
NSObject init_() selector("init"); With the full support for Objective-C, binding the "init" method would not be necessary at all. This will be supported: auto foo = new NSObject; Which will be lowered to: auto foo = NSObject.alloc.init; It will also be possible to bind a D constructor to an init method: extern (Objective-C) class Foo : NSObject { this(Foo) selector("initWithFoo:"); } Although, it's still a breaking change, which affect other projects. -- /Jacob Carlborg
Jan 07 2016