javafx and mutability

There seems to be some misconceptions about mutability rules with JavaFX sequences (see here for example).

We can see what is going on under the hood by using the java disassembler (javap).

// seq.fx
var mylist = [1..3];
insert 4 into mylist;

compiling using javafxc seq.fx and then disassembling it with javap -c seq shows us what goes on under the hood.

  Code:
   0:   getstatic       #1; //Field $mylist:Lcom/sun/javafx/runtime/location/SequenceVariable;
   3:   iconst_1
   4:   iconst_3
   5:   invokestatic    #2; //Method com/sun/javafx/runtime/sequence/Sequences.range:(II)Lcom/sun/javafx/runtime/sequence/Sequence;
   8:   invokevirtual   #3; //Method com/sun/javafx/runtime/location/SequenceVariable.setAsSequence:(Lcom/sun/javafx/runtime/sequence/Sequence;)Lcom/sun/javafx/runtime/sequence/Sequence;
   11:  pop
   12:  getstatic       #1; //Field $mylist:Lcom/sun/javafx/runtime/location/SequenceVariable;
   15:  iconst_4
   16:  invokestatic    #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   19:  invokevirtual   #5; //Method com/sun/javafx/runtime/location/SequenceVariable.insert:(Ljava/lang/Object;)V
   22:  aconst_null
   23:  areturn

Line 19 shows that sequence variables are mutated in-place. No new sequences are created. This is different to clojure, where operations on lists do not mutate the lists themselves.

Update: I’ve changed my mind. It is probable that the sequences are not mutated, for all I’ve shown is the sequence variable remaining unchanged.

Bookmark and Share

You should follow me on twitter here

Leave a Reply