tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
46 stars 11 forks source link

reduce primitive obsession, allow calculation with types like duration #619

Open michaellilltokiwa opened 1 year ago

michaellilltokiwa commented 1 year ago

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

fridis commented 1 year ago

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

simonvonhackewitz commented 3 weeks ago

related to #621