Feature #14967
openAny type
Description
In Scala, there's the concept of an Any type which can be used to match anything.
The implementation of which is quite simple: https://github.com/baweaver/any
class Any
class << self
def ===(b)
true
end
def ==(b)
true
end
def to_proc
proc { true }
end
end
end
What this allows us though is the ability to really maximize the potentials of both Hash#===
[Feature #14869] and Array#===
[Feature #14916]:
case ['Foo', 25]
when [/^F/, Any] then true
else false
end
# => true
case {id: 1, name: 'foo', age: 42}
when {id: Any, name: /^f/, age: Any} then true
else false
end
# => true
case {id: 1, name: 'foo'}
when {id: Any, name: /^f/, age: Any} then true
else false
end
# => false
This could potentially be an alias for Object as well, as the current idea would only work with ===
. is_a?
would return false.
If we choose to pursue pattern matching [Feature #14912] further, I believe a wildcard type would be exceptionally useful.
Updated by shevegen (Robert A. Heiler) about 6 years ago
I do not have any particular opinion about "Any Type" as of yet,
neither pro or con, but just as to what Mr. Beaver wrote here:
"[...] If we choose to pursue pattern matching [Feature #14912]
further, I believe a wildcard type would be exceptionally
useful. [...]"
I believe matz already gave approval to the suggestion/idea
itself behind pattern matching for feature #14912, as can
be seen by the log made during the ~last developer meeting:
https://docs.google.com/document/d/1_cKh0LJd18y5CH1MfM6WC1fqh2rpHHZntrBCLTbwRSE/edit
But he also said that there are (many) details missing as of yet.
(And I think one incompatibility? The point mentioned by Tarui)
I suppose this may be further discussed, perhaps even at another
developer meeting in the future. It helps to know that matz
gave approval to the idea, though; I think it makes suggestions
easier in as much as to know that there may be a realistic chance
to see pattern matching be included into ruby eventually.
My only "concern" (though conern is too strong a word) is that
this can be a bit difficult to read - there is quite a lot of
information condensed in the suggestions per line, and "oldschool"
case/when structures are usually very simple to read, at the least
to me, in normal cases, while staying flexible e. g.:)
x = 'Duck typing means to use a duck for typing.'
case x
when /D.ck/
puts 'A sneaky Duck is hiding in that String.'
when String
puts 'This is a String, a so called duck-string, '\
'because it contains a duck'
when /beaver/
puts 'Those dam beavers are always building something.'
end
Perhaps I am not so used to "in" + pattern, but regexes are
also not always trivial to "decipher" - at the least for me.
Updated by jwmittag (Jörg W Mittag) about 5 years ago
baweaver (Brandon Weaver) wrote:
In Scala, there's the concept of an Any type which can be used to match anything.
This is a very odd characterization of scala.Any
. In Scala, Any
is the top type (in the type-theoretical sense), i.e. the super type of all types. In Ruby, we already have the superclass of all classes, namely ::Object
or ::BasicObject
, depending on how you look at it. (The Ruby Specification says that Object
is the top class but that implementations are allowed to add implementation-specific superclasses above it.)
Now, Ruby doesn't have types in the same sense that Scala has, so a direct comparison of a top type with a top class is strenuous at best, but I believe comparing this feature to a top type a la Scala's scala.Any
is mightily confusing. At least, it confused me when I was reading this feature request.
If what you want is a pattern wildcard, it would make more sense to explicitly call it a pattern wildcard rather than confusing it with a top type.