Crystal: Error messages should show Type|Nil instead of Type?

Created on 31 Oct 2015  路  8Comments  路  Source: crystal-lang/crystal

I think that the more verbose Type|Nil syntax is better for error messages than the easilly misread Type?. Retain Type? as a shortcut in code, but I think Type|Nil is much clearer and visible in error messages.

accepted compiler

Most helpful comment

Let's do this. My argument for docs isn't a good one, it's actually better to show Int32 | Nil because then one can click on Nil too. In fact, I'll change all to_s methods to show the full type, like showing Pointer(Int32) instead of Int32* and so on.

All 8 comments

I like your idea. Sugar for expressing is good. When reporting a less ambiguous syntax makes more sense. +1

Got this for triage.

Not sure about this change. Type? is used in code, so if it appears in errors you should know what it means. It also appears in docs and pretty much everywhere. Should we also show Foo | Nil in docs? Maybe it's too verbose, and nilable types are very common.

This also applies to methods returning a tuple. I think it's easier to read this, in docs (and code too):

def foo : {Int32, Int32}
end

than this:

def foo : Tuple(Int32, Int32)
end

So unless others have a very strong vote for this, I'd close this.

+1 for verbose reporting.

The point is that Type? is easily misread as Type in error messages, where Type | Nil is not. I'm not sure the same applies for docs.

In code, I don't think it's as hard to misread somehow...

The Type? idiom is very common in the language, either in code and documentation. I'm in favor to keep it for error reporting too.

Error output is a completely different context.

  • The code isn't highlighted.
  • The code-structure is broken up
  • There's no formatting like in docs, making distinctions
  • There are English language phrases surrounding the error where the question mark is used with English semantics.

Code + Docs => Type? is Neat, Errors => Type|Nil is better. Because it can be confusing with Type? (punctual pun intended)

Let's do this. My argument for docs isn't a good one, it's actually better to show Int32 | Nil because then one can click on Nil too. In fact, I'll change all to_s methods to show the full type, like showing Pointer(Int32) instead of Int32* and so on.

and the ? can be passed un-noticed very easily

Was this page helpful?
0 / 5 - 0 ratings