aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-08-14 08:56:18 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit7a7db7e0b2f3782a90528f6dbdf84ecd0ce47291 (patch)
tree9a024d9b3fa7bd993462bec5a177d3314dcbc4df
parentadd vis/rgb (diff)
downloadalive-7a7db7e0b2f3782a90528f6dbdf84ecd0ce47291.tar.gz
alive-7a7db7e0b2f3782a90528f6dbdf84ecd0ce47291.zip
document and spec Type member fields
-rw-r--r--alv/type.moon30
-rw-r--r--spec/type_spec.moon17
2 files changed, 39 insertions, 8 deletions
diff --git a/alv/type.moon b/alv/type.moon
index 7aeb29a..1b47b13 100644
--- a/alv/type.moon
+++ b/alv/type.moon
@@ -22,6 +22,17 @@ same = (a, b) ->
true
+local Primitive
+
+--- Magic table containing all `Primitive` types.
+--
+-- When indexed with a string returns a (cached) instance of that type.
+--
+-- @table T
+T = setmetatable {}, __index: (key) =>
+ with type = Primitive key
+ rawset @, key, type
+
--- Base class for types.
-- @type Type
class Type
@@ -84,6 +95,9 @@ class Primitive extends Type
new: (@name) =>
assert (type @name) == 'string', "Typename has to be a string: '#{@name}'"
+ --- the type's unique name.
+ -- @tfield string name
+
--- Struct/Hashmap type.
--
-- Extends `Type`.
@@ -122,6 +136,9 @@ class Struct extends Type
-- @tparam {string=Type} types
new: (@types) =>
+ --- the shape and field types of the struct.
+ -- @tfield {string=Type} types
+
--- Array type.
--
-- Extends `Type`.
@@ -153,14 +170,11 @@ class Array extends Type
-- @tparam Type type
new: (@size, @type) =>
---- Magic table containing all `Primitive` types.
---
--- When indexed with a string returns a (cached) instance of that type.
---
--- @table T
-T = setmetatable {}, __index: (key) =>
- with type = Primitive key
- rawset @, key, type
+ --- the number of elements in this array.
+ -- @tfield number size
+
+ --- the element type of this array.
+ -- @tfield Type type
{
:Type
diff --git a/spec/type_spec.moon b/spec/type_spec.moon
index 2ffdb49..0293385 100644
--- a/spec/type_spec.moon
+++ b/spec/type_spec.moon
@@ -10,6 +10,11 @@ describe 'Primitive', ->
it 'inherits from Type', ->
assert.is.equal Type, ancestor Primitive.__class
+ it 'exposes name', ->
+ assert.is.equal 'bool', bool.name
+ assert.is.equal 'num', num.name
+ assert.is.equal 'str', str.name
+
it 'stringifies well', ->
assert.is.equal 'bool', tostring bool
assert.is.equal 'num', tostring num
@@ -48,6 +53,14 @@ describe 'Array', ->
it 'inherits from Type', ->
assert.is.equal Type, ancestor Array.__class
+ it 'exposes size', ->
+ assert.is.equal 3, vec3.size
+ assert.is.equal 2, str32.size
+
+ it 'exposes inner type', ->
+ assert.is.equal num, vec3.type
+ assert.is.equal (Array 3, str), str32.type
+
it 'stringifies well', ->
assert.is.equal 'num[3]', tostring vec3
assert.is.equal 'my-type[3][24]', tostring Array 24, Array 3, 'my-type'
@@ -93,6 +106,10 @@ describe 'Struct', ->
it 'inherits from Type', ->
assert.is.equal Type, ancestor Struct.__class
+ it 'exposes inner types', ->
+ assert.is.same { note: str, dur: num }, play.types
+ assert.is.same { c: num, b: num, a: num }, abc.types
+
it 'stringifies well', ->
assert.is.equal '{dur: num note: str}', tostring play
assert.is.equal '{a: num b: num c: num}', tostring abc