Fsharp: Cannot use array/list/etc literals in attribute parameters

Created on 1 Dec 2019  路  1Comment  路  Source: dotnet/fsharp

The following code fails to compile:

open System
open Xunit

[<Theory>]
[<InlineData(12, 1, true, [||])>]
let banana (x, y, maybe, arr) = ()

This is because array literals are not considered constants for the purposes of attribute checking: https://github.com/dotnet/fsharp/blob/9be86c23a9540c41929ff3dbcd904d069ba34a34/src/fsharp/PostInferenceChecks.fs#L1589-L1613

Although not idiomatic F#, this pattern exists in multiple test frameworks (NUnit has the same feature) and should probably typecheck, considering it is allowed in C#.

From: https://twitter.com/ploeh/status/1200096980275519489

Area-Compiler Feature Improvement

Most helpful comment

FWIW, something like this ought to compile as well:

[<InlineData([|2;2;4|],    [|2|], 4,  true)>]
[<InlineData([|2;2;4|], [|3, 3|], 4, false)>]

The main point here is that it should be possible to pass more than one array, and the arrays shouldn't need to sit rightmost.

I'm explicitly adding this, as I often see people suggest using a ParamArray as a workaround; it's not.

>All comments

FWIW, something like this ought to compile as well:

[<InlineData([|2;2;4|],    [|2|], 4,  true)>]
[<InlineData([|2;2;4|], [|3, 3|], 4, false)>]

The main point here is that it should be possible to pass more than one array, and the arrays shouldn't need to sit rightmost.

I'm explicitly adding this, as I often see people suggest using a ParamArray as a workaround; it's not.

Was this page helpful?
0 / 5 - 0 ratings