dommer
dommer

Reputation: 19810

Can I get the inferred type of a value, without the contents, in F# Interactive?

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.

Upvotes: 3

Views: 205

Answers (2)

Kevin
Kevin

Reputation: 2291

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.

Upvotes: 2

CaringDev
CaringDev

Reputation: 8551

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>>[]"

Upvotes: 2

Related Questions