I am trying to create a method which gets input from the user and converts that to a specified type. Is there a way to do this without rewriting for each type.
Something like this:
struct Input(InputType)
def self.get_from_stdin(msg_fail : String = "Error! Wrong type, please reenter: ")
input = gets
begin
input = input.to_s.to(InputType)
rescue
puts msg_fail
input = Input(InputType).get_from_stdin
end
input
end
end
age = Input(Int32).get_from_stdin("Age must be a number, please reenter: ")
Basically, I want to achieve something akin to this:
foo = foo.to(MyType)
I imagine this would be difficult to do without macros.