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

Allow defining methods called ! and ? #7959

Closed
dscottboggs opened this issue Jul 11, 2019 · 13 comments
Closed

Allow defining methods called ! and ? #7959

dscottboggs opened this issue Jul 11, 2019 · 13 comments

Comments

@dscottboggs
Copy link
Contributor

This is not a request to allow redefining the meaning of the prefix-! operator. This is a request to allow defining a method which could be called on something like something.! or something.?.

Since there is no case where someone can define a ! or ? method, it wouldn't conflict with any other language features. Redefining the meaning of the prefix-! operator is explicitly denied by the compiler (for good reason!).

This was brought up by a (seemingly) new user in the gitter chat

@dscottboggs
Copy link
Contributor Author

Also I totally recognize that this should be a low-priority issue, especially for the core team, but I think it makes sense to keep track of ideas and suggestions like this.

@asterite
Copy link
Member

What would be the meaning for those symbols?

@dscottboggs
Copy link
Contributor Author

It would depend on the context. They would be regular methods. For example, in the context of Int, Int#! could be factorial, as suggested by @artmadeit in the chat.

I would argue that it would be very nice for Object#not_nil! to be replaced/aliased by Object#! and Object#nil? to be replaced/aliased by Object#?, but I also respect the argument against unnecessary changes or aliases.

I can't think of other use cases off of the top of my head, but I don't think that a feature should be prevented just because it might not seem that useful in the typical contexts. If I hadn't seen the suggestion by @artmadeit, I would not have thought of using it for factorial. This doesn't seem like an incredibly coimplicated feature, nor does it seem like one prone to causing people to create bugs. It seems consistent with the majority of the language semantics and Crystal's Ruby roots.

@asterite
Copy link
Member

I agree that it's easy to implement. I disagree about it being useful. Theres simply no easy way to talk about these things (is it bang? exclamation mark? question?) and if the meaning is going to depend on context then it's going to be even more confusing.

I have experience with custom operators in Haskell and I think this is a road for a lot of confusion. So I'll say no to this.

@dscottboggs
Copy link
Contributor Author

I respect your opinion but it seems harsh to close this without further discussion from other members of the community.

@asterite
Copy link
Member

Okay,sorry.

@asterite asterite reopened this Jul 12, 2019
@girng
Copy link
Contributor

girng commented Jul 12, 2019

IMO, something.! and something.? looks odd in crystal. also, it might lead to confusion with other !s and ?s. i think it's important to have certain syntax protected

however, 25! would be cool to do, not gonna lie.

@ysbaddaden
Copy link
Contributor

I can only see confusing behaviour. I expect a ! method to overload the unary not operator, not be some other method named !

@oprypin
Copy link
Member

oprypin commented Jul 12, 2019

Community member passing by.
Close pls.
It's one of those things that are so absurd that one can't even explain why.

Reminds me of how people sometimes write "?" by itself in chat.

@yxhuvud
Copy link
Contributor

yxhuvud commented Jul 12, 2019

" would argue that it would be very nice for Object#not_nil! to be replaced/aliased by Object#!"

Ugh, please no. I wouldn't mind making it a bit more general by changing that to Object#not!(Type) or something similar, which would make type subtraction a bit simpler for the non-nil case. Thus changing not_nil! to not!(Nil). Though perhaps not! isn't the best name for that.

@vlazar
Copy link
Contributor

vlazar commented Jul 12, 2019

  1. use common ! and ? symbols with no explicit meaning attached to them
  2. make meaning context dependent

What could possibly go wrong?

@dscottboggs
Copy link
Contributor Author

Yeah, if no one has any good use-cases for this aside from Int#!, I have to agree.... make it Int#factorial and don't bother changing things.

@asterite
Copy link
Member

Thank you all for the feedback, but let's try to be more. respectful of other's ideas and proposals. (no mocking nor sarcasm, please) ❤️

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

No branches or pull requests

7 participants