3

When a value is created in the F# Interactive console the inferred type and contents of the value are displayed.

How can I, at a later date, redisplay the inferred type without showing all the contents?

For example, I have an array, mydata of 1000 items. Typing mydata into the F# Interactive console will display the type, but also the contents of the array.

4

2 回答 2

2

Unquote具有类型的扩展属性:

> let mydata = [|Some([42])|];;
val mydata : int list option [] = [|Some [42]|]
> mydata.GetType().FSharpName;;
val it : string = "option<list<int>>[]"
于 2015-07-26T16:54:28.400 回答
2

如何使用具有如下类型的 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

但这只会使最初的评估沉默。

于 2015-07-26T18:49:32.947 回答