fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Empty array as default parameter doesn't work as expected

Open Lanayx opened this issue 5 years ago • 3 comments

Please provide a succinct description of the issue.

Repro steps

open System.Runtime.InteropServices

type C() =
    member _.M ([<Optional; DefaultParameterValue([||]:byte[])>]keyBytes:byte[]) = 
        keyBytes

Expected behavior

The code either passes empty array as default argument or doesn't compile

Actual behavior

The code compiles, but parameter is not optional anymore, I can't call M() without parameters sharplab

Known workarounds

Use null instead of empty array. Fsharp-style optional argument won't work, since the method is exposed from library

Related information

.NET Core SDK (reflecting any global.json): Version: 3.1.101 Commit: b377529961

Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows

Lanayx avatar Sep 14 '20 11:09 Lanayx

Yeah, we don't process arrays (or rather, we process them as default case: src/fsharp/TypedTreeOps.fs:L8980 - and reurn None as default value), so we have None as default value for ILParameter: src/fsharp/IlxGen.fs:L5945, resulting in not setting .param value for in IL. Since it's just metadata, you cannot set it with anything other than a literal (i.e. should be available at compile-time). I don't think, C# allows this either. I guess we should emit warning?

vzarytovskii avatar Sep 16 '20 13:09 vzarytovskii

@vzarytovskii I think it should raise error [FS0267] This is not a valid constant expression or custom attribute value

Lanayx avatar Sep 16 '20 15:09 Lanayx

Yeah, this should raise an error. In C# an error is raised unless it is a literal or null passed in. There are two issues here, really:

  • this one, which should raise an error
  • https://github.com/dotnet/fsharp/issues/8551

cartermp avatar Sep 16 '20 16:09 cartermp