Skip to content
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

Allow return type declaration for generated function #21322

Open
cstjean opened this issue Apr 7, 2017 · 6 comments
Open

Allow return type declaration for generated function #21322

cstjean opened this issue Apr 7, 2017 · 6 comments
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@cstjean
Copy link
Contributor

cstjean commented Apr 7, 2017

Runtime dispatch to a generated function is always typed as ::Any, for good reason:

@generated function foo(obj)
    quote
        return true 
    end
end
glob = 2
bar() = foo(glob)
@inferred bar()  # error: return type Bool does not match inferred return type Any

If we could specify the return type @generated function foo(obj)::Bool, that would help fix common cases (eg. predicates). Currently, this syntax is valid, but it specifies the return type of the expansion, which is useless.

@JeffBezanson
Copy link
Member

I see the problem, but currently return type declarations don't really work that way --- putting ::Bool on a function doesn't mean that we can or will infer Bool as the return type. It's syntax for a certain transformation of the function body. I agree it should be applied to the generated code, but we still won't be able to infer the return type without calling the generator.

@JeffBezanson
Copy link
Member

#12783 is related; also about sorting out which stage things belong to.

@yuyichao
Copy link
Contributor

yuyichao commented Apr 7, 2017

Given that we add type assertion to after the convert we should be able to infer?

@JeffBezanson
Copy link
Member

Yes, but if the argument type to the generated function isn't known at compile time, we can't call the generator, so there's no function body to look at.

@yuyichao
Copy link
Contributor

yuyichao commented Apr 7, 2017

Ok. I was assuming the representation/lowering of return type declaration will need changing for generated function since merely an automatic convertion doesn't seem to be too useful for generated function when the user can easily achieve this when generating the code.

@StefanKarpinski
Copy link
Member

Given that the other type annotations in the function signature are used for dispatch, which means they're considered to mean what they mean at application time, it seems sane to me that the return type annotation would mean that as well. In which case it would presumably mean that each return point is automatically rewritten with the appropriate convert-assert, no?

@kshyatt kshyatt added the types and dispatch Types, subtyping and method dispatch label Apr 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

5 participants