The Crystal Programming Language Forum

Feedback on macro "Type < OtherType" operators

As a note I saw this in the stdlib:

src/slice.cr
86:    {% unless T <= Int::Primitive || T <= Float::Primitive %}
...

Which is cool and clever. And makes sense once you know what’s going on I would also like to point out that it’s pretty hard to understand to beginners. T is a value less than Int::Primitive? Does that mean Int::Primitive is value “5” and T is something less than that? T is also less than Float::Primitive? Wait, do those overlap? What the…?

Suggestion: replace those operators with just “is_a?” type operators (more…explicit). Just a feedback…

Cheers!

The problem is, T is in general an ASTNode, not necessarily a TypeNode. And we have is_a?, but it’s used to determine whether you are dealing with a particular kind of node. For example T.is_a?(Path), T.is_a?(TypeNode), etc.

We are not going to change that.

Also, comparing types with < and > works in Ruby too, and that’s where we copied it from.

Also, notice this:

class Bar < Foo

Yeah I remembered the class A < B syntax after a bit of poking. Might be nice to consider something to help it be more understandable to beginners (at some point)…I don’t know if Ruby’s example is good in this particular example… Cheers!

For followers, appears I can already use {{if Int::Primitive.union_types.includes?(T) ...}} if I want to be more explicit so that might satisfy half of the initial request at least, thanks for the feedback.