#Is this type of function unrepresentable in F#?
1 messages · Page 1 of 1 (latest)
Generic functions are fine but a top level let value can't be generic
Yup, a value cannot be generic even if it's a function value
But a function not defined as a value can be generic
This is a CLR restriction
But, you can still let a function imitate a value
let g<'T> = f<'T> ()
This is a function with no value parameters but with a type parameter
F# requires explicitness here
Meanwhile, if the generic type is inferred to be a concrete type it is allowed
let g = f()
do g // The do expression forces it to be a unit
This is allowed
Another example
let plus = (+) // Cannot be generic
let plus = (+)
let a = plus 1. 3. // Constrained to be a float type
TypeScript compiles down to JavaScript which has no notion of generics
so it is just a obj -> obj under the hood
If you throw away generics from the F# function
let f (a: obj) = a
let g = f() // just an obj
This is also allowed
I guess the main concern here is that if a value can be generic then its behaviour under different type parameters would conflict with values supposedly unchanging
I can improve this error message later
This has been a recurring question
and the current error message doesn't explain quite enough
wow that's a much better explanation that my one-liner
I have dug into this error when I first encountered it
I should improve it such that this question will never appear again
probably linking to the discord in error messages is non-canonical but a "More Info" hyperlink to such an explanation in the error message would be great
I think this is the #1 confusing error in F#
Just write more in the message itself
Rust and Elm error messages are long and explain a lot
F# errors aren't as good as them
Another error message is the type mismatch
ok "int" doesn't match "string". But where did the compiler infer those?
Manual type annotations needed? It's work.
Yeah, I've read that error 1000 times and I understand it now but the phrasing still feels backwards to me
Elm points to where these types are inferred from in the error message itself
Using text arrows
F# needs them
yeah, text pointing to the inference site would be amazing
Using accurate ranges will also help a lot in some cases
This is the one
I still remember when type mismatch in a list expression highlighted the entire list
Glad that's now gone
Now for this question specifically the tracking issue is https://GitHub.com/dotnet/fsharp/issues/1161
Untouched for 7 years, wow