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.

dommer
  • 19,610
  • 14
  • 75
  • 137

2 Answers2

2

Unquote has an extension property for types:

> let mydata = [|Some([42])|];;
val mydata : int list option [] = [|Some [42]|]
> mydata.GetType().FSharpName;;
val it : string = "option<list<int>>[]"
CaringDev
  • 8,391
  • 1
  • 24
  • 43
2

How about using printfn with the type like so:

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 = ()

You can shorten the typing required by using a little utility function:

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

The you can use as follows:

> pm mya;;

System.Int32[]
val it : unit = ()

"pm" stands for "print me". Call it whatever you want :)

Another approach if you don't like the type names from GetType() is just to cause an error with the value you want to evaluate. That will give you a more friendly F# type name (if you don't mind ignoring the error of course). For instance on a list you could do:

> 
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>'

Note the type string list between the ''

Lastly you can use: (MSDN)

fsi.ShowDeclarationValues <- false

But this only silences the initial evaluation.

Kevin
  • 2,281
  • 1
  • 14
  • 16