digitalmars.D.learn - How to simulate a new type
- Charles Hixson via Digitalmars-d-learn (7/7) May 14 2015 What are the downsides to simulating a new type with a struct.
- Laeeth Isharc (2/9) May 14 2015 http://dlang.org/phobos/std_typecons.html#.Typedef
- Charles Hixson via Digitalmars-d-learn (20/32) May 14 2015 Yes, that looks as if it would do the job, but what are its advantages
- Adam D. Ruppe (7/9) May 14 2015 None really, except perhaps automatic forwarding of operators
- Charles Hixson via Digitalmars-d-learn (3/12) May 14 2015 Thank you. I was hoping that struct was a good approach, I've just
- tired_eyes (3/19) May 15 2015 FWIW, I've seen a lot of user-defined types made with structs in
What are the downsides to simulating a new type with a struct. What I have in mind is something along the lines of: struct myType { uint64_t value; } The goal of this type is to prevent accidental conversions from myType into ints, uint64_ts, etc.
May 14 2015
On Thursday, 14 May 2015 at 18:42:56 UTC, Charles Hixson wrote:What are the downsides to simulating a new type with a struct. What I have in mind is something along the lines of: struct myType { uint64_t value; } The goal of this type is to prevent accidental conversions from myType into ints, uint64_ts, etc.
May 14 2015
On 05/14/2015 01:42 PM, Laeeth Isharc via Digitalmars-d-learn wrote:On Thursday, 14 May 2015 at 18:42:56 UTC, Charles Hixson wrote:Yes, that looks as if it would do the job, but what are its advantages over a simple struct? I'd be hesitant to try to predict the RAM layout of a templated variable, but it's my understanding that a struct has neither compile time nor runtime penalties over a simple variable, and that it doesn't increase RAM comsumption when included within another struct, or in a static array. What I was really wondering was whether the limitation of access caused by enclosing a variable within a struct was worth the benefit. Templates add a whole 'nother layer of complexity. For that matter I was thinking that, if I so chose, I could at some point add limits along the lines of Ada's types. I like being *able* to define conversions for types, but frequently I'd rather not have them arbitrarily assumed. Otherwise I'd just use alias, which almost does what I want. (E.g., one thing I might eventually want to do is use a couple of bits at the top of the uint64_t as flags, so if I did define operations I'd need to be able to mask the flag bits off before the operation and reapply them to the result, in some defined way of combining the flags. Not good if sometimes the value is going to be automatically reinterpreted as a plain number.)What are the downsides to simulating a new type with a struct. What I have in mind is something along the lines of: struct myType { uint64_t value; } The goal of this type is to prevent accidental conversions from myType into ints, uint64_ts, etc.
May 14 2015
On Friday, 15 May 2015 at 01:03:32 UTC, Charles Hixson wrote:Yes, that looks as if it would do the job, but what are its advantages over a simple struct?None really, except perhaps automatic forwarding of operators which is easy enough to do on a struct too (and with a struct, you can do only the ones that actually make sense for you). Typedef has a few major disadvantages too - surprising behavior if you forget the second parameter, for example. The library typedef should really be removed.
May 14 2015
On 05/14/2015 06:38 PM, Adam D. Ruppe via Digitalmars-d-learn wrote:On Friday, 15 May 2015 at 01:03:32 UTC, Charles Hixson wrote:Thank you. I was hoping that struct was a good approach, I've just never seen anyone recommending it, so I wanted to check.Yes, that looks as if it would do the job, but what are its advantages over a simple struct?None really, except perhaps automatic forwarding of operators which is easy enough to do on a struct too (and with a struct, you can do only the ones that actually make sense for you). Typedef has a few major disadvantages too - surprising behavior if you forget the second parameter, for example. The library typedef should really be removed.
May 14 2015
On Friday, 15 May 2015 at 06:11:41 UTC, Charles Hixson wrote:On 05/14/2015 06:38 PM, Adam D. Ruppe via Digitalmars-d-learn wrote:FWIW, I've seen a lot of user-defined types made with structs in the code of C-ported libs, and was sure that is a common approach.On Friday, 15 May 2015 at 01:03:32 UTC, Charles Hixson wrote:Thank you. I was hoping that struct was a good approach, I've just never seen anyone recommending it, so I wanted to check.Yes, that looks as if it would do the job, but what are its advantages over a simple struct?None really, except perhaps automatic forwarding of operators which is easy enough to do on a struct too (and with a struct, you can do only the ones that actually make sense for you). Typedef has a few major disadvantages too - surprising behavior if you forget the second parameter, for example. The library typedef should really be removed.
May 15 2015