-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added const generics example to tests
- Loading branch information
1 parent
636d48f
commit 48f27fa
Showing
4 changed files
with
102 additions
and
31 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,66 @@ | ||
fun main() { | ||
// Create an array struct | ||
let mut a = Array.new<Int, 5>([1, 2, 3, 4, 5]); | ||
// Print it out | ||
a.println(); | ||
// Set a value! | ||
a.set(1, 1000); | ||
// Print the changed value | ||
println(*a.get(1)); | ||
// Print the whole changed array | ||
a.println(); | ||
println(a); | ||
a[0] = 555; | ||
a.println(); | ||
} | ||
|
||
// An array with a constant parameter length | ||
type Array<T, const N: Int> = [T * N]; | ||
|
||
impl Array<T, N> { | ||
// Create a new Array container | ||
fun new(arr: [T * N]): Array<T, N> { | ||
return arr; | ||
} | ||
|
||
// Get a value from the array | ||
fun get(&self, n: Int): &T { | ||
if n < N { | ||
return &self.data()[n]; | ||
} else { | ||
return Null; | ||
} | ||
} | ||
|
||
fun data_mut(&mut self): &mut T { | ||
return self as &mut T; | ||
} | ||
|
||
fun data(&self): &T { | ||
return self as &T; | ||
} | ||
|
||
// Set a value in the array | ||
fun set(&mut self, n: Int, val: T) { | ||
self.data_mut()[n] = val; | ||
} | ||
|
||
// Print the array | ||
fun print(&self) { | ||
print("["); | ||
for let mut i=0; i<N; i+=1; { | ||
print(self.data()[i]); | ||
if i != N - 1 { | ||
print(", ") | ||
} | ||
} | ||
print("]"); | ||
} | ||
|
||
fun println(&self) { | ||
self.print(); | ||
println(); | ||
} | ||
} | ||
|
||
main(); |
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
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 @@ | ||
[1, 2, 3, 4, 5] | ||
1000 | ||
[1, 1000, 3, 4, 5] | ||
[1, 1000, 3, 4, 5] | ||
[555, 1000, 3, 4, 5] |
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
............................................ .......... | ||
............................................ ......... | ||
............................................. .......... | ||
...................................... .. # ....... | ||
................................ .. .. | ||
.................................+ | ||
............................... | ||
............................* | ||
............................ | ||
........................... | ||
......... ... . ....... | ||
......... .... | ||
.......# .. | ||
......# . | ||
.. | ||
.. | ||
......# . | ||
.......# .. | ||
......... .... | ||
......... ... . ....... | ||
........................... | ||
............................ | ||
............................* | ||
............................... | ||
.................................+ | ||
................................ .. .. | ||
...................................... .. # ....... | ||
............................................. .......... | ||
............................................ ......... | ||
............................................ .......... | ||
...........................................* **........ | ||
...........................................* **....... | ||
............................................+ *......... | ||
...............................*...... *. ++.+... | ||
................................ .* +. | ||
................................* | ||
..........................**... | ||
............................# | ||
..........................** | ||
........**.....*.........*. | ||
........+ .+* * ......* | ||
........* *... | ||
....... .. | ||
.*.*.. * | ||
** | ||
** | ||
.*.*.. * | ||
....... .. | ||
........* *... | ||
........+ .+* * ......* | ||
........**.....*.........*. | ||
..........................** | ||
............................# | ||
..........................**... | ||
................................* | ||
................................ .* +. | ||
...............................*...... *. ++.+... | ||
............................................+ *......... | ||
...........................................* **....... | ||
...........................................* **........ |