www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to simulate a new type

reply Charles Hixson via Digitalmars-d-learn writes:
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
parent reply "Laeeth Isharc" <laeeth nospamlaeeth.com> writes:
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
parent reply Charles Hixson via Digitalmars-d-learn writes:
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:
 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.
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.)
May 14 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent reply Charles Hixson via Digitalmars-d-learn writes:
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:
 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.
Thank you. I was hoping that struct was a good approach, I've just never seen anyone recommending it, so I wanted to check.
May 14 2015
parent "tired_eyes" <pastuhov85 gmail.com> writes:
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:
 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.
Thank you. I was hoping that struct was a good approach, I've just never seen anyone recommending it, so I wanted to check.
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.
May 15 2015