lib/raap/value/interface.rb in raap-0.3.0 vs lib/raap/value/interface.rb in raap-0.4.0
- old
+ new
@@ -3,26 +3,30 @@
module RaaP
module Value
class Interface < BasicObject
def initialize(type, size: 3, self_type: nil, instance_type: nil, class_type: nil)
@type = type.is_a?(::String) ? RBS.parse_type(type) : type
+ unless @type.instance_of?(::RBS::Types::Interface)
+ ::Kernel.raise ::TypeError, "not an interface type: #{@type}"
+ end
@size = size
- definition = RBS.builder.build_interface(@type.name.absolute!)
- definition.methods.each do |name, method|
+ @definition = RBS.builder.build_interface(@type.name.absolute!)
+ @definition.methods.each do |name, method|
method_type = method.method_types.sample or Kernel.raise
- type_params = definition.type_params_decl.concat(method_type.type_params.drop(definition.type_params_decl.length))
+ type_params = @definition.type_params_decl.concat(method_type.type_params.drop(@definition.type_params_decl.length))
ts = TypeSubstitution.new(type_params, @type.args)
subed_method_type = ts.method_type_sub(method_type, self_type:, instance_type:, class_type:)
- BindCall.define_singleton_method(self, name) do |*, &b|
- @fixed_return_value ||= Type.new(subed_method_type.type.return_type).pick(size: size)
+ BindCall.define_singleton_method(self, name) do |*_, &b|
+ # @type var b: Proc?
+ @fixed_return_value ||= Type.new(subed_method_type.type.return_type).pick(size:)
if subed_method_type.block
@fixed_block_arguments ||= size.times.map do
fun_type = FunctionType.new(subed_method_type.block.type)
- fun_type.pick_arguments(size: size)
+ fun_type.pick_arguments(size:)
end
else
@fixed_block_arguments = []
end
if b
@@ -36,15 +40,19 @@
@fixed_return_value
end
end
end
+ def respond_to?(name, _include_all = false)
+ @definition.methods.has_key?(name.to_sym)
+ end
+
def class
Interface
end
def inspect
- "#<interface @type=#{@type.to_s} @methods=#{RBS.builder.build_interface(@type.name.absolute!).methods.keys} @size=#{@size}>"
+ "#<interface @type=#{@type} @methods=#{@definition.methods.keys} @size=#{@size}>"
end
end
end
end