reduce primitive obsession, allow calculation with types like duration
Right now it is not directly possible to add two durations. We will also have this problem when we add other units like the SI-units for example. I thought of adding something like this:
# T the actual underlying numeric type
# U the numericy type
numericy (T (numeric T).type, U (numericy T U).type) is
as_numeric T is abstract
as_numericy (a T) U is abstract
infix + (other U) U is
as_numericy (as_numeric+other.as_numeric)
infix - (other U) U is
as_numericy (as_numeric-other.as_numeric)
infix * (other U) U is
as_numericy (as_numeric*other.as_numeric)
infix / (other U) U is
as_numericy (as_numeric/other.as_numeric)
...
in duration.fz
duration (
# the duration in nano seconds
#
nanos u64
) : numericy u64 duration
is
as_numeric => nanos
as_numericy(a u64) => duration a
...
now we can do this:
ex is
a := (time.duration 1) + (time.duration 100)
say a
@maxteufel @WaelYoussfi @fridis Please share your thoughts on this suggestion. Also I need help with the names...
Good suggestion! In the example, I would prefer to do something like
ex : time.durations is
a := (ns 500) + (years 5)
say a
but I think this would be possible with your suggestion.
I think it would be even better if numericy would extend numeric, but this might be difficult, so keep this as an idea for later (and until numeric is stable:-).
I have some difficulties getting used to the name numericy. How about wrapped_numeric and then rename as_numeric/as_numericy as unwrap/wrap?
This would turn out really cool once postfix calls are supported:
ex : time.durations is
a := 500 ns + 5 years
say a
but still have to check how much confusion this would cause in other contexts...
related to #621
I would find subtraction and division for time.duration useful. @maxteufel is there something against it?
@simonvonhackewitz Do you have a specific example where subtraction and division would be useful? How do we deal with fractions of nanoseconds in the case of divisions?
This is basically implemented, we may want to add stuff like division as mentioned by @simonvonhackewitz but this is for another issue.