-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc4d12c
commit fd63e9e
Showing
44 changed files
with
3,921 additions
and
2,222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
--- | ||
title: Object-oriented programming | ||
--- | ||
|
||
Spice supports object-oriented programming in the form of composition. That means that types can be composed via | ||
structs, interfaces and methods. | ||
|
||
## Example scenario | ||
|
||
You need to model three different kinds of objects: humans, cars and parrots. Therefore, you create three structs with | ||
the respective names: | ||
|
||
```spice | ||
type Human struct { | ||
string firstName | ||
string lastName | ||
unsigned int age | ||
} | ||
type Car struct { | ||
string brand | ||
string model | ||
unsigned int seats | ||
} | ||
type Parrot struct { | ||
string name | ||
unsigned int age | ||
} | ||
``` | ||
|
||
Now, as you have a speaking parrot, you want to give the parrot and the human the ability to speak. In addition to that, | ||
all three objects can make sounds. The recommended way of doing this, is to use interfaces like this: | ||
|
||
```spice | ||
type Speak interface { | ||
p sayHello(string); | ||
} | ||
type MakeSound interface { | ||
p makeSound(); | ||
} | ||
type Human struct : MakeSound, Speak { | ||
string firstName | ||
string lastName | ||
unsigned int age | ||
} | ||
p Human.makeSound() { | ||
// Sigh ... | ||
} | ||
p Human.sayHello() { | ||
// Hi! | ||
} | ||
type Car struct : MakeSound { | ||
string brand | ||
string model | ||
unsigned int seats | ||
} | ||
p Car.makeSound() { | ||
// Wroom, wroom | ||
} | ||
type Parrot struct : MakeSound, Speak { | ||
string name | ||
unsigned int age | ||
} | ||
p Parrot.makeSound() { | ||
// Sqawk! | ||
} | ||
p Parrot.sayHello() { | ||
// Hello, squawk! | ||
} | ||
``` | ||
|
||
As all living beings, parrots and humans have an age. So you might want to extract the `unsigned int age`, that exists | ||
in both structs to a separate struct called `LivingBeing`. | ||
|
||
```spice | ||
// ... | ||
type LivingBeing struct { | ||
unsigned int age | ||
} | ||
type Human struct : MakeSound, Speak { | ||
LivingBeing livingBeing | ||
string firstName | ||
string lastName | ||
} | ||
type Parrot struct : MakeSound, Speak { | ||
LivingBeing livingBeing | ||
string name | ||
} | ||
// ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
//import "std/type/int" as unused; | ||
f<int> test(int i, int j = 12, string t = "Test") { | ||
printf("Test: %d, %s", j, t); | ||
return i; | ||
} | ||
|
||
f<int> main() { | ||
unsigned int t = 12u; | ||
printf("%d", t); | ||
test(1); | ||
test(1, 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.