Why Object is avoided as generic type

#1

Hello, I am curious why Object can’t be used as generic type in Hash, Array etc.

The exact error message is: “can’t use Object as generic type argument yet, use a more specific type” - so, here are some plans for the future about this?

I can use my own superclass as generic type in Array and then insert all subclass instances in it, so what is such special about Object? It’s like OO without Object.

If there is a good source of information about this, thank you for the link!

1 Like

#2

The way Crystal is implemented, it knows all classes at compile time and when you have a type hierarchy it’s actually similar to a union of that type and all its subtypes. That’s considered when you call a method. That for Object or Reference would mean considering all methods in all types in the program, which can be very slow. and inefficient. Also, I guess Object can be represented as a union of all objects but maybe there’s a more efficient way. Then there are already some bugs related to generics for a similar reason, with Object it will be worse.

I don’t know if this will change in the future.

1 Like

#3

Now, it looks I can deal with it, but it’s a sad story a little (no offense). Maybe you’ll find a way to solve it in the future, good luck.

0 Likes