Project

General

Profile

Actions

Feature #14967

open

Any type

Added by baweaver (Brandon Weaver) over 5 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:88303]

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0