Skip to content

Commit

Permalink
Make _Callable and Procable powered by _Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Nov 28, 2024
1 parent d7f3d8d commit d72479b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
9 changes: 5 additions & 4 deletions lib/literal/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Literal::Types
autoload :AnyType, "literal/types/any_type"
autoload :ArrayType, "literal/types/array_type"
autoload :BooleanType, "literal/types/boolean_type"
autoload :CallableType, "literal/types/callable_type"
autoload :ClassType, "literal/types/class_type"
autoload :ConstraintType, "literal/types/constraint_type"
autoload :DescendantType, "literal/types/descendant_type"
Expand All @@ -20,16 +19,18 @@ module Literal::Types
autoload :NeverType, "literal/types/never_type"
autoload :NilableType, "literal/types/nilable_type"
autoload :NotType, "literal/types/not_type"
autoload :ProcableType, "literal/types/procable_type"
autoload :RangeType, "literal/types/range_type"
autoload :SetType, "literal/types/set_type"
autoload :TruthyType, "literal/types/truthy_type"
autoload :TupleType, "literal/types/tuple_type"
autoload :UnionType, "literal/types/union_type"
autoload :VoidType, "literal/types/void_type"

ProcableType = InterfaceType.new(:to_proc).freeze
CallableType = InterfaceType.new(:call).freeze

NilableBooleanType = NilableType.new(BooleanType::Instance).freeze
NilableCallableType = NilableType.new(CallableType::Instance).freeze
NilableCallableType = NilableType.new(CallableType).freeze
NilableJSONDataType = NilableType.new(JSONDataType::Instance).freeze
NilableLambdaType = NilableType.new(LambdaType).freeze
NilableProcableType = NilableType.new(ProcableType).freeze
Expand Down Expand Up @@ -67,7 +68,7 @@ def _Boolean?

# Matches if the value responds to `#call`.
def _Callable
CallableType::Instance
CallableType
end

# Nilabl version of `_Callable`
Expand Down
31 changes: 0 additions & 31 deletions lib/literal/types/callable_type.rb

This file was deleted.

28 changes: 27 additions & 1 deletion lib/literal/types/interface_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,42 @@

# @api private
class Literal::Types::InterfaceType
# TODO: We can generate this and make it much more extensive.
METHOD_TYPE_MAPPINGS = {
:call => Set[Proc, Method],
:to_proc => Set[Proc, Method],
:to_s => Set[String],
}.freeze

include Literal::Type

def initialize(*methods)
raise Literal::ArgumentError.new("_Interface type must have at least one method.") if methods.size < 1
@methods = methods
end

attr_reader :methods

def inspect = "_Interface(#{@methods.map(&:inspect).join(', ')})"
def inspect
"_Interface(#{@methods.map(&:inspect).join(', ')})"
end

def ===(value)
@methods.all? { |m| value.respond_to?(m) }
end

def >=(other)
case other
when Literal::Types::InterfaceType
@methods.all? { |m| other.methods.include?(m) }
when Module
@methods.map { |m| METHOD_TYPE_MAPPINGS[m] }.all? { |types| types&.include?(other) }
when Literal::Types::IntersectionType
other.types.any? { |type| Literal.subtype?(type, of: self) }
when Literal::Types::ConstraintType
other.object_constraints.any? { |type| Literal.subtype?(type, of: self) }
else
false
end
end
end
12 changes: 0 additions & 12 deletions lib/literal/types/procable_type.rb

This file was deleted.

9 changes: 9 additions & 0 deletions test/types.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ def expect_type_error(expected:, actual:, message:)
Expected: _Interface(:each, :map, :select)
Actual (NilClass): nil
MSG

assert _Interface(:a) >= _Interface(:a)
assert _Interface(:a) >= _Interface(:a, :b)
assert _Interface(:a, :b) >= _Interface(:a, :b)
refute _Interface(:a, :b) >= _Interface(:a)
refute _Interface(:a) >= _Interface(:b)
assert _Interface(:call) >= _Callable
assert _Interface(:to_proc) >= _Procable
refute _Interface(:to_proc, :random_method) >= Proc
end

test "_Intersection" do
Expand Down

0 comments on commit d72479b

Please sign in to comment.