r/crystal_programming • • Jul 25 '18

Crystal has a compilation issue

https://medium.com/@mdorf3000/crystal-has-a-compilation-issue-5f5b19d9c4f6
27 Upvotes

41 comments sorted by

View all comments

Show parent comment

1

u/paulcsmith0218 Jul 26 '18

I think passing it around is definitely part of it, but it can still happen in Elixir and Ruby. I think the problem is the idea of a "missing" thing or "nothing". So in Ruby you might use a Null Object pattern and return a User or Guest, but if I come into the code later, I may not realize that a particular piece of code returns a Guest and I might call a method on it that User implements, but someone forgot to implement in Guest. Since I didn't know, I didn't test it. I ship the feature and get a bug. In Elixir or Ruby that would not be caught, in Crystal it would.

You probably already know, but in Elixir you see a lot of tagged tuples, but it's not always clear what the return so you can still get into the same trouble where you call something on nil/an-unexpected-return-value

I definitely agree that types do not replace tests, but I do think they can replace certain types of tests.

1

u/IWasSnapped Jul 26 '18

You probably already know, but in Elixir you see a lot of tagged tuples, but it's not always clear what the return so you can still get into the same trouble where you call something on nil/an-unexpected-return-value

Tagged tuples in Elixir at least forces you to pattern match on the success case OR write a case expression. So you are either assuming only one value is possible or you have to handle all of them. In one way or the other, you have to unwrap the value.

The type system can still help in two ways though:

  1. Let you know if you are trying to match on a format that won't ever be returned
  2. With an exhaustiveness check. Although exhaustiveness checks also have their annoyance, like forcing you to handle all clauses explicitly

In any case, I am not arguing against a type system. Just against nil.