Project

General

Profile

Feature #14967

Any type

Added by baweaver (Brandon Weaver) 3 months ago. Updated 2 months ago.

Status:
Open
Priority:
Normal
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.

History

#1 [ruby-core:88326] Updated by shevegen (Robert A. Heiler) 2 months 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.

Also available in: Atom PDF