mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 12:32:09 +03:00
[nim/en] add objects and ref objects (#4762)
This commit is contained in:
parent
08c1c2e5d8
commit
1638727b7b
@ -61,7 +61,10 @@ var
|
|||||||
|
|
||||||
child = (name: "Rudiger", age: 2) # Assign all at once with literal ()
|
child = (name: "Rudiger", age: 2) # Assign all at once with literal ()
|
||||||
today.sun = "Overcast" # or individual fields.
|
today.sun = "Overcast" # or individual fields.
|
||||||
today.temp = 70.1
|
today[1] = 70.1 # or by index.
|
||||||
|
|
||||||
|
let impostor = ("Rudiger", 2) # Two tuples are the same as long as they have
|
||||||
|
assert child == impostor # the same type and the same contents
|
||||||
|
|
||||||
# Sequences
|
# Sequences
|
||||||
|
|
||||||
@ -114,6 +117,21 @@ when compileBadCode:
|
|||||||
# More Types and Data Structures
|
# More Types and Data Structures
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# Objects are similar to tuples, but they *require* names of the fields
|
||||||
|
|
||||||
|
type
|
||||||
|
Room = ref object # reference to an object, useful for big objects or
|
||||||
|
windows: int # objects inside objects
|
||||||
|
doors: int = 1 # Change the default value of a field (since Nim 2.0)
|
||||||
|
House = object
|
||||||
|
address: string
|
||||||
|
rooms: seq[Room]
|
||||||
|
|
||||||
|
var
|
||||||
|
defaultHouse = House() # initialize with default values
|
||||||
|
defaultRoom = new Room() # create new instance of ref object
|
||||||
|
sesameHouse = House(address: "123 Sesame St.", rooms: @[defaultRoom])
|
||||||
|
|
||||||
# Enumerations allow a type to have one of a limited number of values
|
# Enumerations allow a type to have one of a limited number of values
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Loading…
Reference in New Issue
Block a user