Skip to content

Rewrite Map.zip -> Map.zip.toMap #62

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

Closed
wants to merge 3 commits into from

Conversation

MasseGuillaume
Copy link
Contributor

No description provided.

@@ -66,6 +66,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
)

val mapZip =
SymbolMatcher.exact(
Symbol("_root_.scala.collection.IterableLike#zip(Lscala/collection/GenIterable;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like it’s going to match any call to zip, even if the receiver is not of type Map[_, _].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

then we are blocked by scalameta/scalameta#1212

@MasseGuillaume MasseGuillaume force-pushed the map-zip branch 3 times, most recently from 1e993e1 to eec6b8a Compare June 25, 2018 14:12
final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
def unapply(tree: Tree): Boolean = {
index.denotation(tree)
.map(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
Copy link
Contributor

Choose a reason for hiding this comment

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

What are names? Why take the headOption of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julienrf wizardry approved by @olafurpg

Copy link
Contributor

Choose a reason for hiding this comment

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

I’m sorry but I’d like to have more information about what it does.

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's in the commit message: ae843d4

Copy link
Contributor

Choose a reason for hiding this comment

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

That does not really answer my question. Why do we use names.headOption.exists rather than names.exists?

Copy link
Contributor Author

@MasseGuillaume MasseGuillaume Jun 25, 2018

Choose a reason for hiding this comment

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

for Set[Int] it would be:

1) scala.collection.immutable.Set
2) Int

we take the head.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, so if I write val xs = HashMap(1 -> "foo") and then xs.zip(…), the denotation for xs will contain Seq("scala.collection.immutable.HashMap", "Int", "String")? And then it won’t match with the types you wrote when you defined CollectionMap.

Is it possible to know all the ancestors of the type symbol?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave a comment in the source code that this is totally hacky and is only used to unblock writing further tests as a temporary workaround while Term.tpe is not supported.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to know all the ancestors of the type symbol?

v0.6 has support for that but not v0.5

Copy link
Contributor

Choose a reason for hiding this comment

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

OK!

def unapply(tree: Tree): Boolean = {
index.denotation(tree)
.map(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
.getOrElse(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

.map(p).getOrElse(false) can be replaced with .exists(p)

new TypeMatcher(symbols: _*)(index)
}

final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind adding some documentation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please, in particular highlight in caps lock that this is not The Real Deal. This is only to unblock tests, I would in fact prefer if this was not merged into master because users should not run this.

@MasseGuillaume
Copy link
Contributor Author

@julienrf ready for review

val CollectionMap: TypeMatcher = TypeMatcher(
Symbol("_root_.scala.collection.immutable.Map#"),
Symbol("_root_.scala.collection.mutable.Map#"),
Symbol("_root_.scala.Predef.Map#")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add HashMap and TreeMap as well?

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 would rather not dig that hole too deep. The idea is to document what is the expected type, not to have a working rule.

@julienrf
Copy link
Contributor

What do you think of merging this as an “Experimental” rule separate from “NewCollections”?

@MasseGuillaume
Copy link
Contributor Author

Yes, let's do this.

@MasseGuillaume MasseGuillaume mentioned this pull request Jun 27, 2018
martijnhoekstra pushed a commit to martijnhoekstra/scala-collection-compat that referenced this pull request Nov 9, 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