diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-10 11:05:07 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:23:21 +0000 |
| commit | ff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1 (patch) | |
| tree | 2c63643500ba972171331aa3535a6e6ef7667622 /alv | |
| parent | test alv.type pp (diff) | |
| download | alive-ff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1.tar.gz alive-ff9bf90b87c63a0aa3a6bf2989b3835130eb8ef1.zip | |
add and test Type:eq(a,b)
Diffstat (limited to 'alv')
| -rw-r--r-- | alv/type.moon | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/alv/type.moon b/alv/type.moon index 4367bd2..c8808d4 100644 --- a/alv/type.moon +++ b/alv/type.moon @@ -29,6 +29,12 @@ class Type -- @tparam any value -- @treturn string + --- check two values of this type for equality. + -- @function eq + -- @tparam any a + -- @tparam any b + -- @treturn bool + --- create a `SigStream` of this type. -- @tparam ?any init initial value -- @treturn SigStream @@ -59,6 +65,8 @@ class Primitive extends Type else tostring value + eq: (a, b) => a == b + __eq: (other) => @name == other.name __tostring: => @name @@ -78,6 +86,12 @@ class Struct extends Type inner = table.concat ["#{k}: #{@types[k]\pp v}" for k, v in opairs value], ' ' "{#{inner}}" + eq: (a, b) => + for key, type in pairs @types + if not type\eq a[key], b[key] + return false + true + __eq: (other) => same @types, other.types __tostring: => inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ' ' @@ -105,6 +119,12 @@ class Array extends Type inner = table.concat [@type\pp v for v in *value], ' ' "[#{inner}]" + eq: (a, b) => + for i=1, @size + if not @type\eq a[i], b[i] + return false + true + __eq: (other) => @size == other.size and @type == other.type __tostring: => "#{@type}[#{@size}]" |
