Skip to content

Add examples in https://docs.scala-lang.org/overviews/core/custom-collection-operations.html that don't use ad-hoc polymorphism #1695

@mtomko

Description

@mtomko

I am porting some legacy code from 2.12 to 2.13 (this code originated maybe 10 years ago in the 2.9 or 2.10 days...) and I found this document to help me:

https://docs.scala-lang.org/overviews/core/custom-collection-operations.html

The code in question was a function that took a collection of type CC[A] and transformed it, returning (in this case) CC[Int]. The closest example, in the documentation was this one:

https://docs.scala-lang.org/overviews/core/custom-collection-operations.html#transforming-any-collection

But that example uses ad-hoc polymorphism, and my function does not. Using ad-hoc polymorphism, the solution constructs a class IntersperseOperation that takes a few type parameters, and then defines an intersperse method, which takes even more type parameters, some of which depend on the type parameters of the enclosing class.

I found it rather challenging use this example for my own code. I was hoping we could add some documentation giving an example of how (for example) you might write a method like (this won't compile or be correct, but that's the point - I'd like to see how it could be made to work):

def intersperse[A, CC[A]](xs: CC[A], sep: A): CC[A] = ???

The implementation here might not be that interesting, the key part is understanding how to capture enough information in the type parameters and implicit arguments to be able to use the implementation given already in the document.

Thanks - I want to point out that I was able to follow these docs a few weeks ago to port some code that was using ad-hoc polymorphism - that was pretty painless.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions