www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum question

reply "Eric" <eric makechip.com> writes:
I would like to use enums with elements that are either a struct 
type
or a class type.  However, using struct type seems inefficient 
because
structs are pass by value.  Apparently you can't pass an enum as 
a reference,
so eliminating the pass by value for enums does not seem 
possible. I have read
on this forum that someday enums may be able to be made with 
class elements.
Can enums be made of classes yet, or is there a way to make the 
enum based
on structs more efficient?

Thanks,
Eric
Mar 18 2014
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 18 March 2014 at 20:40:36 UTC, Eric wrote:
 However, using struct type seems inefficient because structs 
 are pass by value.
That's not necessarily a problem, especially if the struct is small, passing by value is faster than by reference. What is your code trying to do?
 Can enums be made of classes yet, or is there a way to make the 
 enum based on structs more efficient?
enums can't be classes, but you could make a static class constructed at compile time and pass that reference around.
Mar 18 2014
parent "Eric" <eric makechip.com> writes:
On Tuesday, 18 March 2014 at 20:56:45 UTC, Adam D. Ruppe wrote:
 On Tuesday, 18 March 2014 at 20:40:36 UTC, Eric wrote:
 However, using struct type seems inefficient because structs 
 are pass by value.
That's not necessarily a problem, especially if the struct is small, passing by value is faster than by reference. What is your code trying to do?
 Can enums be made of classes yet, or is there a way to make 
 the enum based on structs more efficient?
enums can't be classes, but you could make a static class constructed at compile time and pass that reference around.
Hmmm... What do you think the crossover point is for performance of value vs reference? ie, how large can a struct be before passing it around gets slower than passing around a reference? (I would guess 8 bytes, but that's just a guess). I am trying to emulate the java enum type. Enums are great for data safety in API design. The more immutable data they contain the better in my opinion - hence my concern about performance. -Eric
Mar 18 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/18/2014 01:40 PM, Eric wrote:

 Apparently you can't pass an enum as a reference,
It is possible as seen in the following code.
 I have read on this forum that someday enums may be able to be
 made with class elements.
Someday is today! :) class C { int i; this(int i) { this.i = i; } } enum E : C { one = new C(1), two = new C(2) } void foo(ref E e) { e = E.two; } void main() { auto e = E.one; foo(e); assert(e == E.two); } Ali
Mar 18 2014