Type implemented
This commit is contained in:
parent
60717ed15d
commit
8becd000a2
|
|
@ -0,0 +1,34 @@
|
|||
struct Moon{}
|
||||
|
||||
struct Mars{}
|
||||
|
||||
struct Venus{}
|
||||
|
||||
type World = Mars | Moon | Venus
|
||||
|
||||
fn (m Mars) dust_storm() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
fn main(){
|
||||
mut w := World(Moon{})
|
||||
|
||||
assert w is Moon
|
||||
w = Mars{}
|
||||
|
||||
mars := w as Mars
|
||||
|
||||
if mars.dust_storm(){
|
||||
println('bad weather!')
|
||||
}
|
||||
|
||||
//w1 := World(Mars{})
|
||||
|
||||
if mut w is Mars{
|
||||
assert typeof(w).name == typeof(Mars{}).name
|
||||
|
||||
if w.dust_storm(){
|
||||
println('bad weather!')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
pub interface Reader{
|
||||
mut:
|
||||
read(mut buf []u8) ?int
|
||||
}
|
||||
|
||||
pub interface Write{
|
||||
mut:
|
||||
write(buf []u8) ?int
|
||||
}
|
||||
|
||||
pub interface ReaderWriter{
|
||||
Reader
|
||||
Write
|
||||
}
|
||||
|
||||
|
||||
struct Moon{}
|
||||
struct Mars{}
|
||||
struct Venus{}
|
||||
|
||||
type World = Mars | Moon | Venus
|
||||
|
||||
fn main(){
|
||||
|
||||
sum := World(Moon{})
|
||||
assert sum.type_name() == 'Moon'
|
||||
println(sum)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
interface Adoptable{}
|
||||
|
||||
fn (a Adoptable) speak() string {
|
||||
return 'adopt me!'
|
||||
}
|
||||
|
||||
struct Cat{}
|
||||
|
||||
fn (c Cat) speak() string{
|
||||
return 'meow!'
|
||||
}
|
||||
|
||||
struct Dog{}
|
||||
|
||||
fn main(){
|
||||
cat := Cat{}
|
||||
|
||||
assert dump(cat.speak()) == 'meow!'
|
||||
|
||||
a := Adoptable(cat)
|
||||
|
||||
assert dump(a.speak()) == 'adopt me!'
|
||||
|
||||
if a is Cat{
|
||||
dump(a.speak())
|
||||
}
|
||||
|
||||
b := Adoptable(Dog{})
|
||||
assert dump(b.speak()) == 'adopt me!'
|
||||
|
||||
//if b is Dog{
|
||||
// dump(b.speak())
|
||||
//}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
struct Empty{}
|
||||
|
||||
struct Node{
|
||||
value f64
|
||||
left Tree
|
||||
right Tree
|
||||
}
|
||||
|
||||
type Tree = Empty | Node
|
||||
|
||||
fn sum(tree Tree) f64{
|
||||
return match tree {
|
||||
Empty { 0 }
|
||||
Node { tree.value + sum(tree.left) + sum(tree.right)}
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
left := Node{0.2,Empty{}, Empty{}}
|
||||
right := Node{0.3, Empty{}, Node{0.4, Empty{},Empty{},}}
|
||||
tree := Node{0.5, left, right}
|
||||
|
||||
println(sum(tree))
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
struct Moon{}
|
||||
|
||||
struct Mars{}
|
||||
|
||||
struct Venus{}
|
||||
|
||||
type World = Mars | Moon | Venus
|
||||
|
||||
fn open_parachutes(n int){
|
||||
println(n)
|
||||
}
|
||||
|
||||
fn land(w World){
|
||||
match w {
|
||||
Moon{}
|
||||
Mars{
|
||||
open_parachutes(3)
|
||||
}
|
||||
Venus{
|
||||
open_parachutes(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn (m Moon) moon_walk(){}
|
||||
fn(m Mars) shiver(){}
|
||||
fn(v Venus) sweat(){}
|
||||
|
||||
fn pass_time(w World){
|
||||
match w{
|
||||
Moon { w.moon_walk() }
|
||||
Mars { w.shiver() }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
|
||||
mut world := World(Mars{})
|
||||
|
||||
land(world)
|
||||
pass_time(world)
|
||||
}
|
||||
Loading…
Reference in New Issue