@contact has joined the channel
In the Racket 6.12 Typed Racket Guide section 4.8.2 Polymorphic Functions, the example is of type (All (A) (-> (Listof A) Integer)))
. In this case, and essentially any case with a single occurrence of the type variable, I can’t see any benefit over Any
. Am I missing something (I realize the docs don’t claim it’s actually worthwhile in this case).
That’s interesting, I would like to know the answer too
If we were considering a different type system, the type (All (A) (-> (Listof A) Integer)))
would tell us that this function can never inspect the elements in the list it received but only look at the length. But Typed Racket is different..
I’m only a dabbler in Typed Racket (and “strong” static typing generally), but: The previous two examples are lists where the elements are all the same type — (Listof Number)
and (Listof String)
. So I might expect that (All (A) (-> (Listof A) Integer)))
means it takes a list where every element is the same type (as opposed to a heterogeneous list). Such as (list 1 2 3)
— but not (list 1 'two "three")
. And yet, when I try the example list-length
function with say (list-length (list 1 'two "three"))
, it happily type-checks and says 3
.
The thing is, Typed Racket has subtyping, and all types have a common supertype, Any
, so the caller can always pick Any
for A
.
I think what @gfb says is correct, but I can’t be sure. @samth or @pnwamk would probably know for certain.
What @gfb says is right