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
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.
What would be the meaning for those symbols?
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.
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.
I respect your opinion but it seems harsh to close this without further discussion from other members of the community.
Okay,sorry.
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.
I can only see confusing behaviour. I expect a ! method to overload the unary not operator, not be some other method named !
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.
" 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.
!
and ?
symbols with no explicit meaning attached to themWhat could possibly go wrong?
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.
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) 鉂わ笍
Most helpful comment
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) 鉂わ笍