Sign in to follow this  

Inconsistent behavior with 'ref' type and out references to handle params

This topic is 1210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class base {}
class derived : base {}

class unrelated {}

void fillInProperType(ref@ obj, base@& out b, unrelated@& out u)
{
	if ( obj is null )
	{
		print("obj is null");
		return;
	}

	@b = cast<base>(obj);
	@u = cast<unrelated>(obj);

	if ( b !is null )
		print("b");
	else if ( u !is null )
		print("u");
	else
		print("both are null");
}

void workaround(ref@ obj, base@& out b, unrelated@& out u)
{
	if ( obj is null )
	{
		print("obj is null");
		return;
	}

	base@ _b      = cast<base>(obj);
	unrelated@ _u = cast<unrelated>(obj);

	if ( _b !is null )
	{
		print("b");
		@b = _b;
	}
	else if ( u !is null )
	{
		print("u");
		@u = _u;
	}
	else
		print("both are null");
}

void main()
{
	/*
	ref@ r = null;
	base@ b = null;
	unrelated@ u = null;
	fillInProperType(r, b, u);	// "obj is null" [CORRECT]
	*/

	/*
	ref@ r = base();
	base@ b = null;
	unrelated@ u = null;
	fillInProperType(r, b, u);	// "b" [CORRECT]
	*/

	/*
	ref@ r = unrelated();
	base@ b = null;
	unrelated@ u = null;
	fillInProperType(r, b, u);	// "b" [INCORRECT]
	*/

	/*
	ref@ r = derived();
	base@ b = null;
	unrelated@ u = null;
	fillInProperType(r, b, u);	// "b" [CORRECT, but probably by accident]
	*/

	/*
	ref@ r = base();
	base@ b = null;
	unrelated@ u = null;
	workaround(r, b, u);	// "b" [CORRECT]
	*/

	/*
	ref@ r = unrelated();
	base@ b = null;
	unrelated@ u = null;
	workaround(r, b, u);	// "u" [CORRECT]
	*/

	ref@ r = derived();
	base@ b = null;
	unrelated@ u = null;
	workaround(r, b, u);	// "b" [CORRECT]
}

fillInProperType() is assigning directly to an out reference to a handle and doesn't seem to work correctly. workaround() first assigns to local variables.. and that seems to work correctly.

Share this post


Link to post
Share on other sites

The good news is that CScriptHandle is behaving as it should and that the output parameters are assigned with their correct values.

 

The bad news is that the compiler is not doing the correct comparison when verifying if the output parameter is null or not. That's why in your original code the fillInProperType is always printing "b", regardless of the handle being null or not.

 

I'm still investigating the solution.

Share this post


Link to post
Share on other sites

This topic is 1210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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