Skip to content

Commit

Permalink
Added tagged union examples (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
chung-leong committed Apr 10, 2024
1 parent bffa198 commit a674fd0
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/examples/tagged-union-example-1.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const std = @import("std");

pub const Number = union(enum) {
integer: i32,
big_integer: i64,
decimal: f64,
complex: std.math.complex.Complex(f64),
};

pub const a: Number = .{ .integer = 123 };
pub const b: Number = .{ .big_integer = 1234567890 };
pub const c: Number = .{ .decimal = 0.12345 };
pub const d: Number = .{ .complex = .{ .re = 1, .im = 2 } };
6 changes: 6 additions & 0 deletions docs/examples/tagged-union-example-1a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { a, b, c, d } from './tagged-union-example-1.zig';

console.log(a.integer);
console.log(b.big_integer);
console.log(c.decimal);
console.log(d.complex.valueOf());
4 changes: 4 additions & 0 deletions docs/examples/tagged-union-example-1a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
123
1234567890n
0.12345
{ re: 1, im: 2 }
6 changes: 6 additions & 0 deletions docs/examples/tagged-union-example-1b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { b, c } from './tagged-union-example-1.zig';

console.log(b.big_integer);
console.log(b.integer);

console.log(c.integer ?? c.big_integer ?? c.decimal);
3 changes: 3 additions & 0 deletions docs/examples/tagged-union-example-1b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1234567890n
null
0.12345
5 changes: 5 additions & 0 deletions docs/examples/tagged-union-example-1c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { c } from './tagged-union-example-1.zig';

for (const [ tag, value ] of c) {
console.log(`${tag} => ${value}`);
}
1 change: 1 addition & 0 deletions docs/examples/tagged-union-example-1c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
decimal => 0.12345
21 changes: 21 additions & 0 deletions docs/examples/tagged-union-example-1d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { a, b, c, d } from './tagged-union-example-1.zig';

for (const number of [a, b, c, d ]) {
for (const [ tag, value ] of number) {
switch (tag) {
case 'integer':
console.log('Do something with integer');
break
case 'big_integer':
console.log('Do something with big integer');
break;
case 'decimal':
console.log('Do something with decimal number');
break;
case 'complex':
console.log('Do something with complex number');
break;
}
}
}

4 changes: 4 additions & 0 deletions docs/examples/tagged-union-example-1d.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Do something with integer
Do something with big integer
Do something with decimal number
Do something with complex number
3 changes: 3 additions & 0 deletions docs/examples/tagged-union-example-1e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a, Number } from './tagged-union-example-1.zig';

console.log(`${Number.tag(a)}`);
1 change: 1 addition & 0 deletions docs/examples/tagged-union-example-1e.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
integer

0 comments on commit a674fd0

Please sign in to comment.