Arc Forumnew | comments | leaders | submitlogin
Why do I get a non-mutable hash-table?
1 point by lark 4822 days ago | 6 comments
I don't understand why I get the following warning:

hash-table-put!: expects type <mutable hash-table> as 1st argument, given: #hash(("PdqtMuXyJnNgRVwkJ" . t))

How did I create a non-mutable hash-table in the first place?



2 points by akkartik 4821 days ago | link

I just remembered running into this as well. Trying to read a literal #hash results in immutable scheme hashes.

  arc> (= h (w/instring f "#hash((a . 1))" read-table.f))
  arc> (= h!b 2)
  Error: "hash-set!: expects type <mutable table> as 1st argument..
The solution is to not use literal #hashes just like we don't use literal #t or #f. Use write-table and read-table, and make sure the tables you persist are read as lists.

  arc> (write-table (obj a 1 b 2))
  ((a 1) (b 2))

-----

1 point by lark 4808 days ago | link

Can I persist and read back a #hash that contains a #hash?

Alternatively, can I persist and read back a list of hash tables?

-----

1 point by akkartik 4807 days ago | link

Yeah I have those in my repo. Look for serialize in the link in my profile.

I'll add it to anarki later today.

Update: I just found myself complaining about this nearly 3 years ago, along with an implementation: http://arclanguage.org/item?id=10677. Not as clean as serialize, though.

Update 2: Ok, now read and write work with nested tables. You can read the output of write:

  arc> (= h (w/instring f (tostring (write (obj a 1 b (obj c 3 d 4)))) read.f))
  #hash((a . 1) (b . #hash((c . 3) (d . 4))))
And you can modify the results:

  arc> (= h!c 34) ; modify outer table
  arc> h
  #hash((c . 34) (a . 1) (b . #hash((c . 3) (d . 4))))

  arc> (= h!b!e 34) ; modify inner table
  arc> h
  #hash((c . 34) (a . 1) (b . #hash((c . 3) (e . 34) (d . 4))))
http://github.com/nex3/arc/commit/547d8966de

-----

2 points by akkartik 4821 days ago | link

Well, what did you do to get the warning?

-----

1 point by lark 4818 days ago | link

I'm wondering if I ran against some weird condition where a hash table passed as a parameter in a function makes itself immutable.

-----

1 point by akkartik 4818 days ago | link

Is there a set of steps that reliably shows this error?

-----