diff --git a/.github/labeler.yml b/.github/labeler.yml index f23205d6e..3bc595497 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -19,7 +19,7 @@ 'documentation': - changed-files: - any-glob-to-any-file: - - 'doc/**/*' + - 'docs/**/*' - 'media/specs/**/*' 'ci': diff --git a/.run/spice.run.xml b/.run/spice.run.xml index b7cdd68f7..f5034be42 100644 --- a/.run/spice.run.xml +++ b/.run/spice.run.xml @@ -1,5 +1,5 @@ - + diff --git a/docs/docs/how-to/oop.md b/docs/docs/how-to/oop.md index f2b1947c2..c648e8907 100644 --- a/docs/docs/how-to/oop.md +++ b/docs/docs/how-to/oop.md @@ -47,12 +47,18 @@ type Human struct : MakeSound, Speak { unsigned int age } +p Human.ctor(string firstName, string lastName, unsigned int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; +} + p Human.makeSound() { - // Sigh ... + printf("Sigh...\n"); } -p Human.sayHello() { - // Hi! +p Human.sayHello(string name) { + printf("Hi, %s!\n", name); } type Car struct : MakeSound { @@ -61,8 +67,14 @@ type Car struct : MakeSound { unsigned int seats } +p Car.ctor(string brand, string model, unsigned int seats) { + this.brand = brand; + this.model = model; + this.seats = seats; +} + p Car.makeSound() { - // Wroom, wroom + printf("Wroom, wroom!\n"); } type Parrot struct : MakeSound, Speak { @@ -70,16 +82,35 @@ type Parrot struct : MakeSound, Speak { unsigned int age } +p Parrot.ctor(string name, unsigned int age) { + this.name = name; + this.age = age; +} + p Parrot.makeSound() { - // Sqawk! + printf("Sqawk!\n"); } -p Parrot.sayHello() { - // Hello, squawk! +p Parrot.sayHello(string name) { + printf("Hello %s, squawk!\n", name); +} + +f main() { + Human human = Human("John", "Doe", 25); + Car car = Car("Toyota", "Corolla", 5); + Parrot parrot = Parrot("Polly", 3); + + human.makeSound(); + car.makeSound(); + parrot.makeSound(); + + human.sayHello("Jane"); + parrot.sayHello("Jane"); + return 0; } ``` -As all living beings, parrots and humans have an age. So you might want to extract the `unsigned int age`, that exists +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 @@ -101,4 +132,8 @@ type Parrot struct : MakeSound, Speak { } // ... -``` \ No newline at end of file +``` + +The `compose` keyword is used to include the fields and methods of another struct into the current struct. The members of the +parent struct are accessible directly via name. e.g. `parrot.age` or through the compose member itself, e.g. +`parrot.livingBeing.age`. \ No newline at end of file diff --git a/media/test-project/test.spice b/media/test-project/test.spice index d148ac687..5a5c7e1b1 100644 --- a/media/test-project/test.spice +++ b/media/test-project/test.spice @@ -1,15 +1,77 @@ -import "std/data/hash-table"; +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.ctor(string firstName, string lastName, unsigned int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; +} + +p Human.makeSound() { + printf("Sigh...\n"); +} + +p Human.sayHello(string name) { + printf("Hi, %s!\n", name); +} + +type Car struct : MakeSound { + string brand + string model + unsigned int seats +} + +p Car.ctor(string brand, string model, unsigned int seats) { + this.brand = brand; + this.model = model; + this.seats = seats; +} + +p Car.makeSound() { + printf("Wroom, wroom!\n"); +} + +type Parrot struct : MakeSound, Speak { + string name + unsigned int age +} + +p Parrot.ctor(string name, unsigned int age) { + this.name = name; + this.age = age; +} + +p Parrot.makeSound() { + printf("Sqawk!\n"); +} + +p Parrot.sayHello(string name) { + printf("Hello %s, squawk!\n", name); +} f main() { - HashTable hashTable = HashTable(); - hashTable.upsert(1, 2); - hashTable.upsert(2, 3); + Human human = Human("John", "Doe", 25); + Car car = Car("Toyota", "Corolla", 5); + Parrot parrot = Parrot("Polly", 3); - Optional value = hashTable.get(1); - assert(value.get() == 2); - value = hashTable.get(2); - assert(value.get() == 3); + human.makeSound(); + car.makeSound(); + parrot.makeSound(); + human.sayHello("Jane"); + parrot.sayHello("Jane"); + return 0; } /*import "bootstrap/util/block-allocator";