Skip to content

Improve internal type signature #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fxdmhtt
Copy link

@fxdmhtt fxdmhtt commented Jun 5, 2025

The signature of GroupBy.Invoke function is abnormal, which causes the secondary encapsulated 'Collection<'T> type to be incorrectly identified (identified as Id<'T>).

Refer to ChunkBy.Invoke, this should be just a typo.

@@ -305,7 +305,7 @@ type GroupBy =
static member GroupBy (x: list<'T>, f: 'T->'Key, _: list<'Key*list<'T>>, [<Optional>]_impl: GroupBy) = Seq.groupBy f x |> Seq.map (fun (x, y) -> x, Seq.toList y) |> Seq.toList
static member GroupBy (x: 'T [] , f: 'T->'Key, _: ('Key*('T [])) [] , [<Optional>]_impl: GroupBy) = Seq.groupBy f x |> Seq.map (fun (x, y) -> x, Seq.toArray y) |> Seq.toArray

static member inline Invoke (projection: 'T->'Key) (source: '``C<'T>``) : '``C<'Key * 'C<'T>>`` =
static member inline Invoke (projection: 'T->'Key) (source: '``Collection<'T>``) : '``Collection<'Key * 'Collection<'T>>`` =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the ticks in the type signature. The C here is more for documentation rather than an actual type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it won't change any behavior. Not even a public signature.
But it reads better, so we can accept the PR as a code readability improvement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, I’m wondering if @fxdmhtt has some issue with code? Something that does not compile?

@gusty
Copy link
Member

gusty commented Jun 5, 2025

The signature of GroupBy.Invoke function is abnormal, which causes the secondary encapsulated 'Collection<'T> type to be incorrectly identified (identified as Id<'T>).

@fxdmhtt please create an issue for this. with a small repro, then we can help and reference eventually a PR that will address it.

We'll accept this PR but it's unlikely that it will solve any issue.

@gusty gusty changed the title Update Collection.fs Improve internal type signature Jun 5, 2025
@fxdmhtt
Copy link
Author

fxdmhtt commented Jun 5, 2025

I have seen your reply and I completely agree with what you said.

I think it is more of my own problem and I didn't want to bother you about it.

Please allow me to extract a minimal reproducible code tomorrow. Thank you very much!

@fxdmhtt
Copy link
Author

fxdmhtt commented Jun 6, 2025

Thank you very much.

The minimal example code is as follows:

open FSharpPlus

let inline groupby (key: 'T -> 'Key when 'Key : equality) (sequence: '``Collection<'T>``) : Map<'Key, '``Collection<'T>``> =
    sequence
    |> groupBy key
    |> Map.ofSeq

I will get a wavy line error on groupBy:

The type 'Internals.Id<'Key * Internals.Id<'T>>' is not compatible with the type '('Key * 'Collection<'T>) seq'.

This code doesn't really mean anything, I'm just more familiar with Python so I'm trying to simulate the behavior of the Toolz package. This is a toy.

Specifically, I'm trying to wrap the generic groupBy as Toolz.groupby

I'm a beginner in F# and don't know if this problem is caused by FSharpPlus, but as of now, I really don't have any idea to solve this compilation error.

FSharpPlus: 1.7.0
OS: Windows 10
.NET: 9.0

Thanks again!

Additional information:

I found that when the function signature is changed to the following:

let inline groupby (key: 'T -> 'Key when 'Key : equality) (sequence: '``Collection<'T>``) =
    sequence
    |> groupBy key
    |> Map.ofSeq

that is, the signature of the function output is removed.

the compilation error will disappear, but the type inference will not be able to recognize the real information.

The Ionide plugin infers:

val inline groupby:
   key     : ('T -> 'Key) ->
   sequence: 'Collection<'T> (requires static member GroupBy )
          -> Map<'b,'c> (requires comparison)

I don't know how to modify

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