Class: Finitio::BuiltinType

Inherits:
Type
  • Object
show all
Defined in:
lib/finitio/type/builtin_type.rb

Overview

A BuiltinType generator allows capuring an information type to a type of the host language, here a Ruby class. For instance,

Int := BuiltinType(ruby.Fixnum)

The set of values captured by the information type is the same set of values that can be represented by the host type. In the example, Int captures the same set of numbers as ruby’s Fixnum.

The ruby class is used as concrete representation of the information type. In the example:

R(Int) = Fixnum

Accordingly, the dress transformation function has the following signature:

dress :: Alpha  -> Int    throws TypeError
dress :: Object -> Fixnum throws TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#name, #name=, #to_s

Constructor Details

#initialize(ruby_type, name = nil) ⇒ BuiltinType

Returns a new instance of BuiltinType.



24
25
26
27
# File 'lib/finitio/type/builtin_type.rb', line 24

def initialize(ruby_type, name = nil)
  super(name)
  @ruby_type = ruby_type
end

Instance Attribute Details

#ruby_typeObject (readonly)

Returns the value of attribute ruby_type.



28
29
30
# File 'lib/finitio/type/builtin_type.rb', line 28

def ruby_type
  @ruby_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



45
46
47
48
# File 'lib/finitio/type/builtin_type.rb', line 45

def ==(other)
  return false unless other.is_a?(BuiltinType)
  other.ruby_type==ruby_type
end

#default_nameObject



30
31
32
# File 'lib/finitio/type/builtin_type.rb', line 30

def default_name
  @ruby_type.name.to_s
end

#dress(value, handler = DressHelper.new) ⇒ Object

Check that value is a valid instance of ruby_type through ‘===` or raise an error.



40
41
42
43
# File 'lib/finitio/type/builtin_type.rb', line 40

def dress(value, handler = DressHelper.new)
  handler.failed!(self, value) unless ruby_type===value
  value
end

#hashObject



51
52
53
# File 'lib/finitio/type/builtin_type.rb', line 51

def hash
  self.class.hash ^ ruby_type.hash
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/finitio/type/builtin_type.rb', line 34

def include?(value)
  ruby_type === value
end