Thomas
Thomas

Reputation: 12107

Not understanding F# syntax with type conversion

This works:

let a = 3
let b = string b
let c = a |> string

but:

let a = string j.SelectToken("hello")

doesn't work, while

let a = j.SelectToken("hello") |> string

works.

why is that?

Upvotes: 0

Views: 82

Answers (2)

Scott Hutchinson
Scott Hutchinson

Reputation: 1721

Unless you wrap j.SelectToken("hello") in parentheses, you get this error:

Successive arguments should be separated by spaces or tupled, 
and arguments involving function or method applications should be parenthesized
F# Compiler(597)

Why has already been answered at https://stackoverflow.com/a/23848236/5652483

There is an open issue with interesting discussion at https://github.com/fsharp/fslang-suggestions/issues/644

Upvotes: 1

Jim Foye
Jim Foye

Reputation: 2036

The compiler thinks you want to pass j.SelectToken to string. Remember, functions are values, so that is legal. Any of these work:

let a = j.SelectToken "hello" |> string
let a = string (j.SelectToken "hello")
let a = string <| j.SelectToken "hello"
let a = "hello" |> j.SelectToken |> string

Upvotes: 1

Related Questions