【问题标题】:Can I get the inferred type of a value, without the contents, in F# Interactive?我可以在 F# Interactive 中获取推断的值类型(不包含内容)吗?
【发布时间】:2015-07-27 15:43:29
【问题描述】:

在 F# Interactive 控制台中创建值时,会显示推断的值类型和内容。

我怎样才能在以后重新显示推断的类型而不显示所有内容?

例如,我有一个包含 1000 个项目的数组 mydata。在 F# Interactive 控制台中键入 mydata 将显示类型,以及数组的内容。

【问题讨论】:

    标签: f# fsi


    【解决方案1】:

    如何将 printfn 与这样的类型一起使用:

    F# Interactive for F# 3.1 (Open Source Edition)
    Freely distributed under the Apache 2.0 Open Source License
    For help type #help;;
    
    > 
    val mya : int [] = [|3; 2; 5; 6; 7; 8|]
    
    > printfn "%A" (mya.GetType());;
    
    System.Int32[]
    val it : unit = ()
    

    您可以通过使用一个小实用功能来缩短所需的打字时间:

    let pm v = printfn "%A" (v.GetType())
    

    你可以按如下方式使用:

    > pm mya;;
    
    System.Int32[]
    val it : unit = ()
    

    “pm”代表“打印我”。随便你怎么称呼它:)

    如果您不喜欢 GetType() 中的类型名称,另一种方法是导致您要评估的值出错。这将为您提供更友好的 F# 类型名称(如果您当然不介意忽略错误)。例如,在您可以执行的列表中:

    > 
    val myl : string list = ["one"; "two"]
    
    > printfn myl;;
    
    Script.fsx(195,9): error FS0001: The type 'string list' is not compatible with the type 'Printf.TextWriterFormat<'a>'
    

    注意''

    之间的类型字符串列表

    最后你可以使用:(MSDN)

    fsi.ShowDeclarationValues <- false
    

    但这只会使初始评估保持沉默。

    【讨论】:

      【解决方案2】:

      Unquote 具有类型的扩展属性:

      > let mydata = [|Some([42])|];;
      val mydata : int list option [] = [|Some [42]|]
      > mydata.GetType().FSharpName;;
      val it : string = "option<list<int>>[]"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        相关资源
        最近更新 更多