Skip to main content
Skip to content

Lesson 9 of 12

Objects: values with names

Describe one thing with many named fields, read them with a dot, and walk through every key with chabiyan.

9 min

Making an object

Curly braces with key: value pairs make an object — AltFLang calls it a vastu ("thing"). Keys are plain names (or quoted text), values can be anything, including lists and other objects. Read a field with a dot: student.naam.

Expected output

Press Run to try it

Zoya class 10 mein haiPehla subject: Maths

Changing and adding fields

Assign to a field to change it. Assigning to a field that does not exist yet adds it. Reading a missing field gives khali. Bracket access obj["key"] works too, and is the way to go when the key is stored in a variable.

Expected output

Press Run to try it

Desi 8 Neelakhali{ brand: Desi, ram: 8, rang: Neela }

Going through every key

chabiyan(obj) ("keys") returns a list of the field names and mulya(obj) ("values") a list of the values. Loop over the keys and use bracket access to reach each value. lambai(obj) counts the fields.

Expected output

Press Run to try it

chai -> 15samosa -> 20vada_pav -> 25Sab daam: [15, 20, 25]

Practice exercise

Create rakho kitab = { naam: "Godaan", lekhak: "Premchand", pages: 312 };. Add a field padh_li set to sahi, then print Godaan by Premchand and then the number of fields (4).

Expected output

Godaan by Premchand
4
Open in playground
Show solution
solution.altf
rakho kitab = { naam: "Godaan", lekhak: "Premchand", pages: 312 };
kitab.padh_li = sahi;
bol(kitab.naam, "by", kitab.lekhak);
bol(lambai(kitab));