jw-y / pkl-python

Python binding for Apple's Pkl language
MIT License
34 stars 0 forks source link

Objects are duplicated instead of referenced #2

Closed jonpry closed 1 month ago

jonpry commented 2 months ago

If I create multiple references to the same instance in pkl, such as:

class Node {
  name: String
}

class Foo {
  ref: Node
}

node = new Node {
   name = "Hello"
}

foo1 = new Foo {
   ref = node
}

foo2 = new Foo {
  ref = node
}

I find that this property is not preserved in python. pkl.foo1.ref == pkl.foo2.ref because of elementwise comparison in python. But if I pkl.foo1.ref = "New" then pkl.foo1.ref != pkl.foo2.ref

I am constructing complex graphs in pkl. Being able to check that these references are equal is very useful. Is there a way to add this feature or is this information already lost in the message passing API?

jw-y commented 2 months ago

Unfortunately, I believe above information is lost by the time the message is received.

If you run,

import pkl
from pkl import Parser
from pprint import pprint

class NewParser(Parser):
    def parse(self, obj):
        pprint(obj)

pkl.load("./tmp.pkl", parser=NewParser())

output (msgapi output):

[1,
 'tmp',
 'file:///home/jwyang/dev/pkl-python/tmp.pkl',
 [[16,
   'node',
   [1,
    'tmp#Node',
    'file:///home/jwyang/dev/pkl-python/tmp.pkl',
    [[16, 'name', 'Hello']]]],
  [16,
   'foo1',
   [1,
    'tmp#Foo',
    'file:///home/jwyang/dev/pkl-python/tmp.pkl',
    [[16,
      'ref',
      [1,
       'tmp#Node',
       'file:///home/jwyang/dev/pkl-python/tmp.pkl',
       [[16, 'name', 'Hello']]]]]]],
  [16,
   'foo2',
   [1,
    'tmp#Foo',
    'file:///home/jwyang/dev/pkl-python/tmp.pkl',
    [[16,
      'ref',
      [1,
       'tmp#Node',
       'file:///home/jwyang/dev/pkl-python/tmp.pkl',
       [[16, 'name', 'Hello']]]]]]]]]

You can try different methods, such as