Skip to content

Commit

Permalink
[nim/en] add objects and ref objects (#4762)
Browse files Browse the repository at this point in the history
  • Loading branch information
janAkali authored May 18, 2024
1 parent 08c1c2e commit 1638727
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion nim.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1638727

Please sign in to comment.