Skip to content
Kavachकवच

Sets: Making a set

● Beginner2 min

Set.of(…) makes a set; add and remove change it, so it must be a let.

fn main() {
    let seen = Set.of(2, 7)
    seen.add(2)
    print(seen.len())
    print(seen.contains(7))
    seen.remove(7)
    print(seen.len())
}

The program ran and printed:

kavach run build.kav

2true1
build.kav: Kavach code, then what running it prints

A set isn't made by calling its type like a function, as in some other languages. The message says what to write instead:

fn main() {
    let guests = Set(["Asha", "Ravi"])
    guests.add("Meera")
    print(guests.len())
}

The compiler refused the program:

kavach run call.kav

error: `Set` can't be called like a function: make a set with `Set.of(1, 2, 3)`, or `Set.with_capacity(n)` and `add` (spec 2.2)
call.kav: Kavach code, then what running it prints