the compiler will try to infer the type from some_expression. In this case it’s this:
x || nil
Can we infer something from x? Let’s look if it has a type restriction. Nope, it doesn’t have any. Can we infer something from nil? Yes! It’s nil so it must be of type Nil. Well, Nil or something else (x) that we don’t know the type of. So the end result is that we can only infer it to be Nil.
It doesn’t matter what you pass to the constructor. The compiler doesn’t look at calls to infer the type of instance vars: just what you assign to @x inside the constructor (or, well, any other method, but never calls to methods.)
At this point I wouldn’t mind having to specify the type of instance variables explicitly. It doesn’t take much time and it would solve a lot of these issues/discussions. And it might make the code clearer. I hope maybe for Crystal 2.0…
At this point I wouldn’t mind having to specify the type of instance variables explicitly. It doesn’t take much time and it would solve a lot of these issues/discussions. And it might make the code clearer. I hope maybe for Crystal 2.0…
Indeed, I fell upon this error with:
@description : String? = nil
There is no other @description manipulation in the class because I’m doing TDD, so it is completely normal that the code is not complete yet. When I declare the type explicitly I’m waiting for the compiler to just swallow it and don’t complain about.
EDIT : okay, nevermind, declaring the type at the class level does the trick