Fable icon indicating copy to clipboard operation
Fable copied to clipboard

Should we allow to disable reflection on a specific type?

Open MangelMaxime opened this issue 2 months ago • 1 comments

With the code below,

type SubType<'T> =
    {
        SubValue: 'T
    }

[<Erase>]
type TypeA<'T> = TypeA of (unit -> SubType<'T>)

type Updates =
    {
        Func : TypeA<unit>
    }

Fable generates this error:

error FABLE: Cannot get type info of generic parameter T. Fable erases generics at runtime, try inlining the functions so generics can be resolved at compile time.

This is because, here Func : TypeA<unit> has a concrete type giving.

However, for a library I am working on I kind of need this type of code supported.

A workaround, is to enable --noReflection flag in Fable but it means not reflection can happen anywhere, so perhaps it could be nice to introduce a [<NoReflection>] attribute to disable it only for specific types.

Another workaround is to use an anonymous record:

type Updates =
    {|
        VarView : TypeA<unit>
    |}

I will see if I can rewrite my library to use the anonymous record, as it would be better to not introduce an attribute for a really specific usage.

MangelMaxime avatar Oct 23 '25 21:10 MangelMaxime

Note, in my situation I Updates have additional properties who are mark as mutable which is invalid for anonymous records

MangelMaxime avatar Oct 23 '25 21:10 MangelMaxime