fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

reduce primitive obsession, allow calculation with types like duration

Open michaellilltokiwa opened this issue 3 years ago • 2 comments

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...

michaellilltokiwa avatar Nov 12 '22 09:11 michaellilltokiwa

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...

fridis avatar Nov 30 '22 15:11 fridis

related to #621

simonvonhackewitz avatar Aug 22 '24 14:08 simonvonhackewitz

I would find subtraction and division for time.duration useful. @maxteufel is there something against it?

simonvonhackewitz avatar Mar 10 '25 16:03 simonvonhackewitz

@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?

maxteufel avatar Mar 11 '25 06:03 maxteufel

This is basically implemented, we may want to add stuff like division as mentioned by @simonvonhackewitz but this is for another issue.

michaellilltokiwa avatar Mar 13 '25 12:03 michaellilltokiwa