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

Structure with isolated objects and values that belong to value:Cloneable in an isolated expression #856

Closed
MaryamZi opened this issue May 25, 2021 · 4 comments

Comments

@MaryamZi
Copy link
Member

I believe it is currently not possible to make a structure that has both isolated objects and values that belong to value:Cloneable directly be part of an isolated expression, which may be required as the initial value of a isolated object's field or when transferring in/out such a structure (in a lock statement that accesses a "protected" variable).

I guess we can still use the spread operator (as suggested in #855) if the values that belong to value:Cloneable are subtypes of readonly.

type Config record {|
   int[] & readonly a;
   IsolatedClass b;
|};

But this would not be possible if the types of the rest of the fields are not subtypes of readonly.

type Config record {|
   int[] a;
   IsolatedClass b;
   map<boolean> c;
|};

isolated class Foo {
   private Config c;

   isolated function init(Config con) {
      self.c = con; // ERROR: RHS is not an isolated expression and cannot use `clone`/`cloneReadOnly`.
                    // `self.c = {...con};` can't be used because `a` and `c` are mutable.
   }
}

Although we can get this to work by separating out the isolated objects and the values that belong to value:Cloneable and only using clone/cloneReadOnly with the latter, I don't think it will scale with nested records that have isolated objects as field types.

isolated class Foo {
   private Config c;

   isolated function init(Config con) {
      var {b, ...rest} = con;
      self.c = {b, ...rest.clone()};
   }
}

Could be related to #784

Originally posted by @MaryamZi in #855 (comment)

@MaryamZi
Copy link
Member Author

MaryamZi commented May 8, 2023

Although we can get this to work by separating out the isolated objects and the values that belong to value:Cloneable and only using clone/cloneReadOnly with the latter, I don't think it will scale with nested records that have isolated objects as field types.

Also won't work with recursive types.

type Client isolated client object {
    remote function get(string... strs) returns anydata|error;
};

type Plugin distinct isolated object {
    isolated function process(Context context) returns error?;
};

type Context record {|
    readonly string path;
    Client 'client;
    Plugin[] plugins;
|};

type Node record {|
    readonly string path;
    Context? context = ();
    table<Node> key(path) children = table [];
|};

isolated class Class {
    private table<Node> key(path) nodes;

    function init(table<Node> key(path) nodeTable) {
        // need to initialize `self.nodes` with values from `nodeTable`
        self.nodes = table []; 
    }
}

@jclark
Copy link
Collaborator

jclark commented May 9, 2023

There's a fundamental issue which I think is related to this: #1244.

@jclark
Copy link
Collaborator

jclark commented May 9, 2023

@MaryamZi Can we close this in favour of #1244?

@MaryamZi
Copy link
Member Author

@MaryamZi Can we close this in favour of #1244?

Yes, I believe the changes proposed in "Making cloning work with isolated objects" addresses this.

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

2 participants