[nim/en] add objects and ref objects (#4762)

This commit is contained in:
Archar Gelod 2024-05-18 08:26:21 +00:00 committed by GitHub
parent 08c1c2e5d8
commit 1638727b7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -61,7 +61,10 @@ var
child = (name: "Rudiger", age: 2) # Assign all at once with literal ()
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
@ -114,6 +117,21 @@ when compileBadCode:
# 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
type