Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list is immutable? #2

Open
mw66 opened this issue Oct 3, 2023 · 3 comments
Open

list is immutable? #2

mw66 opened this issue Oct 3, 2023 · 3 comments

Comments

@mw66
Copy link

mw66 commented Oct 3, 2023

e.g.

$ ./chickenclisp 
=> (set x '(1 2 3))
[1, 2, 3]
=> (array-set-n x 0 4)
[4, 2, 3]
=> x
[1, 2, 3]

x is unchanged.

while in Lisp (SBCL), list is mutable:

* (defvar x '(1 2 3))
X
* x
(1 2 3)
* (setf (car x) 4)
4
* x
(4 2 3)

x is mutable.

@mw66
Copy link
Author

mw66 commented Oct 3, 2023

and none of set, set-p, set-c can modify (car x):

$ ./chickenclisp 
=> (set x '(1 2 3))
[1, 2, 3]
=> (set (car x) 4)
object.Exception@source/orelang/Value.d(143): Enforcement failed
$ ./chickenclisp 
=> (set x '(1 2 3))
[1, 2, 3]
=> (set-p (car x) 4)
Segmentation fault (core dumped)
$ ./chickenclisp 
=> (set x '(1 2 3))
[1, 2, 3]
=> (set-c (car x) 4)
object.Exception@source/orelang/Value.d(182): Enforcement failed

@mw66
Copy link
Author

mw66 commented Oct 3, 2023

The reason is in:

this.opAssign(value);

void opAssign(T)(T[] value) if (!is(T == Value) && !is(T == immutable(char))) {

So int / number ... all other types array (except string i.e immutable(char)) ... is always copied on modification, I don't think it's Lisp list / array semantics.

This is a bug, actually the opposite is true: only immutable string (in D's semantics) should be always copied on modification.

@mw66
Copy link
Author

mw66 commented Oct 3, 2023

Also what's the good of this init()?

void init() {

  1. first, in opAssign(), init() is called, then this.type is set; all the ifs in init() is meaningless.
  2. even if you want to check those so many ifs, why not using switch(this.type)? at least it will jump and break, to save some CPU cycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant