Structs code file implemented
This commit is contained in:
parent
c84fde6772
commit
c93bd34a2f
|
|
@ -0,0 +1,33 @@
|
|||
struct Size{
|
||||
mut:
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
fn (s &Size) area() int{
|
||||
return s.width * s.height
|
||||
}
|
||||
|
||||
struct Button{
|
||||
Size
|
||||
title string
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut button := Button{title: 'Click me', height: 2}
|
||||
button.width = 3
|
||||
|
||||
assert button.area() == 6
|
||||
assert button.Size.area() == 6
|
||||
println(button)
|
||||
|
||||
mut button1 := Button{Size: Size{
|
||||
width : 34
|
||||
height: 10
|
||||
}}
|
||||
|
||||
println(button1)
|
||||
|
||||
button1.Size = Size{width: 2, height: 9}
|
||||
println(button1)
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
struct User{
|
||||
age int
|
||||
}
|
||||
|
||||
fn (u User) can_register() bool{
|
||||
return u.age > 16
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
|
||||
user1 := User{age: 10}
|
||||
|
||||
println(user1.can_register())
|
||||
|
||||
user2 := User{age: 20}
|
||||
|
||||
println(user2.can_register())
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import sample
|
||||
|
||||
fn main(){
|
||||
//mut into1 := sample.Information{data: 'Sample'}
|
||||
//println(into1)
|
||||
info := sample.new_information('Sample information')!
|
||||
println(info)
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
module sample
|
||||
|
||||
@[noinit]
|
||||
pub struct Information{
|
||||
pub:
|
||||
data string
|
||||
}
|
||||
|
||||
pub fn new_information(data string) !Information{
|
||||
|
||||
if data.len == 0 || data.len > 100{
|
||||
return error('data must be between 1 and 100 characters')
|
||||
}else{
|
||||
return Information{
|
||||
data: data
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue