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

Object#in? #8671

Closed
jlcrochet opened this issue Jan 10, 2020 · 13 comments · Fixed by #8720
Closed

Object#in? #8671

jlcrochet opened this issue Jan 10, 2020 · 13 comments · Fixed by #8720

Comments

@jlcrochet
Copy link

Maybe this is redundant, but I have always thought that a Python-ish obj.in?(enum) would be more intuitive than the Ruby-ish enum.includes?(obj):

class Object
  def in?(e : Enumerable) : Bool
    e.includes?(self)
  end
end

1.in? 1..10  #=> true
[1, 2].in? [[1, 2], [3, 4]]  #=> true
@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Jan 10, 2020

See https://github.com/crystal-lang/crystal/wiki/FAQ#why-are-aliases-discouraged.

EDIT: Sorry, it's not actually an alias.

@jkthorne
Copy link
Contributor

I don't know about adding something like this to the root object but it would be nice to have in?

@jlcrochet
Copy link
Author

@Blacksmoke16 Yeah, it's true that it isn't technically an alias, but I think the principle is the same: it merely provides an alternative to something the language already supports. Probably not a good idea.

@RX14
Copy link
Contributor

RX14 commented Jan 10, 2020

I actually don't mind this, I've written if {a, b, c}.includes? x a bunch of times to avoid a case with a single when, and if x.in? {a, b, c} is nicer.

I'm pretty ambivalent, if others in the core team are open im happy to accept a PR.

@jlcrochet
Copy link
Author

@RX14 Sorry, should I go ahead and reopen this? Not sure what the proper etiquette is.

@RX14
Copy link
Contributor

RX14 commented Jan 11, 2020

@Marzipanzerfaust usually it ends up better to leave it a few days to make sure everyone's had their say before closing.

@RX14 RX14 reopened this Jan 11, 2020
@straight-shoota
Copy link
Member

@RX14 When used with a fixed set of values, x.in?(a, b, c) could also work. So this would really be a nice way to write disjunctive comparisons with a single reference than x == a || x == b || x == c.

@bew
Copy link
Contributor

bew commented Jan 11, 2020

I actually don't mind this, I've written if {a, b, c}.includes? x a bunch of times to avoid a case with a single when, and if x.in? {a, b, c} is nicer.

Note that you'll need parentheses because the parser will think it's a block

@j8r
Copy link
Contributor

j8r commented Jan 11, 2020

Is the only advantage to write an expression the other way around?

Having two redundant methods will produce different styles: those who prefer includes?, others prefering in?, and code with both mixed.'

I thought adding methods to Object was something to avoid.

There is also Enumerable#any?, which is quite powerful.

@RX14
Copy link
Contributor

RX14 commented Jan 11, 2020

@straight-shoota yeah, a

def in?(*values)
  self.in? values
end

would be neat

@RX14
Copy link
Contributor

RX14 commented Jan 11, 2020

Aliases on the same object (different names for the same method) are similar but different to shortcuts on other objects.

This is on the borderline of breaking the no-aliases rule, which is why i'm open to the rest of the core team's input instead of rejecting it.

@jlcrochet
Copy link
Author

My reasoning for this was similar to the existence of String#match versus Regex#match: one is simply the reverse of the other but is slightly more convenient in certain situations, especially in method chains.

@j8r
Copy link
Contributor

j8r commented Jan 11, 2020

In any case, the Enumerable annotation must not be put in Object, because it will make it depend on the Enumerable module (object is before enumerable in prelude)

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

Successfully merging a pull request may close this issue.

8 participants