In Crystal this is handled via the differentiation between def initialize
and def self.new
. Usually the rule of thumb is use self.new
overloads to transform the arguments to fit a smaller amount of initialize
methods. E.g. your example could be written as:
class Test
def self.new(f_or_s : Float64 | String) : self
new f_or_s.to_i
end
def initialize(@value : Int32)
raise ArgumentError.new "parameter error" if @value > 50
end
end
Also checkout Constructor variants and inheritance, as it has some more detailed information on how the two relate, especially when inheritance is introduced.