Advertisement

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

Started by August 18, 2014 04:13 PM
2 comments, last by WitchLord 10 years, 3 months ago

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.

Thanks. I'll investigate this.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Fixed in revision 1994.

Regards,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement