Sign in to follow this  
Sir Ementaler

Conditional operator is unusable with functions.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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)

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