Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -9,7 +9,7 @@ model # instance context
| Every value passed to the constructor sets the instance variable
| according to the order.
|^
fun makeNoise = void by block # method of Bear
fun makeNoise void by block # method of Bear
writeLine("Growl!")
end
end
@ -17,14 +17,14 @@ type Cat
model
text noise
new by text noise # an explicit constructor
me.noise = noise # we must use me to access instance variables
me.noise noise # we must use me to access instance variables
end
fun makeNoise = void by block
fun makeNoise void by block
writeLine(me.noise)
end
end
type Main
Bear bear = Bear("Bruno") # creating a new instance
writeLine("The bear is called " + bear.name)
Bear bear Bear("Bruno") # creating a new instance
writeLine("The bear is called ", bear.name)
bear.makeNoise()
Cat("Meow").makeNoise()

View file

@ -0,0 +1,21 @@
// In this example, the can_register method has a receiver of type 'User' named 'u'.
struct User {
age int
}
// 'can_register' is a method of the 'User' struct above
fn (u User) can_register() bool {
return u.age > 16
}
user := User{
age: 10
}
println(user.can_register()) // "false"
user2 := User{
age: 20
}
println(user2.can_register()) // "true"

View file

@ -0,0 +1,26 @@
// Embedded structs
struct Size {
mut:
width int
height int = 2
}
fn (s &Size) area() int {
return s.width * s.height
}
// The 'Size' struct is embedded into the 'Button' struct
struct Button {
Size
title string = "Click me"
}
// With embedding, the struct 'Button' will get all the fields and methods from struct 'Size'
mut button := Button{}
button.width = 3
println("${button.Size.area()}") // 'Size' struct methods can be used by the 'Button' struct
println("${button.area()}") // You can use the method names directly

View file

@ -0,0 +1,7 @@
struct User {}
fn User.new() User {
return User{}
}
user := User.new()

View file

@ -0,0 +1,35 @@
// 'pub'
pub struct Public {
pub:
data string
}
struct Private {
data string
}
pub fn public_function() {}
fn private_function() {}
// '@[noinit]'
module sample
@[noinit]
pub struct Information {
pub:
data string
}
// factory function
pub fn new_information(data string) !Information {
if data.len == 0 || data.len > 100 {
return error("data must be between 1 and 100 characters")
}
return Information{
data: data
}
}