Fable: Creating default values for generic types that are structs (value types)

Created on 13 Oct 2020  路  5Comments  路  Source: fable-compiler/Fable

Unchecked.defaultof<'T> where T: struct doesn't work properly, it creates null instead of the struct type constructor.

All 5 comments

@alfonsogarciacaro Something like this:

type TestType<'T when 'T: struct>() =
    let x = Unchecked.defaultof<'T>

Ah, ok. That's going to be difficult because we're not passing the generic info at runtime so it's not possible to locate the constructor :/ It should work in inlined functions though.

Closing as unsupported.

Should we error/emit a warning when the generic has a struct constraint as currently the compilation succeeds but creates wrong code? (I think we'd have to add the constraints to the generic params in Fable AST for add, as currently they only hold the name).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jwosty picture jwosty  路  3Comments

funlambda picture funlambda  路  4Comments

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

MangelMaxime picture MangelMaxime  路  3Comments

MangelMaxime picture MangelMaxime  路  3Comments