digitalmars.D.learn - enum question
- Eric (15/15) Mar 18 2014 I would like to use enums with elements that are either a struct
- Adam D. Ruppe (6/10) Mar 18 2014 That's not necessarily a problem, especially if the struct is
- Eric (10/20) Mar 18 2014 Hmmm... What do you think the crossover point is for performance
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/30) Mar 18 2014 Someday is today! :)
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
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
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: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. -EricHowever, 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
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