Advertisement

Conditional operator is unusable with functions.

Started by October 17, 2015 10:07 AM
2 comments, last by WitchLord 9 years, 1 month ago

Here's something I run into on a regular basis; consider the following code:


funcdef fd@ fd();
fd@ f() {
	return true ? f : f;
}

This will not compile because it "can't implicitly convert from '$func@' to 'fd@'". As far as I can tell, there's in fact no way to use the result of a conditional expression whose result is an unspecified function handle. I attempted conversions and casts but they're unsurprisingly ineffective. I also tried calling a result but that is similarly impossible:


void f() {
	(true ? f : f)();
}

This code fails with "No matching signatures to '$func::opCall()'". To my surprise, the conditional operator also doesn't differentiate between functions with completely varying signatures, i.e. the following code actually compiles:


int g(int) {
	return 0;
}
void f() {
	true ? f : g;
}

Not that the result is possible to use in any way. Another interesting fact is that whereas code


funcdef fd@ fd();
fd@ f() {
	fd@ h = f;
	return true ? h : f;
}

compiles, very similar code


funcdef fd@ fd();
fd@ f() {
	fd@ h = f;
	return true ? f : h;
}

does not. It appears to me that making all of this work as expected would require major changes to the engine, so as a more viable solution I'd suggest enabling the syntax:


funcdef fd@ fd();
fd@ f() {
	return true ? fd(f) : fd(f);
}

i.e. explicit conversion to a specific function handle.

I will look into this. Some of these are probably bugs that shouldn't be too difficult to fix.

The syntax you suggested, i.e. fd(f) is used for creating delegates, which is not what you want. To do an explicit cast the correct syntax is cast<fd>(f).

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

Ah, I didn't realize this syntax is already in use, otherwise I probably would've suggested the latter myself. That's an incredibly useful thing to know, I wonder how I overlooked it in the documentation. Thanks for pointing that out!

I've fixed the problem in revision 2244.

It was actually quite simple to fix. I had just missed adding a call in the compiler to resolve the function name into an actual function pointer.

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