Neat: UFCS for integer dot operator suffix

Peter Alexander peter.alexander.au at gmail.com
Mon Sep 24 11:46:18 PDT 2012


On Monday, 24 September 2012 at 17:47:33 UTC, David Piepgrass 
wrote:
> However, unit checking cannot be done satisfactorially in a 
> library; it has two main problems when provided that way:
> 1. It's too bulky (too much syntax required, as units have to 
> be spelled out constantly)
> 2. Values with traditionally-typed units don't interoperate 
> with existing libraries, including very simple functions such as
>
> int abs(int x) { return x > 0 ? x : -x; }
> int square(int x) { return x*x; }
>
> You can define an inplicit conversion from e.g. 'Unit!"pixels"' 
> to 'int' but then you'll need to manually cast it back, and the 
> compiler can't check your cast to make sure it's correct.
>
> IMO, solving these two problems requires a parallel type system 
> to infer unit relationships automatically, either with direct 
> language support, or a separate analysis tool that uses the 
> compiler as a service (currently not possible with D).

+1

You can partially solve the function arg/return value problem by 
making those functions templates but:

(a) This doesn't help you with existing functions (e.g. trig 
function in std.math)
(b) It stops those functions from being virtual.
(c) Generally makes the functions more difficult to work with (no 
common type, horrible compilation errors)


More information about the Digitalmars-d mailing list