Class: Types::Lambda

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/lambda.rb

Overview

Represents a lambda (function) type with argument and return types.

“‘ruby type = Types::Lambda(Types::String, Types::Integer, returns: Types::String) type.to_s # => “Lambda(String, Integer, returns: String)” “`

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generic

#to_rbs, #|

Constructor Details

#initialize(argument_types, return_type) ⇒ Lambda

Returns a new instance of Lambda.



20
21
22
23
# File 'lib/types/lambda.rb', line 20

def initialize(argument_types, return_type)
	@argument_types = argument_types
	@return_type = return_type
end

Instance Attribute Details

#argument_typesObject (readonly)

Returns the value of attribute argument_types.



26
27
28
# File 'lib/types/lambda.rb', line 26

def argument_types
  @argument_types
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



28
29
30
# File 'lib/types/lambda.rb', line 28

def return_type
  @return_type
end

Instance Method Details

#composite?Boolean

Returns:



31
32
33
# File 'lib/types/lambda.rb', line 31

def composite?
	true
end

#parse(input) ⇒ Object

Parses the input as a lambda/proc.



48
49
50
51
52
53
54
55
56
57
# File 'lib/types/lambda.rb', line 48

def parse(input)
	case input
	when ::String
		eval("lambda{#{input}}", TOPLEVEL_BINDING)
	when ::Proc
		input
	else
		raise ArgumentError, "Cannot coerce #{input.inpsect} into Lambda!"
	end
end

#to_sObject



36
37
38
39
40
41
42
# File 'lib/types/lambda.rb', line 36

def to_s
	if @return_type
		"Lambda(#{@argument_types.join(', ')}, returns: #{@return_type})"
	else
		"Lambda(#{@argument_types.join(', ')})"
	end
end