ThakeeNathees / pocketlang

A lightweight, fast embeddable scripting language.
https://thakeenathees.github.io/pocketlang/
MIT License
1.52k stars 80 forks source link

[Design] add copy constructor of List and Map (BREAKING) #272

Open khchen opened 2 years ago

khchen commented 2 years ago
  1. List/map constructor can accept a list/map, works as copy constructor.
  2. The original list constructor is abandoned, it do the same thing as list literal .
  3. List constructor can accept a integer, returns a preallocated list (filling in null).
  4. List join (+ operator) should always returned a new list, instead the old one.

Example:

l1 = [1, 2, 3]
l2 = l1
l3 = List(l1)
l1[0] = 4
print(l1); print(l2); print(l3)

m1 = {1: 1, 2: 2, 3: 3}
m2 = m1
m3 = Map(m1)
m1[1] = 4
print(m1); print(m2); print(m3)

print(List(3))

Output:

[4, 2, 3]
[4, 2, 3]
[1, 2, 3]
{2:2, 1:4, 3:3}
{2:2, 1:4, 3:3}
{2:2, 1:1, 3:3}
[null, null, null]