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

Constant Summary

Constants included from Metadata

Metadata::EMPTY_METADATA

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#anonymous?, #name, #name=, #named?, #to_s

Methods included from Metadata

#metadata, #metadata=, #metadata?

Constructor Details

#initialize(ruby_type, name = nil, metadata = 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,  = nil)
  super(name, )
  @ruby_type = ruby_type
end

Instance Attribute Details

#ruby_typeObject (readonly) Also known as: representator

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?



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

def ==(other)
  super || (other.is_a?(BuiltinType) && other.ruby_type==ruby_type)
end

#default_nameObject



32
33
34
# File 'lib/finitio/type/builtin_type.rb', line 32

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.



42
43
44
45
# File 'lib/finitio/type/builtin_type.rb', line 42

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

#hashObject



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

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

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/finitio/type/builtin_type.rb', line 36

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