6

This code will generate the following warning

module TimeSeries open System type TimedValue<'T> = { ts : DateTime; value: 'T} type TimeSerie<'T> = TimedValue<'T> seq let t : TimedValue<'double> = { ts = DateTime.Today; value=5} 

Warning:

This construct causes code to be less generic than indicated by the type annotations. The type variable 'double has been constrained to be type 'int'.

I am quite new to F#, I think the 5 is interpreted as an int and somehow F# tells me that I asked a double but it will be an int.

When I tried replacing 5 with 5. this told me that it was still constrained by the float type.

Should I somehow cast it in double or just remove the declaration part : TimedValue<'double> and let F# deal with the types ?

1 Answer 1

12

Remove the apostrophe before double.

let t : TimedValue<double> = { ts = DateTime.Today; value=5.0} 

A leading apostrophe is used to declare a type argument. So, you've declared a generic value but, by specifying value=5 you've constrained the type arg to be int. You could also use a wildcard in place of the type arg:

let t : TimedValue<_> = { ts = DateTime.Today; value=5.0} 

or remove the type annotation completely:

let t = { ts = DateTime.Today; value=5.0} 
Sign up to request clarification or add additional context in comments.

3 Comments

And note that the correct name for double in F# is actually float.
@Jon Harrop: Thanks Jon, I was wondering about this as well, I did not start reading your book yet, I am still on "Beginning F#"
double and float are both aliases for System.Double, but yes, float is more common/idiomatic.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.