Merge pull request #16 from viniciusfdasilva/dev

Dev
This commit is contained in:
Vinicius Silva 2024-01-23 22:38:53 -03:00 committed by GitHub
commit aa3cab342b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 207 additions and 6 deletions

View File

@ -2,21 +2,19 @@
struct Dog{
breed string
speak() string
}
fn (d Dog) speak() string {
println('Woof')
fn (d Dog) speak() string{
return 'Woof'
}
struct Cat{
breed string
speak() string
}
fn (c Cat) speak() string{
println('Meow')
return 'Meow'
}
interface Something{}
@ -24,7 +22,7 @@ interface Something{}
fn announce(s Something){
if s is Dog {
println('a ${s.breed] dog')
println('a ${s.breed} dog')
}else if s is Cat{
println('a cat speaks ${s.speak()}')
}else {

View File

@ -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!')
}
}
}

View File

@ -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)
}

View File

@ -0,0 +1,36 @@
interface IFoo{
foo()
}
interface IBar{
bar()
}
struct SFoo{}
fn (sf SFoo) foo() {}
struct SFooBar {}
fn (sfb SFooBar) foo() {}
fn (sfb SFooBar) bar() {
dump('This implements IBar')
}
fn main(){
mut arr := []IFoo{}
arr << SFoo{}
arr << SFooBar{}
for a in arr{
dump(a)
if a is IBar{
a.bar()
}
}
}

View File

@ -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())
//}
}

View File

@ -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))
}

View File

@ -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)
}

View File

@ -1 +1,3 @@
### Codes of my learning process in V Programming Language
[V Documentation](https://github.com/vlang/v/blob/master/doc/docs.md)