Skip to content

Can't call conditional expression #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andy-hanson opened this issue Sep 9, 2018 · 4 comments
Closed

Can't call conditional expression #255

andy-hanson opened this issue Sep 9, 2018 · 4 comments
Labels

Comments

@andy-hanson
Copy link
Contributor

function f(x: i32): i32 { return x; }
function g(x: i32): i32 { return x + 2; }
export function test(b: boolean): i32 {
  return (b ? f : g)(1);
}

Currently fails with:

ERROR TS1005: ')' expected.

   return (b ? f : g)(1);
             ~
@MaxGraey
Copy link
Member

MaxGraey commented Sep 9, 2018

Hmm, for this specific case when signatures are identical it pretty simple, we can just syntactic sugaring to b ? f(1) : g(1), but that about this:

function f(x: i32): i32 { return x; }
function g(x: i32, y: i32): i32 { return x + y; }

function test(b: boolean): i32 {
  return (b ? f : g)(1, 2);
}

This valid in Typescript btw.
Of course this could be translated to b ? f(1) : g(1, 2). But as for me it's a rather dangerous approach.

@andy-hanson
Copy link
Contributor Author

andy-hanson commented Sep 10, 2018

You can already do this in master using let fn = b ? f : g;. It looks like AssemblyScript doesn't do function subtyping, so it's an error if f and g aren't exactly the same type.
However, there's apparently still an error for b ? f : g in the resolver, since this is just a temporary expression and there's no local variable to resolve to.
The semantic part of this problem seems basically the same as #251 -- compileCallExpression expects to just resolve the called expression and never compiles it, which is a problem since the called expression may contain arbitrary code.

@dcodeIO
Copy link
Member

dcodeIO commented Sep 10, 2018

Yeah, it can't resolve to a concrete Function in this case, but there's also something I simply called FunctionTarget (basically an index bound to a signature) that should work, that is if the resolver had code to combine a left and right Function to a FunctionTarget.

@MaxGraey
Copy link
Member

Resolved in #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants