简体   繁体   中英

F# Code Quotations and generic functions

If you try to enclose a generic function between the <@@ and @@> symbols, say

<@@ let f x = x in f 1 @@>

you get the following compilation error:

Inner generic functions are not permitted in quoted expressions. Consider adding some type constraints until this function is no longer generic.

Is this an implementation limitation (a missing feature not implemented yet) or a conceptual problem (or maybe both)?

EDIT: just for clarification, the same error occurs even with typed quotations using symbols <@ and @> .

EDIT 2: You can, however, annotate a generic function with the ReflectedDefinitionAttribute whose AST should be available through reflection.

Generic definitions would require lots of changes to the API. For instance, the Quotation.Var type has three fields: a name, a type (represented by a System.Type value), and a mutability flag. But if you have generic definitions, then you need to extend the variable's Type property to allow type parameters , not just concrete .NET types. But representing these is a bit tricky - how do you make sure equality works properly (eg in let z = let x (a:'a) = a in let y (a:'a) = a in x, y the two 'a s are independent and shouldn't be treated as equal, and the inferred signature is z: ('a->'a)*('b->'b) !

And it gets worse. If the generic type is internal to the definition (as in your example), then at least the overall type of the expression can still be represented in the existing F# type system (eg as a Quotation.Expr<int> ). But if the type variable can "escape", then we've got some thorny issues. For example, what is the type of <@ fun x -> x @> ? We'd like it to be something like a Quotations.Expr<forall 'a.'a> , but of course that's not a valid type in F# today.

That isn't to say that it would be impossible to solve these problems, but it would require a lot of design, implementation, and testing effort, as well as non-trivial changes to the type system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM