Partially do objects, remove gitbook
This commit is contained in:
parent
06c6b0a4f4
commit
64313ec3ce
6 changed files with 42 additions and 4 deletions
|
|
@ -5,7 +5,8 @@
|
||||||
* [Numbers](numbers.md)
|
* [Numbers](numbers.md)
|
||||||
* [Variables](variables/page.md)
|
* [Variables](variables/page.md)
|
||||||
* [Result](variables/result.md)
|
* [Result](variables/result.md)
|
||||||
* [Types](types.md)
|
* [Objects](objects.md)
|
||||||
|
* [Enums](enums.md)
|
||||||
* [If, Else, While, Block](if_else_while.md)
|
* [If, Else, While, Block](if_else_while.md)
|
||||||
* [Case Statements](case.md)
|
* [Case Statements](case.md)
|
||||||
* [For Loops & Iterators](for_iterators.md)
|
* [For Loops & Iterators](for_iterators.md)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
"plugins": [],
|
"plugins": [],
|
||||||
"links": {
|
"links": {
|
||||||
"home": "http://nimrod-lang.org/",
|
"home": "http://nimrod-lang.org/",
|
||||||
"about": null,
|
"about": false,
|
||||||
"issues": null,
|
"issues": null,
|
||||||
"contribute": null,
|
"contribute": null,
|
||||||
"sharing": {
|
"sharing": {
|
||||||
|
|
|
||||||
1
src/enums.md
Normal file
1
src/enums.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Enums
|
||||||
38
src/objects.md
Normal file
38
src/objects.md
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
# Objects
|
||||||
|
|
||||||
|
In Nimrod, objects are like structs from C family languages and define a grouping of fields.
|
||||||
|
|
||||||
|
``` nimrod
|
||||||
|
type
|
||||||
|
Animal* = object
|
||||||
|
name*, genus*, species*: string
|
||||||
|
age: int # None of your business!
|
||||||
|
PAnimal* = ref Animal
|
||||||
|
|
||||||
|
proc sleep*(a: var Animal) =
|
||||||
|
a.age += 1
|
||||||
|
|
||||||
|
# create a new Animal, not allocated on the heap
|
||||||
|
var carl = Animal(name : "Carl",
|
||||||
|
genus : "Lama",
|
||||||
|
species : "glama",
|
||||||
|
age : 12)
|
||||||
|
|
||||||
|
# Allocate an Animal on the heap
|
||||||
|
# Note that the pointer is immutable, not the pointee
|
||||||
|
let mittens: ref Animal = new(Animal)
|
||||||
|
mittens.name = "Mittens"
|
||||||
|
mittens.genus = "Panthera"
|
||||||
|
mittens.species = "Leo"
|
||||||
|
mittens.age = 6
|
||||||
|
|
||||||
|
# This also goes on the heap since PAnimal is a
|
||||||
|
# reference type
|
||||||
|
let spot = PAnimal(name: "Spot",
|
||||||
|
genus: "Canis",
|
||||||
|
species: "Lupus",
|
||||||
|
age: 1)
|
||||||
|
|
||||||
|
# Same as before, only pointer value is unchangeable
|
||||||
|
spot.age = 2
|
||||||
|
```
|
||||||
|
|
@ -212,7 +212,6 @@ class(Dog of Animal):
|
||||||
class(Cat of Animal) do:
|
class(Cat of Animal) do:
|
||||||
method vocalize: string = "meow"
|
method vocalize: string = "meow"
|
||||||
|
|
||||||
|
|
||||||
run_test()
|
run_test()
|
||||||
echo "Tests passed!"
|
echo "Tests passed!"
|
||||||
```
|
```
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
# Types
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue