• Advertisement
Sign in to follow this  

Conditional operator is unusable with functions.

This topic is 915 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

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
Advertisement

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

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

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

  • Advertisement