Skip to content
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

Merged
merged 1 commit into from Jan 27, 2018
Merged

Class: add comparison operators #5645

merged 1 commit into from Jan 27, 2018

Conversation

asterite
Copy link
Member

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, that T 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 for self, but the problem still remains for other. That's why I invoke the opposite method in other 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.

# Int32 <= String # => false
# ```
def <(other : T.class) : Bool forall T
# This is so that the method is expanded differently for each type
Copy link
Contributor

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 ;)

Copy link
Contributor

@RX14 RX14 left a 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.

@asterite
Copy link
Member Author

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...

@RX14
Copy link
Contributor

RX14 commented Jan 27, 2018

@asterite well this is the only use we've found that we still need macro def. I'm perfectly happy with this workaround because it's super duper rare.

@asterite
Copy link
Member Author

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 {% @type %}.

@asterite asterite added this to the Next milestone Jan 27, 2018
@asterite asterite merged commit bfd7c99 into crystal-lang:master Jan 27, 2018
end

# :nodoc:
def _lt(other : T.class) forall T
Copy link
Contributor

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)?

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants