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

Fix: Rename coll to iter for consistency in lazyMap examples #3139

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

Conversation

blank71
Copy link

@blank71 blank71 commented Jan 4, 2025

https://docs.scala-lang.org/overviews/collections-2.13/views.html

fix(docs): Rename variable iter to coll for consistency with documentation

Replaced variable name iter with coll in the code examples to align with the surrounding documentation and improve clarity.

@blank71
Copy link
Author

blank71 commented Jan 4, 2025

I'm new to scala and I found this.

But I found the problem below. Maybe I need to fix document except code. Fix coll to iter. At least I think it should avoid using coll.

% scala-cli version
Scala CLI version: 1.5.1
Scala version (default): 3.5.1
Your Scala CLI version is outdated. The newest version is 1.5.4
It is recommended that you update Scala CLI through the same tool or method you used for its initial installation for avoiding the creation of outdated duplicates.
scala> def lazyMap[T, U](coll: Iterable[T], f: T => U) = new Iterable[U]:
     |   def iterator = coll.iterator map f
     |
-- [E049] Reference Error: -----------------------------------------------------
2 |  def iterator = coll.iterator map f
  |                 ^^^^
  |Reference to coll is ambiguous.
  |It is both defined in method lazyMap
  |and inherited subsequently in anonymous class Object with Iterable[U] {...}
  |
  | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: -------------------------------------------------
2 |  def iterator = coll.iterator map f
  |                                   ^
  |                                   Found:    (f : T => U)
  |                                   Required: U => U
  |
  | longer explanation available when compiling with `-explain`
2 errors found

scala> def lazyMap[T, U](iter: Iterable[T], f: T => U) = new Iterable[U]:
     |   def iterator = iter.iterator map f
     |
def lazyMap[T, U](iter: Iterable[T], f: T => U): Iterable[U]

@blank71 blank71 changed the title Fix: Rename iter to coll for consistency in lazyMap examples Fix: Rename coll to iter for consistency in lazyMap examples Jan 4, 2025
@SethTisue
Copy link
Member

SethTisue commented Jan 7, 2025

What's wrong with coll?

I can see the desire to shorten Iterable to iter, but personally I feel like iter is so strongly associated with Iterator that coll might actually be better here. There isn't any Collection type in the collections API, but Seq and Set and Map are all subtypes of Iterable.

I do see there is one existing use of iter: Iterable but that one precedent doesn't necessarily bind us.

For whatever it's worth, the source code consistently uses coll: Iterable. I've often been immersed in that source code, so that's probably part of where my impression comes from that coll: Iterable is fine and may even be preferable.

@blank71
Copy link
Author

blank71 commented Jan 7, 2025

Thanks!!!

The first problem is that the code uses iter while the description uses coll, causing a discrepancy. This is I want to fix. It's no problem for me with either changing the code or the description.

Some try, I changed the code from iter to coll. The old documentation used coll. Found the problem as described above. It reads like the problem is caused by an ambiguity between the coll argument and the coll that Iterable has.

I check the implementation of Iterable in Iterable.scala file.

trait Iterable[+A] extends IterableOnce[A]
  with IterableOps[A, Iterable, Iterable[A]]
  with IterableFactoryDefaults[A, Iterable] {
  ...
  final protected def coll: this.type = this
  ...
}

All of this leads me to think that the use of coll should be avoided. Please let me know if there is another variable name that would work best instead of iter. Or is it possible to avoid the problem while keeping the use of coll?

@SethTisue SethTisue self-assigned this Jan 8, 2025
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.

2 participants