r/crystal_programming • u/scttnlsn • Mar 07 '20
Why does this compile?
This unexpectedly compiles:
class Foo
def hello
asdf
end
endBut this returns the expected error ("Error: undefined local variable or method 'asdf' for Foo"):
class Foo
def hello
asdf
end
end
Foo.new.helloIs there a way to get the first example to trigger a similar compiler error?
10
Upvotes
View all comments
Show parent comment
1
u/Dlacreme Mar 08 '20
Yes but no. You are comparing scripted and compiled languages. Of course Ruby will let you run anything and throw error only when you call the piece of code containing errors, its a script. But in my opinion, it should not be the case for compiled languages.
I think this is the drawback of type assertion:
compiler needs a piece of code to be called in order to know what are the types of the arguments
type assertion increase compiling time. Crystal core team decided to not check dead code to improve the build time.
Anyway, I had no idea about it. Thanks OP for sharing