2

我有这个记录:

type Client = 
   { Name : string; Income : int ; YearsInJob : int
     UsesCreditCard : bool;  CriminalRecord : bool }

要为此记录创建生成器,我尝试了以下代码:

let chooseFromList xs =
    gen {
        let! idx = Gen.choose(0, List.length xs - 1)
        return (List.nth xs idx)
    }



let generateName = (Gen.oneof [ gen {return "A"};gen {return "B"};gen {return "C"}])
let generateIncome=  (chooseFromList [0..5000])
let generate_YearsInJob= chooseFromList [0..45]
let generate_UsesCreditCard = (Gen.oneof [ gen { return true }; gen { return false } ])
let generate_UsesCriminalRecord= (Gen.oneof [ gen { return true }; gen { return false } ])



let genertate_Client =
{

      Name= generateName;
      Income=generateIncome;
      YearsInJob=generate_YearsInJob
      UsesCreditCard=generate_UsesCreditCard
      CriminalRecord=generate_UsesCriminalRecord
}

问题Name= generateName;与我遇到的错误一致:

This expression was expected to have type string,but here has type Gen<string> 

最后剩余的行存在此错误。

4

1 回答 1

4

当您想要为您的客户生成一个生成器时,您必须首先为您的字段数据生成示例 - 所以gen { ... }语法很方便:

let genertate_Client =
   gen {
      let! name = generateName
      let! income = generateIncome
      let! yearsInJob = generate_YearsInJob
      let! creditCard = genergenerate_UsesCreditCard
      let! criminal = generate_UsesCriminalRecord
      return { 
         Name = name; 
         Income = income;
         YearsInJob = yearsInJob;
         UsesCreditCard = creditCard;
         CriminalRecord = criminal }
   }

当然,在这里您使用计算中的语法从生成器中生成示例let!

于 2015-11-25T05:21:10.647 回答