How to "extend" built-in types

simendsjo simendsjo at gmail.com
Sat Oct 27 02:58:57 PDT 2012


On Friday, 26 October 2012 at 16:32:29 UTC, Jonathan M Davis 
wrote:
> On Friday, October 26, 2012 15:55:34 simendsjo wrote:
>> So.. What do I need to implement for a struct to be a valid
>> built-in type?
>> All valid properties (min, max etc) and operators for that 
>> type?
>
> So, you want stuff like isFloatingPoint and isNumeric to return 
> true for a
> user-defined struct? That pretty much defeats their purpose if 
> that happens.
> They're checking for exact matches, not implicit conversions, 
> and a function
> intended to work explicitly with float isn't necessarily going 
> to work with
> your struct, so it needs isFloatingPoint to be false for 
> something that isn't
> truly a built-in floating point type.

Ok, maybe a bad example - I understand why those templates check 
for specific types.
The thing is that I often doesn't really care about the type, 
only that it exposes certain properties.
I don't want to force an int as parameter unless I need *all* the 
properties of an int. Often a method only requires that it have 
integer division and the mod operator or similar.
I can omit the constraint completely, but error messages isn't as 
good, the method is less explanatory, it gives less safety, and 
overloading becomes impossible.

I'd like the user to be able to use more specific behavior if 
needed.
A Ranged value, NonNull, Fraction etc. are good examples.




More information about the Digitalmars-d-learn mailing list