Question about method signature

I’m checking the method construction, and I was wondering what’s part of the method signature. How the compiler checks for the overload? I know, modern languages leave out the return type from the signature, does it the case here ?

Second question is, how the compiler generates the methods with Union type parameter. Is it similar to C++ template system, generate every possible method signature, or more like Java and other langues generic approach?

Part of the method signature is the type restrictions (or the lack of them), splats, named arguments and whether it yields to a block or captures a block. All methods in Crystal are like C++ templates in that they are instantiated when used. Union types restrictions are just restrictions. If you pass a union type it’s passed as is unless there’s a restriction of a type inside that union: in that case there’s a runtime dispatch that extracts the value from the union type.

Overloads are checked based on which types are more strict than other. So if you have a method with a single argument x and no type restriction, and another one with x : Int32, this second one is more strict and will be selected when passing an integer. Then different arity usually means non-compatible overloads (if you pass two arguments only the methods that accept two arguments (or more) are matched). But we are not super sure yet how all the methods should be sorted because there are just too many variables (splats, named arguments, default values, type restrictions, forall, etc.). So the current implementation is the best heuristic we could find until now.

I hope this answers most (all?) your questions :slight_smile:

2 Likes

Yes, it clears up most of my question about the methods, thanks :)