-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bffa198
commit a674fd0
Showing
11 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
123 | ||
1234567890n | ||
0.12345 | ||
{ re: 1, im: 2 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1234567890n | ||
null | ||
0.12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
decimal => 0.12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
integer |