Sign in to follow this  

Failure in CreateScriptObject when adding class instance to dictionary

Recommended Posts

class foo {}

class FooAndData
    FooAndData ( foo@ _foo )
        @memberVar = _foo;

    foo@ memberVar;

    // todo: add more stuff

void main()
    dictionary dict = { {'entry', foo()} }; // OK
    dict['additionalEntry'] = foo();        // OK
    @dict['additionalEntry'] = foo();       // OK

    dictionary dict2 = { {'entry', FooAndData(null)} }; // OK
    //dict2['additionalEntry'] = FooAndData(null);      // "Failed in call to function 'CreateScriptObject' (Code: -6)"
    @dict2['additionalEntry'] = FooAndData(null);       // OK

I'm confused by the failure of the middle call in the second group. Why can I instantiate a parameter-less instance of foo and add it to the dictionary, but I cannot instantiate FooAndData(null) and add it to the dictionary? In my mind, the dictionary is logically storing object instances, not handles to object instances.

Edited by iraxef

Share this post

Link to post
Share on other sites

    dictionary dict = { {'entry', foo()} };  // This stores the foo instance by value
    dict['additionalEntry'] = foo();          // This copies of the foo instance (value assignment)
    @dict['additionalEntry'] = foo();       // This stores a handle to the foo instance (handle assignment)


The CreateScriptObject fails for FooAndData because the class doesn't have a default constructor and opAssign method or copy constructor (not included automatically since you implemented a non-default constructor).


When initializing the dictionary with the initalization list the dictionary was able to bypass this by storing the actual FooAndData instance, since it was a temporary and wouldn't be used elsewhere.




I'll see if I can come up with a better error message. Unfortunately I don't think it will be possible to detect the error during compile time, since the compiler doesn't know what the dictionary will do with the class on the opAssign call.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this