Skip to content

Commit 6cd693c

Browse files
committed
Implicit conversion/Variant
1 parent 135a8d0 commit 6cd693c

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

codes/cyclic_reference_1.mojo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# We have no issues Referece1 calling Reference2 which also calls Reference1
2+
from cyclic_reference_2 import Reference2
3+
4+
5+
struct Reference1:
6+
fn __init__(out self):
7+
Reference2.print("Reference2 inside Reference1 constructor")
8+
9+
@staticmethod
10+
fn print(s: String):
11+
print(s)
12+
13+
14+
fn main():
15+
var ref1 = Reference1()

codes/cyclic_reference_2.mojo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# We have no issues Referece2 calling Reference1 which also calls Reference2
2+
3+
from cyclic_reference_1 import Reference1
4+
5+
6+
struct Reference2:
7+
fn __init__(out self):
8+
Reference1.print("Reference1 inside Reference2 constructor")
9+
10+
@staticmethod
11+
fn print(s: String):
12+
print(s)
13+
14+
15+
fn main():
16+
var ref2 = Reference2()

codes/game_of_life.mojo

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
from gridv1 import Grid
1+
from gridv1 import Grid as GridV1
2+
from gridv2 import Grid as GridV2
3+
from utils import Variant
4+
import random
5+
6+
alias Grid = Variant[GridV1, GridV2]
27

38

49
fn run(owned grid: Grid) raises -> None:
10+
var inner: GridV1
11+
if grid.isa[GridV1]():
12+
inner = grid[GridV1]
13+
print("Received a grid of type V1")
14+
else:
15+
inner = GridV1(grid[GridV2])
16+
print("Received a grid of type V2 - converted to V1")
517
while True:
618
print("Current mutation:\n\n")
7-
print(grid)
19+
print(inner)
820
print()
921
print()
1022
if input("Enter 'q' to quit or press <Enter> to continue: ") == "q":
1123
break
12-
grid.mutate()
24+
inner.mutate()
1325

1426

1527
fn main() raises -> None:
16-
start = Grid.new(16, 16)
17-
run(start)
28+
random.seed()
29+
var grid: Grid
30+
if random.random_ui64(0, 1):
31+
v1 = GridV1.new(16, 16)
32+
grid = Grid(v1)
33+
else:
34+
v2 = GridV2.new(None, 16, 16)
35+
grid = Grid(v2)
36+
run(grid^)

codes/gridv1.mojo

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
from gridv2 import Grid as GridV2
23

34

45
# Grid is a 2D structure holding cell states (0: dead, 1: alive)
@@ -11,6 +12,19 @@ struct Grid(Stringable, Writable):
1112
fn __init__(out self, data: List[List[Int, True]]):
1213
self.data = data
1314

15+
@implicit
16+
fn __init__(out self, source: GridV2):
17+
data = source.data
18+
rows = source.rows
19+
cols = source.cols
20+
grid = List[List[Int, True]]()
21+
for row in range(rows):
22+
curr_row = List[Int, True]()
23+
for col in range(cols):
24+
curr_row.append(Int((data + (row * cols + col))[]))
25+
grid.append(curr_row)
26+
self.data = grid^
27+
1428
# Get the number of rows in the grid
1529
fn row_count(self) -> Int:
1630
if self.data:
@@ -106,6 +120,7 @@ struct Grid(Stringable, Writable):
106120
if self[row, col] == 0 and alive_neighbours == 3:
107121
self[row, col] = 1
108122

123+
109124
fn run(owned grid: Grid) raises -> None:
110125
while True:
111126
print("Current mutation:\n\n")
@@ -118,5 +133,9 @@ fn run(owned grid: Grid) raises -> None:
118133

119134

120135
fn main() raises -> None:
121-
start = Grid.new(16, 16)
122-
run(start)
136+
grid_2 = GridV2.new(42, 16, 16)
137+
#run(grid_2)
138+
print(grid_2)
139+
print("Implicit conversion\n\n")
140+
grid_1 = Grid(grid_2)
141+
print(grid_1)

codes/gridv2.mojo

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import random
22
from collections import Optional
33
from memory import UnsafePointer, memcpy, memset_zero
4-
from gridv1 import Grid as Grid1
5-
4+
from gridv1 import Grid as GridV1
65

76
struct Grid(Stringable, Writable):
87
var data: UnsafePointer[UInt8]
@@ -14,6 +13,15 @@ struct Grid(Stringable, Writable):
1413
self.cols = cols
1514
self.data = UnsafePointer[UInt8].alloc(rows * cols)
1615

16+
fn __init__(out self, source: GridV1):
17+
rows = len(source.data)
18+
cols = len(source.data[0])
19+
self = Self(rows, cols)
20+
for row in range(rows):
21+
for col in range(cols):
22+
value = UInt8(source[row, col])
23+
(self.data + row * cols + col)[] = value
24+
1725
fn __copyinit__(out self, existing: Self):
1826
self.rows = existing.rows
1927
self.cols = existing.cols
@@ -104,14 +112,9 @@ fn run(owned grid: Grid) raises -> None:
104112

105113

106114
fn main() raises -> None:
107-
grid1 = Grid.new(None, 16, 16)
108-
print("s1: \n")
109-
print(grid1)
110-
grid = Grid.new(None, 0, 0)
111-
g = grid.__moveinit__(grid1^)
112-
print("s2: \n")
113-
print(g)
114-
# run(grid)
115-
grid1 = g.__copyinit__(g)
116-
print("s3: \n")
117-
print(grid1)
115+
grid_1 = GridV1.new(16, 16)
116+
# run(grid_1)
117+
print(grid_1)
118+
print("Implicit conversion\n\n")
119+
grid_2 = Grid(grid_1)
120+
print(grid_2)

0 commit comments

Comments
 (0)