The Crystal Programming Language Forum

Is String? any special?

I’m doing some type dispatching based on *.class overloading. I handle all nullable types in a generic function, so that I don’t have to repeat myself. This seems to work fine both for built-in types and user-defined classes, but not with String?. Take a look at the following example:

def x(t : Int32.class);  puts t;  end
def x(t : String.class); puts t; end
def x(t : (T | Nil).class) forall T; puts t; end

x(Int32) # => Int32
x(Int32?) # => (Int32 | Nil)
x(String) # => String

# Uncommenting the following line yields compile time error:

The error is:

no overload matches ‘x’ with type (String | Nil).class
Overloads are:

  • x(t : Int32.class)
  • x(t : String.class)
  • x(t : T | Nil.class)

It’s a bug. Happens if the class is named String2 too. I think T?.class doesn’t work well in the sense that it doesn’t get inserted in the right order. Please report it as a bug. Thank you!