SalvadorVayshun
SalvadorVayshun

Reputation: 353

F# - Specify Type in Declaration

I am new to F#, so forgive me if this question seems silly, or if it is duplicated. If it is a duplicate, I couldn't find the answer.

The closest articles I could find are here and here. The second link has a hint to an answer, but isn't exact.

My question is this: How would I declare a variable to be specifically one type?

For example, the statement let i = 1 is basically just saying i is an integer, but only because it is set to one. So you could also say let i = "cake" and i would be a string.

Can you say something like let int i = 1? I noticed in the second link that you can kind of "re-declare" a variable by doing something like <variable> : <type>, but how would you do this in the initial declaration in F#?

Upvotes: 4

Views: 2511

Answers (1)

SalvadorVayshun
SalvadorVayshun

Reputation: 353

To close this question officially, via Jeff Mercado's comment:

let i: int = 1

So let <variable name>:<variable type> = <value>

If you do something like let i : float = 1 it will instead throw a syntax error and require you to put in something like let i : float = 1.00.

Upvotes: 3

Related Questions