digitalmars.D.learn - initializer list like in C++?
- Hoenir (5/5) Jul 24 2007 Is there anything like the initializer list (C++) in D?
- Chris Nicholson-Sauls (7/13) Jul 24 2007 class Test {
- Jarrett Billingsley (5/10) Jul 24 2007 No. See
- James Dennett (14/26) Jul 24 2007 I looked at that thread, but found only references to
- BCS (9/36) Jul 24 2007 IIRC this works
- James Dennett (8/48) Jul 24 2007 Can c be modified more than once during construction? If not,
- Chris Nicholson-Sauls (14/64) Jul 24 2007 As I understand it:
- Daniel919 (3/3) Jul 24 2007 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars....
- Hoenir (1/1) Jul 24 2007 Thank you all for your answers :)
Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }
Jul 24 2007
Hoenir wrote:Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }class Test { int x ; this (int y) { x = y; } } ;) Or in short: no. Not that I've ever missed it at all. -- Chris Nicholson-Sauls
Jul 24 2007
"Hoenir" <mrmocool gmx.de> wrote in message news:f84rnt$1db0$1 digitalmars.com...Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }No. See http://www.digitalmars.com/d/archives/digitalmars/D/initializat on_lists_55262.html for an explanation on why D doesn't have them.
Jul 24 2007
Jarrett Billingsley wrote:"Hoenir" <mrmocool gmx.de> wrote in message news:f84rnt$1db0$1 digitalmars.com...I looked at that thread, but found only references to performance, which miss the main reason why these are present/useful in C++: semantics. There are various kinds of things in C++ that can be initialized but cannot be modified, because of references, const-ness, and classes with immutable value semantics. The occasional/slight performance benefit is definitely secondary to the ability to express meaning directly in the code. As D gains the ability to model such immutable members, it might benefit from finding (its own) solution to the same issue. -- JamesIs there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }No. See http://www.digitalmars.com/d/archives/digitalmars/D/initializat on_lists_55262.html for an explanation on why D doesn't have them.
Jul 24 2007
Reply to James,Jarrett Billingsley wrote:IIRC this works const int i; static this(){i=5;} class C { const char c; this(char c_){c=c_;} }"Hoenir" <mrmocool gmx.de> wrote in message news:f84rnt$1db0$1 digitalmars.com...I looked at that thread, but found only references to performance, which miss the main reason why these are present/useful in C++: semantics. There are various kinds of things in C++ that can be initialized but cannot be modified, because of references, const-ness, and classes with immutable value semantics. The occasional/slight performance benefit is definitely secondary to the ability to express meaning directly in the code. As D gains the ability to model such immutable members, it might benefit from finding (its own) solution to the same issue.Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }No. See http://www.digitalmars.com/d/archives/digitalmars/D/initialization_li sts_55262.html for an explanation on why D doesn't have them.
Jul 24 2007
BCS wrote:Reply to James,Can c be modified more than once during construction? If not, what rules are used to determine when it's been modified? Java came up with "definite assignment" rules for this kind of things, and they're pretty decent -- they guarantee that a final value is assigned a value exactly once, while allowing for some flexibility such as setting it down all branches of an if/else. -- JamesJarrett Billingsley wrote:IIRC this works const int i; static this(){i=5;} class C { const char c; this(char c_){c=c_;} }"Hoenir" <mrmocool gmx.de> wrote in message news:f84rnt$1db0$1 digitalmars.com...I looked at that thread, but found only references to performance, which miss the main reason why these are present/useful in C++: semantics. There are various kinds of things in C++ that can be initialized but cannot be modified, because of references, const-ness, and classes with immutable value semantics. The occasional/slight performance benefit is definitely secondary to the ability to express meaning directly in the code. As D gains the ability to model such immutable members, it might benefit from finding (its own) solution to the same issue.Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }No. See http://www.digitalmars.com/d/archives/digitalmars/D/initialization_li sts_55262.html for an explanation on why D doesn't have them.
Jul 24 2007
James Dennett wrote:BCS wrote:As I understand it: === D/1.x === Const fields/variables can be assigned and otherwise modified freely within relevant constructors, being otherwise iron-clad. I believe its a compile-time check only, with available hardware protections at runtime. === D/2.x === Final fields/variables are assign-once, from anywhere, with some check bit being set thereafter to prevent rebinding. Const takes on a meaning relevant to the actual data only, and so means nothing for the discussion at hand. (I may be wrong; for example, it may be that constructors specifically take liberties with 2.x final as they do with 1.x const.) -- Chris Nicholson-SaulsReply to James,Can c be modified more than once during construction? If not, what rules are used to determine when it's been modified? Java came up with "definite assignment" rules for this kind of things, and they're pretty decent -- they guarantee that a final value is assigned a value exactly once, while allowing for some flexibility such as setting it down all branches of an if/else. -- JamesJarrett Billingsley wrote:IIRC this works const int i; static this(){i=5;} class C { const char c; this(char c_){c=c_;} }"Hoenir" <mrmocool gmx.de> wrote in message news:f84rnt$1db0$1 digitalmars.com...I looked at that thread, but found only references to performance, which miss the main reason why these are present/useful in C++: semantics. There are various kinds of things in C++ that can be initialized but cannot be modified, because of references, const-ness, and classes with immutable value semantics. The occasional/slight performance benefit is definitely secondary to the ability to express meaning directly in the code. As D gains the ability to model such immutable members, it might benefit from finding (its own) solution to the same issue.Is there anything like the initializer list (C++) in D? class Test { int x; Test(int y):x(y){}; }No. See http://www.digitalmars.com/d/archives/digitalmars/D/initialization_li sts_55262.html for an explanation on why D doesn't have them.
Jul 24 2007
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=55262 Best regards, Daniel
Jul 24 2007