Class: Typisch::Type::Numeric

Inherits:
Constructor show all
Defined in:
lib/typisch/numeric.rb

Overview

This is aiming to be a nice numeric tower like those of Scheme etc:

Integral < Rational < Real < Complex

In these kinds of numeric tower, type and degree of precision is treated as a separate orthogonal concern; for now I’ve not treated precision at all here, although support could be added, eg to allow a distinction between

- fixed precision binary floating point (Float)
- arbitrary precision decimal floating point (BigDecimal)
- fixed size integer (Fixnum)
- arbitrary size integer (Bignum)
- etc

There are quite a few ways to classify numeric types, so I’ve stuck with just the most basic mathematical numeric tower classification for now.

Constant Summary collapse

TOWER =
[complex, real, rational, integral]

Constants inherited from Constructor

Constructor::CONSTRUCTOR_TYPE_SUBCLASSES

Instance Attribute Summary collapse

Attributes inherited from Typisch::Type

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constructor

#alternative_types, inherited, #type_lattice

Methods inherited from Typisch::Type

#<, #<=, #<=>, #==, #===, #>, #>=, #alternative_types, #annotations, #annotations=, #canonicalize!, #excluding_null, #inspect, #recursive?, #subexpression_types, subtype?, #target, #to_string

Constructor Details

#initialize(type, *valid_implementation_classes) ⇒ Numeric

Returns a new instance of Numeric.



24
25
26
27
# File 'lib/typisch/numeric.rb', line 24

def initialize(type, *valid_implementation_classes)
  @type = type
  @valid_implementation_classes = valid_implementation_classes
end

Instance Attribute Details

#valid_implementation_classesObject (readonly)

Returns the value of attribute valid_implementation_classes.



29
30
31
# File 'lib/typisch/numeric.rb', line 29

def valid_implementation_classes
  @valid_implementation_classes
end

Class Method Details

.check_subtype(x, y) ⇒ Object



58
59
60
# File 'lib/typisch/numeric.rb', line 58

def check_subtype(x, y)
  x.index_in_tower >= y.index_in_tower
end

.top_typeObject



54
55
56
# File 'lib/typisch/numeric.rb', line 54

def top_type(*)
  TOWER.first
end

Instance Method Details

#index_in_towerObject



71
72
73
# File 'lib/typisch/numeric.rb', line 71

def index_in_tower
  TOWER.index {|t| t.equal?(self)}
end

#shallow_check_type(instance) ⇒ Object Also known as: check_type



75
76
77
# File 'lib/typisch/numeric.rb', line 75

def shallow_check_type(instance)
  case instance when *@valid_implementation_classes then true else false end
end

#tagObject



67
68
69
# File 'lib/typisch/numeric.rb', line 67

def tag
  @type
end

#to_sObject



63
64
65
# File 'lib/typisch/numeric.rb', line 63

def to_s(*)
  @name.inspect
end