Skip to content
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

Add a guide on generic programming with collections #1088

Merged
merged 1 commit into from
Feb 4, 2019

Conversation

julienrf
Copy link
Contributor

Note: this PR depends on scala/scala#6674. If that one does not get merged I will rework it.

@julienrf julienrf requested review from SethTisue and lrytz June 20, 2018 00:40
Copy link
Member

@lrytz lrytz left a comment

Choose a reason for hiding this comment

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

Very nice, thanks a lot!

icon: building
url: "core/custom-collections.html"
by: Martin Odersky, Lex Spoon and Julien Richard-Foy
description: "In this document you will learn how the collections framework helps you define your own collections in a few lines of code, while reusing the overwhelming part of collection functionality from the framework."
- title: Generic Programming With Collections (Scala 2.13)
Copy link
Member

@lrytz lrytz Jun 20, 2018

Choose a reason for hiding this comment

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

I think we should find a different title. The term "generic programming" in the context of Scala is claimed by shapeless. Reusing / overloading it can cause confusion.

Something like "Writing generic, external operations that integrate with all collection types"

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that "generic programming" is probably not the best term here. Something a bit pithier and closer to the original would be "Programming With Collections Generically".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I’ve changed the title for “Programming with collections generically”

Copy link
Member

Choose a reason for hiding this comment

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

still not a good title IMO, see my other comment on that

_data/overviews.yml Outdated Show resolved Hide resolved
to support `String` and `Array` values we have to use `IsSeqLike` instead. `IsSeqLike` is similar to
`IsIterableLike` but provides a conversion to `SeqOps[A, Iterable, C]` (for some types `A` and `C`).

Note that in the case `Seq` as well as `Map`, using `IsSeqLike` and `IsMapLike`, respectively, is also
Copy link
Member

@lrytz lrytz Jun 20, 2018

Choose a reason for hiding this comment

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

The biggning beginning is a bit hard to read here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rephrased this as follows:

Using IsSeqLike is also required to make your operation work on SeqView values, because SeqView
does not extend Seq. Similarly, there is an IsMapLike type that makes operations work with
both Map and MapView values.

@milessabin
Copy link
Contributor

It might be worth comparing with this ancient blog post complaining about the difficulty of extending collections.

@julienrf
Copy link
Contributor Author

@milessabin I think IsIterableLike does address the complaints of the article you referenced. Or did I miss something?

@julienrf julienrf force-pushed the collections-extension-methods branch from 8ab37ac to 7d1ad95 Compare June 20, 2018 13:52
Copy link
Member

@SethTisue SethTisue left a comment

Choose a reason for hiding this comment

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

in general, this is very good. the structure is good, the examples are good. I've made only minor suggestions

_data/overviews.yml Outdated Show resolved Hide resolved
**Julien Richard-Foy**

This guide shows how to write operations that can be applied to any collection type and return the same
collection type, and how to write operations that can be parameterized by the type of collection to build.
Copy link
Member

Choose a reason for hiding this comment

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

"how to write operations that can be parameterized by the type of collection to build" isn't clear IMO, and doesn't get explained until quite a bit later. if a better wording can't be found, perhaps you could include one brief example of each of the two kinds of operations covered, maybe that's the best way to get the reader to "aha, I see".

_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
a `String` we want to get back another `String`, and so on.

To produce a collection whose type depend on a source collection, we have to use
`scala.collection.BuildFrom` (formerly `CanBuildFrom`) instead of `Factory`.
Copy link
Member

Choose a reason for hiding this comment

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

as always in this collections work, anytime BuildFrom appears, the snarky or skeptical reader will yell (silently, or perhaps on Twitter) "I thought you got rid of CanBuildFrom!

idk, but it might be worth expanding the parenthetical to a separate sentence like: "BuildFrom resembles the old collections API's CanBuildFrom, but appears much more rarely." idk.

Copy link
Member

Choose a reason for hiding this comment

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

it's a bit of a tangent, but I wonder if the naming of Factory and BuildFrom could be more closely aligned with each other, since they are so closely related to each other. maybe it's too late.


## Summary

- To consume any collection, take an `IterableOnce` (or something more specific) as parameter,
Copy link
Member

Choose a reason for hiding this comment

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

IterableOnce or Iterable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be Seq, Set, Map, etc. as well, that’s why I didn’t want to restrict to just IterableOnce and Iterable.

_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
_overviews/core/programming-collections-generically.md Outdated Show resolved Hide resolved
@SethTisue SethTisue self-assigned this Aug 17, 2018
@SethTisue
Copy link
Member

@julienrf interested in returning to this...?

@julienrf
Copy link
Contributor Author

I’m sorry, I forgot to address your feedback. Sure, I will fix that ASAP.

@julienrf julienrf force-pushed the collections-extension-methods branch 2 times, most recently from 82105d2 to 72e0a40 Compare February 1, 2019 12:12
@julienrf
Copy link
Contributor Author

julienrf commented Feb 1, 2019

I updated the PR with most of the feedback addressed. I think there are still remarks that I didn’t really address because I couldn’t find a good solution. I’m happy to take suggestions…

I’ve also re-tested all code examples with the latest Scala-2.13.x version.

@julienrf julienrf force-pushed the collections-extension-methods branch 2 times, most recently from eefb77f to 5f35553 Compare February 1, 2019 12:35
@julienrf julienrf force-pushed the collections-extension-methods branch from 5f35553 to a384367 Compare February 1, 2019 12:41
@SethTisue SethTisue merged commit fd3492c into scala:master Feb 4, 2019
@julienrf julienrf deleted the collections-extension-methods branch February 6, 2019 16:16
@julienrf
Copy link
Contributor Author

I don’t understand why the page is not visible: https://docs.scala-lang.org/overviews/core/custom-collection-operations.html

@SethTisue
Copy link
Member

SethTisue commented Feb 11, 2019

when I generate the site locally (after an hour of wrestling with #1150 and #1286), the page appears.

@jvican are you the keeper of the site build? I don't see anything obviously amiss at https://platform-ci.scala-lang.org/scala/docs.scala-lang ?

@jvican
Copy link
Member

jvican commented Feb 11, 2019

I don't know what's going on, but it doesn't look related to the build. Are we sure the doc here has been added to all the required indices to show up in the overview?

@SethTisue
Copy link
Member

@jvican it shows up when I build the site locally. that's why I suspect some problem in the deploy pipeline

@jvican
Copy link
Member

jvican commented Feb 11, 2019

i doubt it, the site is rebuilt from scratch, no incremental rebuild is enabled... i'm hitting the bed now, but perhaps cloning + pushing a new commit would make the guide show up? if this PR or other PRs merged after this one weren't properly rebased, maybe that could have caused problems?

SethTisue added a commit that referenced this pull request Feb 12, 2019
@SethTisue
Copy link
Member

I pushed a dummy empty commit: c3f1423

the resulting run was https://platform-ci.scala-lang.org/scala/docs.scala-lang/1131

again, everything looks normal in the Drone log. I still think this has to be a problem in the deploy pipeline

@SethTisue
Copy link
Member

@jvican ping. this isn't super urgent, but also, we should try to get it straightened out in time for RC1, which is coming up on us fairly fast

@julienrf
Copy link
Contributor Author

@SethTisue Our great sysadmin @fsalvi fixed the problem!

@SethTisue
Copy link
Member

sweet, thanks Fabien!

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.

5 participants