gpt4 book ai didi

f# - f# 中的参数化测试 - 这不是有效的常量表达式

转载 作者:行者123 更新时间:2023-12-04 18:40:12 25 4
gpt4 key购买 nike

出于某种原因,当通过 TestCase 将参数传递给测试时属性,我收到有关第一个参数的以下错误消息,在本例中是一个数组:

This is not a valid constant expression or custom attribute value


module GameLogicTest = 
open FsUnit
open NUnit.Framework
open GameLogic.Examle

// This is not a valid constant expression or custom attribute value
[<TestCase( [| 1; 2; 3 |], 3, 1,1)>]
let ``let example.`` (a, m, h, c) =
a
|> proof1 m
|> should equal (h,c)

但是当从属性和方法本身中删除最后一个参数时,一切正常。
[<TestCase( [| 1; 2; 3 |], 3, 1)>]
let ``let example.`` (a, m, h) =
a
|> proof1 m
|> should equal (h,1)

我究竟做错了什么?最好我还会定义一个元组 int * int但它似乎也不起作用。

最佳答案

CLI 对属性参数的种类有限制:

  • 原语:bool、int、float 等
  • 枚举
  • 字符串
  • 类型引用:System.Type
  • 'kinda objects':盒装(如果需要)以上类型的表示
  • 上面一种类型的一维数组(即不允许嵌套数组)

  • 所以我们可以得出结论,你不能使用元组作为属性参数的类型。

    从这里开始我的猜测,所以以下都不是真的。

    我玩了一些不同的属性,发现当属性具有可变数量的参数 (params) 时,F# 编译器开始提示每个数组参数。例如,如果我定义以下属性
    public class ParamsAttribute : Attribute
    {
    public ParamsAttribute(params object[] parameters)
    {}
    }

    并尝试从 F# 代码中使用它,我会收到一个错误:
    [<Params(1, 2, 3)>] // here everything is OK
    let x () = ()

    [<Params([|1; 2; 3|])>] // the same error as you have
    let y () = ()

    我猜编译器可以将 params 参数视为数组,因此不允许在其中定义“嵌套”数组。但正如我所说,这纯属猜测。

    关于f# - f# 中的参数化测试 - 这不是有效的常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012156/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com