Reorganize files
This commit is contained in:
parent
753a1f286d
commit
da59ba56ce
4 changed files with 5 additions and 3 deletions
63
content/types/enums.md
Normal file
63
content/types/enums.md
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
---
|
||||
title: Enums
|
||||
---
|
||||
# Enums
|
||||
Enums in Nimrod are like enums in C, but are type-checked. There are no anonymous enums in Nimrod.
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
CompassDirections = enum
|
||||
cdNorth, cdEast, cdSouth, cdWest
|
||||
|
||||
Colors {.pure.} = enum
|
||||
Red = "FF0000", Green = (1, "00FF00"), Blue = "0000FF"
|
||||
|
||||
Signals = enum
|
||||
sigQuit = 3, sigAbort = 6, sigKill = 9
|
||||
```
|
||||
Notice that each element in `CompassDirections` is prepended with `cd` to avoid name conflicts since references to the enum value do not need to be qualified. The `{.pure.}` pragma that `Colors` has requires that all references to `Colors`'s values be qualified, therefore making a prefix unnecessary.
|
||||
|
||||
Enums can be given custom values and stringify values, as shown by `Colors` and `Signals`.
|
||||
|
||||
While enums can also have disjoint values, it should not be used for any other reason than compatibility with C because it breaks the idea that enums are ordinal.
|
||||
|
||||
``` nimrod
|
||||
for direction in ord(low(CompassDirections))..
|
||||
ord(high(CompassDirections)):
|
||||
echo CompassDirections(direction), " ord: ", direction
|
||||
|
||||
var ordinal = low(int)
|
||||
inc ordinal
|
||||
dec ordinal
|
||||
echo high(char)
|
||||
```
|
||||
```console
|
||||
$ nimrod c -r enums.nim
|
||||
cdNorth ord: 0
|
||||
cdEast ord: 1
|
||||
cdSouth ord: 2
|
||||
cdWest ord: 3
|
||||
3
|
||||
<EFBFBD>
|
||||
```
|
||||
|
||||
Because enums are ordinals, they have the `low`, `high`, `inc`, `dec`, and `ord` methods defined, where
|
||||
|
||||
- `low` gives the lowest possible value
|
||||
- `high` give the highest possible value
|
||||
- `inc` increments
|
||||
- `dec` decrements
|
||||
- `ord` gives the integer value of the enum
|
||||
- `CompassDirections` is a cast that gives an enum from an integer
|
||||
|
||||
It is also possible to iterate through all possible values of ordinal enums, either as shown above, or `cdNorth..cdWest`, which is equivalent.
|
||||
|
||||
|
||||
``` nimrod
|
||||
when false:
|
||||
var nonOrdinal = sigQuit
|
||||
inc nonOrdinal
|
||||
dec nonOrdinal
|
||||
```
|
||||
|
||||
`Signals` is not an ordinal type, and so doesn't have the `inc` and `dec` procedures.
|
||||
70
content/types/objects.md
Normal file
70
content/types/objects.md
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
---
|
||||
title: Objects
|
||||
---
|
||||
# Objects
|
||||
|
||||
In Nimrod, objects are like structs from C family languages and define a grouping of fields. They are by default traced by the garbage collector, so there is no need to explicitly free them when allocated.
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
Animal* = object
|
||||
name*, species*: string
|
||||
age: int
|
||||
|
||||
proc sleep*(a: var Animal) =
|
||||
a.age += 1
|
||||
|
||||
proc dead*(a: Animal): bool =
|
||||
result = a.age > 20
|
||||
|
||||
var carl: Animal
|
||||
carl = Animal(name : "Carl",
|
||||
species : "L. glama",
|
||||
age : 12)
|
||||
|
||||
let joe = Animal(name : "Joe",
|
||||
species : "H. sapiens",
|
||||
age : 23)
|
||||
|
||||
assert not carl.dead
|
||||
for i in 0..10:
|
||||
carl.sleep()
|
||||
assert carl.dead
|
||||
```
|
||||
|
||||
Object types are declared in a type section, as usual. They can be exported, and individual fields can also be exported. Fields can be safely exported without violating encapsulation because call syntax is equivalent between them.
|
||||
|
||||
Initially, `carl` is created on the stack and initialized to zeros, so its value is `[name = nil, species = nil, age = 0]`. It is mutable, so that means that the contents of `carl` can be changed. This also means it can be passed to functions that require a variable parameter, like `sleep()`, which can modify its value.
|
||||
|
||||
`joe` is also created on the stack, but it's contents are immutable and can not be changed. Attempting to do so, say through `joe.age = 57`, will fail with an error at compile time.
|
||||
|
||||
``` nimrod
|
||||
let mittens: ref Animal = new(Animal)
|
||||
|
||||
mittens.name = "Mittens"
|
||||
mittens.species = "P. leo"
|
||||
mittens.age = 6
|
||||
|
||||
mittens.sleep()
|
||||
```
|
||||
|
||||
`mittens` is a reference to an object allocated on the heap. The value of `mittens` cannot be changed, so `mittens` can never point to anything else, but the value that `mittens` is pointing at can and is changed from the default initialization value of zeros. It's mutability also means that `mittens` can be passed to functions that require a variable parameter.
|
||||
|
||||
You might ask whether there is a more concise way of initializing reference types, and there is if you give the reference type a name:
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
PAnimal* = ref Animal
|
||||
|
||||
let spot = PAnimal(name: "Spot",
|
||||
species: "C. lupus",
|
||||
age: 1)
|
||||
```
|
||||
|
||||
In many cases it is only wanted to have the object be a reference type, which is possible by declaring it as a `ref object`.
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
Thing* = ref object
|
||||
positionX*, positionY*: int
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue