0

Is there language in which integer variables are stored in list/array by reference, not by value? I.e.

a = 1
l = [a]
print(l) # [1]
a += 1
print(l) # [2]
DSblizzard
  • 4,007
  • 7
  • 48
  • 76
  • It is incorrect expression. – DSblizzard Dec 07 '19 at 14:47
  • What is the benefit of the semantics you are suggesting? It seems a bit awkward with special cases (*e.g.*, certain operations only work if the list has one element). – lurker Dec 07 '19 at 14:55
  • Which operations? I want to see benefits and drawbacks in this hypothetical language, because can imagine too little on my own. – DSblizzard Dec 07 '19 at 15:11
  • [] + [] == []; [] + [1] == [1]; [1, 2] + [1] == [1, 2, 1]. List joining works even for empty lists. I cannot understand your example. – DSblizzard Dec 07 '19 at 15:57
  • I misunderstood your original semantic, so I deleted my other comments. That was my fault. – lurker Dec 07 '19 at 16:01
  • If you are talking about list joining, then several languages will use `+` to join lists. Python and Ruby, for example, join lists with `+`: `[1] + [2] == [1,2]`, etc. Regarding the reference, I'm not sure what language would use references for integers in a list. They do use references for more complex data types in a list. So in Ruby, for example, if I say `a = 1`, define `x = [a]`, then `x == [1]`. If I then increment `a` so that `a == 2`, we still have `x == [1]`. But if I say `y = [x]` (so y == [[1]]` then do `x[0] += 1`, then I have `x == [2]` and `y == [[2]]`. That's Ruby specifically. – lurker Dec 07 '19 at 16:05

1 Answers1

0

I can't think of any language that does this for immediate types like numbers and booleans. Performance gets very bad if numbers are mutable objects. Performance gets very bad for GC VMs if numbers are mutable types on the heap.

I've seen people fake that kind of functionality in OO languages. See this post. Square bracket [] operator overloading c++

ahoffer
  • 6,347
  • 4
  • 39
  • 68