Let's say I want my method to accept anything that is "number like" i.e. knows how to negate, add, subtract, multiply and divide. It needs to do these with itself and with numbers (Int32 and Float64 for my purposes)
abstract struct Numberlike
alias Num = (Int32 | Float64)
abstract def -
abstract def +(other : self)
abstract def +(other : Num)
abstract def -(other : self)
abstract def -(other : Num)
abstract def *(other : self)
abstract def *(other : Num)
abstract def /(other : self)
abstract def /(other : Num)
end
I'm having a problem where my child struct seems to need to operate on all Numberlike
instead of just self. For example
struct Term
alias Num = (Int32 | Float64)
getter coeff : Num
getter sym : Symbol
def initialize(@coeff, @sym); end
def -(other : self)
self.class.new(coeff - other.coeff, sym)
end
end
The above can return
abstract `def Numberlike#-(other : self)` must be implemented by Term
because the compiler interprets my requirement as "be sure that all numberlike can operate on all other numberlike" but what I want to find a way to say is "be sure that all terms can operate on numbers and terms (self) as well".