Merge pull request #15 from viniciusfdasilva/dev
Enuns and interfaces created
This commit is contained in:
commit
f918fe3cbb
Binary file not shown.
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
|
||||
type Filter = fn (string) string
|
||||
|
||||
|
||||
fn filter(s string, f Filter) string{
|
||||
return f(s)
|
||||
}
|
||||
|
||||
fn uppercase(s string) string{
|
||||
return s.to_upper()
|
||||
}
|
||||
|
||||
|
||||
type String = string
|
||||
|
||||
|
||||
fn (s String) my_upper() string{
|
||||
return s.to_upper()
|
||||
}
|
||||
|
||||
fn main(){
|
||||
|
||||
res := filter('Teste', fn (s string) string {
|
||||
return s
|
||||
})
|
||||
|
||||
println(res)
|
||||
|
||||
println(uppercase('teste'))
|
||||
|
||||
mut str := String('oi')
|
||||
println(str)
|
||||
println(str.my_upper())
|
||||
|
||||
my_filter := Filter(uppercase)
|
||||
println(my_filter)
|
||||
}
|
||||
|
|
@ -1,36 +1,72 @@
|
|||
enum Color as u8{
|
||||
enum Color as u8 {
|
||||
@none
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
|
||||
enum Grocery{
|
||||
apple
|
||||
orange = 5
|
||||
pear
|
||||
}
|
||||
|
||||
|
||||
enum Cycle{
|
||||
one
|
||||
two
|
||||
three
|
||||
}
|
||||
|
||||
|
||||
fn (c Cycle) next() Cycle{
|
||||
|
||||
match c {
|
||||
.one {
|
||||
return .two
|
||||
}
|
||||
|
||||
.two {
|
||||
return .three
|
||||
}
|
||||
|
||||
.three {
|
||||
return .one
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main(){
|
||||
mut color := Color.red
|
||||
|
||||
color = .green
|
||||
|
||||
mut color1 := Color.@none
|
||||
|
||||
println(color)
|
||||
|
||||
match color {
|
||||
.red {
|
||||
println('the color was red')
|
||||
}
|
||||
|
||||
.green {
|
||||
println('the color was green')
|
||||
}
|
||||
|
||||
.blue {
|
||||
println('the color was blue')
|
||||
}
|
||||
|
||||
else {
|
||||
println('color was not indentified')
|
||||
}
|
||||
.red { println('The color was red') }
|
||||
.green { println('The color was green')}
|
||||
.blue { println('The color was blue ')}
|
||||
else { println('The value is none')}
|
||||
}
|
||||
|
||||
color = Color.@none
|
||||
println(color)
|
||||
|
||||
|
||||
g1 := int(Grocery.apple)
|
||||
g2 := int(Grocery.orange)
|
||||
g3 := int(Grocery.pear)
|
||||
|
||||
println('Grocery ids ${g1}, ${g2}, ${g3}')
|
||||
|
||||
|
||||
mut c := Cycle.one
|
||||
|
||||
for _ in 0..10 {
|
||||
println(c)
|
||||
c = c.next()
|
||||
}
|
||||
|
||||
println(color1)
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
type String = string
|
||||
|
||||
fn (s String) my_upper() string {
|
||||
return s.to_upper()
|
||||
}
|
||||
|
||||
fn main(){
|
||||
|
||||
mut str := String('abc')
|
||||
println(str.my_upper())
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
|
||||
struct Dog{
|
||||
breed string
|
||||
speak() string
|
||||
}
|
||||
|
||||
fn (d Dog) speak() string {
|
||||
println('Woof')
|
||||
}
|
||||
|
||||
|
||||
struct Cat{
|
||||
breed string
|
||||
speak() string
|
||||
}
|
||||
|
||||
fn (c Cat) speak() string{
|
||||
println('Meow')
|
||||
}
|
||||
|
||||
interface Something{}
|
||||
|
||||
|
||||
fn announce(s Something){
|
||||
if s is Dog {
|
||||
println('a ${s.breed] dog')
|
||||
}else if s is Cat{
|
||||
println('a cat speaks ${s.speak()}')
|
||||
}else {
|
||||
println('something else')
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
dog := Dog{'Leonberger'}
|
||||
cat := Cat{'Siamese'}
|
||||
|
||||
announce(dog)
|
||||
announce(cat)
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
|
||||
interface Foo{
|
||||
write(string) string
|
||||
}
|
||||
|
||||
interface Bar{
|
||||
mut:
|
||||
write(string) string
|
||||
}
|
||||
|
||||
struct MyStruct{}
|
||||
|
||||
fn (s MyStruct) write(a string) string{
|
||||
return a
|
||||
}
|
||||
|
||||
fn main(){
|
||||
s1 := MyStruct{}
|
||||
|
||||
fn1(s1)
|
||||
}
|
||||
|
||||
fn fn1(s Foo){
|
||||
println(s.write('Foo'))
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
struct Dog{
|
||||
breed string
|
||||
}
|
||||
|
||||
fn (d Dog) speak() string{
|
||||
return 'woof'
|
||||
}
|
||||
|
||||
struct Cat{
|
||||
breed string
|
||||
}
|
||||
|
||||
fn (c Cat) speak() string{
|
||||
return 'meow'
|
||||
}
|
||||
|
||||
interface Speaker{
|
||||
breed string
|
||||
speak() string
|
||||
}
|
||||
|
||||
fn main(){
|
||||
dog := Dog{'Leonberger'}
|
||||
cat := Cat{'Siamese'}
|
||||
|
||||
mut arr := []Speaker{}
|
||||
|
||||
arr << dog
|
||||
arr << cat
|
||||
|
||||
for item in arr{
|
||||
println('a ${item.breed} says: ${item.speak()}')
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue