The Crystal Programming Language Forum

Runtime vs Compile time errors

Hello. New to Crystal and the community. Just wrote a longer intro post.

I am a long time Pharo user and reasonably new to statically compiled languages. I have some experience with Nim, but not extensively.

I am porting an application to Crystal and doing some things I have not done before. I write programs for me and not professionally. I am porting the app and learning Crystal at the same time.

While I have believed in the idea of writing tests. I have never put in the effort to do so. So, while porting my app to Crystal I have decided to attempt to learn Crystal and do things in a better manner, do things the right way.

So I started off with a file with no dependencies on any other part of the app. I have completed that port. But now I decided I am going to write the spec for the this file. All is going fine. I am getting educated.

But then I write a test for next method in the file and I get a runtime error that I do not know why it is runtime and not compile time.

  # Returns the {Sunday 21:00UTC. Friday 21:59.999UTC} of the last complete trading week.
  def last_trading_weekends()
    weekspan = 7)
    open_sunday, close_friday = trading_weekends(Time.utc)
    if open_sunday <= dt <= close_friday
      open_sunday -= weekspan
      close_friday -= weekspan
    {open_sunday, close_friday}

$ crystal spec spec/ 
Showing last frame. Use --error-trace for full trace.

In src/core/

 39 | if open_sunday <= dt <= close_friday
Error: undefined local variable or method 'dt' for top-level

The error is simple. I copied and pasted code and didn’t make the adjustments of a variable now longer being passed in.

I was just curious as to why this isn’t caught in the editor or the compiler. I thought this was one of the advantages of a statically compiled langauge. In Pharo I would have been able to save the method with this error. I suspect that it should be caught but for some reason it is not.

I am using Codium (VS Code) on MXLinux. The experience is currently primitive but doable.



This is a compile time error. When you run crystal spec or crystal run or crystal it still needs to compile a binary, just within a tmp dir. I.e. the spec never runs as compilation fails before that can happen.

1 Like
crystal build src/

Gives no errors and builds a binary that currently doesn’t do anything.

Also, the spec has been run several times for other methods. It was just when this method was called that the error occured.


Try crystal build spec/ Crystal doesn’t compile (and therefore doesn’t catch compile-time errors for) methods that aren’t used in the binary.

This is also why testing is so important. Otherwise if your program never uses a method, it gets excluded from the resulting binary. Then if someone were to go and use it for the first time it could break, like you saw here. Even if you’re not testing much of the business logic, having test coverage at least ensures things will still compile.



Thanks both of you for your patience in explaining this to this Crystal newbie.

That is exactly why I wanted to go through my port in a manner using best practices and learning Crystal properly. I wanted to get my education done right. :slight_smile:

Thanks again.


No problem. If it would be easier for you, could also join one of the chat channels listed in Community - The Crystal Programming Language. Such as Discord or gitter.

Thanks. Joined the gitter channel.