-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class: add comparison operators #5645
Class: add comparison operators #5645
Conversation
# Int32 <= String # => false | ||
# ``` | ||
def <(other : T.class) : Bool forall T | ||
# This is so that the method is expanded differently for each type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we need macro def
back ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a hack, but OK.
Maybe we can bring macro def back, and make it mandatory if you use @type inside of a macro, for clarity... I don't know... |
@asterite well this is the only use we've found that we still need |
Yes, I think class comparison should be something core of the language, so probably implemented somehow in the codegen. This is a workaround to have this functionality right now in a simple (kind of hacky way), but as you say I can't imagine other uses for macro defs that don't use |
end | ||
|
||
# :nodoc: | ||
def _lt(other : T.class) forall T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make it protected
(and all below)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, but it doesn't work. The compiler doesn't like it for some reason and I have no time/will to investigate it now.
Fixes #2060
The problem with combining types in a same hierarchy is that the compiler combines them to their parent type as a virtual type. Then when you pass it to a method and match with
other : T.class
, thatT
is the virtual type.I solve this with using
@type
inside macro code, which signals that the method must be expanded separately for each different type. Well, that solves the problem forself
, but the problem still remains forother
. That's why I invoke the opposite method inother
using a helper method and there too, I use@type
inside macro code, and actually use macro code to achieve that.It works. It could always be improved later if we find that's needed. The specs are commented because I also had to change something in the macro comparison code, so those will fail for CI... but we can uncomment those in a next version.