What' the meta programming story in Fable 2.0?
See original GitHub issueI’m playing with F#'s quotation feature recently, and I find it’s really powerful because we can create a lambda function based on expression tree and eval it to a real function, this is much like macros in other languages. Unfortunately, it’s unavailable in Fable.
Here are some cases that I would use some equivalent feature in Fable:
1. Create default empty record.
type People = {
name: string
age: int
}
module People =
let empty =
{ name = ""
age = 0 }
In F#, I need to write lots’ of empty record, it could actually be done by Reflection or quotations, but Fable’s reflection API is very limit, I’m not sure there is a way to do this. And since Fable 2.0 will use more lightweight types, plugins API with full access to generic type info might be a good idea.
2. Create functions dynamically
I usually create lots of Records/Classes, like BookInput
(book model for input form), BookOutput
(book model for output), BookEntity
(book entity for ORM) and need write lots of functions to transform between them. The transform functions can be generated by quotations, but there still are some in fables that I’m not sure how to implement, like BookOutput
-> BookInput
.
Fable’s interop API is very powerful and convenient, but as same as No.1, without type information it could be really hard to handle FSharp values/types.
I know reflection, plugins are not included in the alpha release, but maybe it’s the time for discussion? 😄
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (8 by maintainers)
@Zaid-Ajaj For that, we need to add first the metadata before making it optional 😃 But in any case, I don’t think it’s a question of all or nothing, because in most cases users would need reflection for only a few types. Also, just to let you know, I’m very interested in making Fable.Remoting work with Fable 2 (stable) out-of-box or with little changes. I already had a look at the source and I will try to find if it’s possible to resolve the needed reflection info at compile time.
@zaaack I’m not totally sure, but I guess TP accept types as static parameters. Attribute constructor arguments are also static and beside literals (strings and numbers) they also accept types.
TypeProviders don’t accept other types as static parameters. https://stackoverflow.com/questions/9547225/can-i-provide-a-type-as-an-input-to-a-type-provider-in-f