#Is this type of function unrepresentable in F#?

1 messages · Page 1 of 1 (latest)

twilit cedar
#

The equivalent is achievable in typescript 🤔

slate quest
#

Generic functions are fine but a top level let value can't be generic

craggy wedge
#

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
craggy wedge
#

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

slate quest
#

wow that's a much better explanation that my one-liner

craggy wedge
#

I have dug into this error when I first encountered it

#

I should improve it such that this question will never appear again

slate quest
#

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

craggy wedge
#

I think this is the #1 confusing error in F#

craggy wedge
#

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.

slate quest
#

Yeah, I've read that error 1000 times and I understand it now but the phrasing still feels backwards to me

craggy wedge
#

Elm points to where these types are inferred from in the error message itself

#

Using text arrows

#

F# needs them

slate quest
#

yeah, text pointing to the inference site would be amazing

craggy wedge
neat marten
#

Using accurate ranges will also help a lot in some cases

craggy wedge
#

This is the one

craggy wedge
#

Glad that's now gone

#

Untouched for 7 years, wow