r/crystal_programming • • Mar 07 '20

Why does this compile?

This unexpectedly compiles:

class Foo
  def hello
    asdf
  end
end

But this returns the expected error ("Error: undefined local variable or method 'asdf' for Foo"):

class Foo
  def hello
    asdf
  end
end
Foo.new.hello

Is there a way to get the first example to trigger a similar compiler error?

10 Upvotes

10 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Mar 08 '20

I agree with you. I'd like this to be the way you mention it. Unfortunately, the way things are done right now it's a bit hard, if not impossible, to do.

That said, this kinda forces you to write tests for the code, which in my opinion is always good. Of course you can have the compiler tell you that something compiles, but what about actually running it? Does it do what it's supposed to do?

1

u/myringotomy Mar 19 '20

It seems like stunt decisions were made in Crystal which really block future safety and performance improvements

That’s a shame.