fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.93k stars 300 forks source link

Reflection for classes #2027

Open Shmew opened 4 years ago

Shmew commented 4 years ago

I've been working on bindings for fast-check which are mostly complete.

The library has the ability to do model-based testing, which I have implemented:

image

The thing I'd like to do is automatically generate a complete Arbitrary<'Msg> if it's possible as I believe it would make writing these cases even easier.

I have reflection working for almost all types, but the thing giving me trouble is getting class constructors.

Is this something that can currently be resolved with Fable? If not, is adding the support for these feasible?

Shmew commented 4 years ago

@alfonsogarciacaro think you could give me guidance on where to start with implementing this? I'd be willing to try a PR if I had a better idea of where to start.

alfonsogarciacaro commented 4 years ago

Well, actually I think @krauthaufen has already done that in #1839 which, besides adding support for quotations, increases the support for reflection. I've been very busy for the last months so I couldn't check the PR properly but time allowing I'll try to start publishing alpha/beta versions of Fable 3 based on the PR soon.

In any case, there are instructions in the PR description about how to test it right now. The fable-compiler-quotations package is a bit old, but I'll merge the branch with master and publish a new version.

alfonsogarciacaro commented 4 years ago

Having trouble with merging master into quotations but forgot to mention System.Activator is supported in the latest version of Fable, so you can do something like this (inlining is important to know the type at compile time):

type A(i1: int, i2: int) =
    member x.Value2 = i2 + i2

let inline activate<'T> (args: obj[]) =
    System.Activator.CreateInstance(typeof<'T>, args) :?> 'T

let test() =
    let a = activate<A> [|4; 5|]
    a.Value2 |> printfn "%i"

test()
alfonsogarciacaro commented 4 years ago

You can check the Reflection tests to see what's currently supported. FSharpValue.MakeRecord/MakeUnion are supported too.

Shmew commented 4 years ago

I've explored quite a bit with what we can currently reflect when making fast-check bindings for fable.

System.Activator is supported in the latest version of Fable

Yeah, that's what got me thinking about this issue again actually! The issue I'm having that prevents just using that is I need to be able to tell given a System.Type if it is a class. If there's something else available currently that would solve this that would be great too!

alfonsogarciacaro commented 4 years ago

A class in what sense? Something that's not a record, a union, a tuple, an array or an enum? In that case, maybe you can just write a helper to discard the rest of the options.

Shmew commented 4 years ago

So my use-case is I want to be able to automatically generate an arbitrary of any type using default primitive arbitraries (if necessary).

So for example if they have something like this:

type MyClass (someNum: int) =
    let mutable someInnerState = someNum
    member _.AddOne () = someInnerState <- someInnerState + 1

Which I would receive a System.Type of MyClass. Then I would be able to create an arbitrary for someNum and construct the class by mapping that arbitrary into the constructor.

alfonsogarciacaro commented 4 years ago

Hmm, this can be done with Activator, but the problem is we cannot know the types of the constructor arguments for a class right now (this can be done with records and unions though), but it would be very interesting for your use case, I'll see what I can do.

I was a bit reluctant to include information for all the class members because it could increase the bundle size, but maybe we could generate an independent getMembers method that gets removed by tree shaking if not being called. The only caveat would be that you need to know the type at compile time to access the members as in:

let inline getMembersOf<'T>() =
    typeof<'T>.GetMembers()

getMembersOf<MyClass>()
Shmew commented 4 years ago

Yeah all the types would be known at compile time, that would be great!

alfonsogarciacaro commented 4 years ago

Related #2085