[ruby-core:94284] [Ruby master Feature#14967] Any type
From:
Ruby-Lang@...
Date:
2019-08-11 19:43:38 UTC
List:
ruby-core #94284
Issue #14967 has been updated by jwmittag (Jg W Mittag).
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.
----------------------------------------
Feature #14967: Any type
https://bugs.ruby-lang.org/issues/14967#change-80613
* Author: baweaver (Brandon Weaver)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
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
```ruby
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]:
```ruby
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.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>