tov / dssl2

A data structures student language, version 2
MIT License
9 stars 4 forks source link

Add op-assign operators such as `+=`, `-=`, etc. #13

Open tov opened 4 years ago

tov commented 4 years ago

We should be able to write:

def sum_vec(v: vec[int]):
    let res = 0
    for n in v: res += n
    res
tov commented 4 years ago

Question: Which of these is E1 += E2 sugar for?

markovejnovic commented 3 years ago

Question: Which of these is E1 += E2 sugar for?

  • E1.__iadd__(E2)
  • (fake syntax for references):
    let rval = &E1
    *E1 = *E1 + E2

Personally, I think __iadd__ is the better choice. Suppose you have:

let a = [3, 4, None, None]
let b = [6, 8]

One could choose to implement:

let c = a + b
a == a # Not mutating
b == b # Not mutating
c == [3, 4, None, None, 6, 8]
a += b
a == [3, 4, 6, 8] # Mutating

Not the best design choice in my opinion, but someone might have a good reason for doing that.