diff options
Diffstat (limited to 'alv')
| -rw-r--r-- | alv/type.moon | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/alv/type.moon b/alv/type.moon index ecd1066..408edff 100644 --- a/alv/type.moon +++ b/alv/type.moon @@ -4,6 +4,7 @@ -- @module type import opairs from require 'alv.util' import result from require 'alv.cycle' +import Error from require 'alv.error' shared_shape = (a, b) -> for key in pairs a @@ -35,6 +36,11 @@ class Type -- @tparam any b -- @treturn bool + --- index into this type or throw a error. + -- @function get + -- @tparam any key + -- @treturn Type + --- create a `SigStream` of this type. -- @tparam ?any init initial value -- @treturn SigStream @@ -67,6 +73,8 @@ class Primitive extends Type eq: (a, b) => a == b + get: (key) => error "cannot index into Primitive type '#{@}'" + __eq: (other) => @name == other.name __tostring: => @name @@ -93,10 +101,8 @@ class Struct extends Type return false true - __eq: (other) => same @types, other.types - __tostring: => - inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ' ' - "{#{inner}}" + get: (key) => + assert @types[key], Error 'index', "#{@} has no '#{key}' key" --- create a new struct type with a subset of keys. project: (keys) => @@ -105,6 +111,11 @@ class Struct extends Type types[key] = @types[key] @@ types + __eq: (other) => same @types, other.types + __tostring: => + inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ' ' + "{#{inner}}" + --- instantiate a Primitive type. -- @classmethod -- @tparam {string=Type} types @@ -127,6 +138,11 @@ class Array extends Type return false true + get: (key) => + assert (type key) == 'number' + assert key >= 0 and key < @size, Error 'index', "index '#{key}' out of range!" + @type + __eq: (other) => @size == other.size and @type == other.type __tostring: => "#{@type}[#{@size}]" |
