Workaround type testing against interface (not supported) or classes with generics (emits warnings) #3886
MangelMaxime
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I wanted to share with you a tips that I found today. In a project of mine, I needed to type test against interface but this is not supported by Fable.
So I decided to use abstract class instead because they are supported.
However, when type testing against classes:
Fable generate a warning 'Generic args are ignored in type testing'.
Unfortunately, we cannot suppress this warning, and it is emitted once per compilation. This means that any Fable.Form.Simple.Bulma user will see this warning...
To work around this issue, it possible to dynamically cast the instance:
The code above will not generate any warning, and works if we call an API that is specific to
the casted type.
In the case above,
InnerField
only exists inIStandardField
, so if we get an instance ofIGenericField
,we will get an exception which then have a chance to handle, and so on.
However, I am not a fan of this approach, as it feels dirty to use exceptions for control flow.
The solution I found, is to use a DU that we get access to via implementing a class
IRendererField
.And then, we make the abstract class
IStandardField
andIGenericField
inherit fromIRendererField
and specify the type of field renderer they are.
This makes the code feel cleaner, emit no warning and closer to type testing against classes/interfaces.
Snippet of the domain use in my application
I wanted to share this tips I found, as perhaps it can be useful to someone else or the future me 🤣.
Beta Was this translation helpful? Give feedback.
All reactions