Synopsis
This part of SByC implements a TypeSystem abstraction. A type is simply collection of values. A type system is to generate and parse literals and to coerce from String values.
TypeSystem::Ruby::type_of(-125) # => Fixnum lit = TypeSystem::Ruby::to_literal(-125) # => "-125" TypeSystem::Ruby::parse_literal("-125") # => -125 TypeSystem::Ruby::coerce('-125', Integer) # => -125
Semantics
- A type is defined as a collection of values
- A sub type is a subset
- Values are immutable
- A literal is any valid type system expression
- Coercing means converting a value to another type
The Ruby type system
The TypeSystem::Ruby module implements the abstraction for common ruby types. The following types are recognized:
- NilClass
- TrueClass and FalseClass
- Fixnum and Bignum
- Float
- String
- Symbol
- Class
- Module
- Regexp
User defined types (say Foo
and v.kind_of?(Foo)
) are supported provided that they provide two methods
- A class :parse method such that
Foo::parse(v.inspect) == v
- A :to_ruby_literal instance method such that
Kernel.eval(v.to_ruby_literal) == v