Jump to content
  • Advertisement
Sign in to follow this  
iraxef

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

This topic is 1427 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!