summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArchar Gelod <60652075+Archargelod@users.noreply.github.com>2024-05-18 08:26:21 +0000
committerGitHub <noreply@github.com>2024-05-18 02:26:21 -0600
commit1638727b7b1a72e357f4046fd8903de6eec4cfcf (patch)
treec367ee52ec32e99b44c721422baf0dd61e9915b0
parent08c1c2e5d87b87a2425fed74790f9ca1eccda360 (diff)
[nim/en] add objects and ref objects (#4762)
-rw-r--r--nim.html.markdown20
1 files changed, 19 insertions, 1 deletions
diff --git a/nim.html.markdown b/nim.html.markdown
index e3c749dc..0259dd45 100644
--- a/nim.html.markdown
+++ b/nim.html.markdown
@@ -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