Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.22 KB

intersection-types.md

File metadata and controls

60 lines (46 loc) · 1.22 KB
id title
intersection-types
Intersection Types

TODO: This page is still a fragment. Contributions welcome!

Intersection types can be useful to say, after the fact, that the input must implement two specific interfaces.

# typed: true
extend T::Sig

module I1
  def i1; end
end

module I2
  def i2; end
end

class C
  include I1
  include I2
end

class D
  include I1
  include I2
end


sig {params(x: T.all(I1, I2)).void}
def foo(x)
  x.i1
  x.i2
end

foo(C.new)
foo(D.new)

This is useful because we don't have to know ahead of time all the things that might implement these two interfaces. It also gets around the problem where we'd have to make a third interface, I12, like this:

module I12
  include I1
  include I2
end

and include this interface into C and D (because maybe we don't have control over what interfaces C and D can include).

→ View on sorbet.run