Skip to content

Cannot pass in voptions to optional arguments marked as [<Struct>] #19074

@zvmsbackend

Description

@zvmsbackend

The release of F# 10 introduces to ability to mark an optional parameter as [<Struct>] to avoid unnecessary heap allocation. However, due to some flaw of the type checker, one now cannot directly pass in a voption to such a parameter.

Repro steps

Suppose there is a method with a [<Struct>] optional argument:

type Foo =
    static member Bar([<Struct>] ?x: unit) = x

Now if one pass in a voption, they gets an FS0001:

Foo.Bar(?x=ValueNone)
// error FS0001: This expression was expected to have type
//     'unit option'
// but here has type
//     'unit voption'

Conversely, passing in an option does not work too:

Foo.Bar(?x=None)
// error FS0193: Type mismatch. Type
//     “'a option”
// and type
//     “unit voption”     are incompatible

Expected behavior

The compiler should at least accept the case of voption into [<Struct>] optional parameter. Additionally, the compiler may apply implicit conversion between options and voptions to provide better backward compatiblity.

Actual behavior

It doesn't.

Known workarounds

Avoid explicit optional argument passing, and resort to pattern matching or something to decide whether the argument is to be passed in to the method.

Related information

  • .NET SDK: .NET 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBug

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions