Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

types

f#

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 ?

like image 384
BlueTrin Avatar asked Oct 22 '12 21:10

BlueTrin


1 Answers

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}
like image 196
Daniel Avatar answered Nov 20 '22 17:11

Daniel