Fabulous: Replace obj with discriminated unions for better type safety and type discoverability

Created on 24 Jul 2018  路  4Comments  路  Source: fsprojects/Fabulous

Some properties are bound like obj in the json file to allow implicit conversion just like we would do in XAML.
E.g. Image.Source can be set as a string or as an ImageSource.
Or GridRowDefinition can be either "*" or GridLength.Star
Same with View.Margin

But when using such types, IntelliSense only shows that the property is obj.
Developers don't know about the implicit conversions (if they didn't check the samples) and must look into the documentation of Xamarin.Forms to know what type exactly is allowed.

Wouldn't changing those obj to discriminated unions be better?
It would make typing explicit to developers, while allowing the compiler to check if there's nothing funny going on.
And cherry on top, converters would output warnings if we're missing a case (which is not the case currently with type matching)

What do you think?

needs infos

Most helpful comment

I think F# should have type-tagged union types (string|ImageSource) (erased to obj). I am hoping we can add those in F# 5.0, partly driven by exactly this question

All 4 comments

I think F# should have type-tagged union types (string|ImageSource) (erased to obj). I am hoping we can add those in F# 5.0, partly driven by exactly this question

That would be great!

Is it something that will come in the foreseeable future (e.g. ~6 months)?
Or should we do something in the meantime and replace later once out?

I think it will take longer,. maybe a year to see through to all relevant tooling. I feel using "obj" is likely ok for now, as people nearly always do view elements via cut-and-paste snippets, but I know it's not ideal.

Closing due to #341

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slang25 picture slang25  路  6Comments

kevinha picture kevinha  路  13Comments

Happypig375 picture Happypig375  路  3Comments

willsam100 picture willsam100  路  3Comments

SergejDK picture SergejDK  路  5Comments