Skip to content

Conversation

@NoahStoryM
Copy link
Contributor

length just returns the length of a list, it should have nothing to do with the type of the list.

@capfredf
Copy link
Member

I would say (All (a) (-> (Listof a) Index)) is 1. more accurate 2. in line with types for other list functions, though (-> (Listof Any) Index) won't be wrong for length. As to typed-untyped interaction, I need to sleep on it. By the way, do you have any programs that the polymorphic type gets in your way?

I'll take another look at the types for indexes-of and indexes-where later this week. Thanks

@NoahStoryM
Copy link
Contributor Author

I think that length should be in line with types for length functions.

For example, I want to implement a generic function for finding the length of a data structure:

#lang typed/racket


(: make-length (All (A) [-> (pred A) [-> A Index] [-> Any * Index]]))
(define make-length
   (λ (pred len)
     (λ args
       (match args
         [`(,arg) #:when (pred arg) (len arg)]))))


(make-length bytes?  bytes-length)
(make-length string? string-length)
(make-length vector? vector-length)
(make-length list?   length)

Although that (inst length Any) can be used to solve the problem of this code, but I compared the types of length and vector-length, I can't get the meaning of defining length as a polymorphic type -- After all, it just returns the length of a list, it has nothing to do with the specific type of the list, I think this is also the reason why the argument type of vector-length is VectorTop.

@capfredf
Copy link
Member

That seems a valid point. Note that in your example only vector and list are containers. Since length has been assumed to have a polymorhic type, for back -compatibility, I think we should keept it that way. (All (a) (-> (Listof Any) Index) looks a bit weird to me
a does not appear in the body and the whole type is actually mormorphic.

@NoahStoryM
Copy link
Contributor Author

NoahStoryM commented Mar 29, 2022

I also feel that (All (A) (-> (Listof Any) Index)) looks weird. I thought maybe TR will give warnings for these usages in the future, and eventually TR may no longer be compatible with them. So that (All (A) (-> (Listof Any) Index)) is a solution that allows me to avoid (inst length Any) now.

And it won't be a big problem for me to change the code back if that day comes. I agree to keep the current type of length.

@NoahStoryM NoahStoryM changed the title Add types for indexes-of and indexes-where, and modify the type of length. Add types for indexes-of and indexes-where. Mar 29, 2022
@samth
Copy link
Member

samth commented Jun 17, 2022

@capfredf are you ok with this now?

@capfredf
Copy link
Member

lgtm

@capfredf capfredf merged commit 21780da into racket:master Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants