Sign in to follow this  
Sir Ementaler

Conditional operator is unusable with functions.

Recommended Posts

Sir Ementaler    1472

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
WitchLord    4678

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
Sir Ementaler    1472

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!

Share this post


Link to post
Share on other sites
WitchLord    4678

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

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