Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_type.rb

Direct Known Subclasses

CompositeType

Defined Under Namespace

Modules: VoidType Classes: CompositeType, ConjunctiveType, ContainerType, DisjunctiveType, EnumeratedType, NegativeType

Constant Summary collapse

Void =
Class.new.extend(VoidType)

Instance Method Summary collapse

Instance Method Details

#&(t) ⇒ Object

Constructs a type which must be A AND B.

Array.of(Positive & Integer)



148
149
150
151
152
153
154
155
156
157
# File 'lib/composite_type.rb', line 148

def & t
  case
  when equal?(t)
    self
  else
    a, b = self, t
    a, b = b, a if a.to_s > b.to_s
    ConjunctiveType.new_cached(a, b)
  end
end

#of(t) ⇒ Object

Constructs a type of that matches an Enumerable with an element type.

Array.of(String)



55
56
57
# File 'lib/composite_type.rb', line 55

def of t
  ContainerType.new_cached(self, t)
end

#with(*types) ⇒ Object

Constructs a type of Enumerable elements.

String.with(Integer, Float) === [ "foo", 1, 1.2 ]
Hash.of(String.with(Integer))


83
84
85
# File 'lib/composite_type.rb', line 83

def with *types
  EnumeratedType.new_cached(self, *types)
end

#|(t) ⇒ Object

Constructs a type which can be A OR B.

Array.of(String|Integer)


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/composite_type.rb', line 111

def | t
  case
  when t <= self
    self
  when self <= t
    t
  else
    a, b = self, t
    a, b = b, a if a.to_s > b.to_s
    DisjunctiveType.new_cached(a, b)
  end
end

#~@Object

Constructs a type which must not be A.

Array.of(~ NilClass)



185
186
187
188
189
190
191
192
193
194
# File 'lib/composite_type.rb', line 185

def ~@
  case
  when x = NegativeType::INVERSE_MAP[self]
    x
  when self.is_a?(NegativeType)
    self._t.first
  else
    NegativeType.new_cached(self)
  end
end