Empty array as default parameter doesn't work as expected
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
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 I think it should raise error [FS0267] This is not a valid constant expression or custom attribute value
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