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

Tuple2 implicits do not work with Arrays #661

Open
tovbinm opened this issue Jul 28, 2018 · 9 comments
Open

Tuple2 implicits do not work with Arrays #661

tovbinm opened this issue Jul 28, 2018 · 9 comments

Comments

@tovbinm
Copy link

tovbinm commented Jul 28, 2018

The following code fails to compile:

import com.twitter.algebird.Monoid._
import com.twitter.algebird.Operators._
val m = Map.empty[Int, (Array[Long], Array[Long])]
m + m // fails to compile

unless we explicitly add:

implicit val sgTuple2 = new com.twitter.algebird.Tuple2Semigroup[Array[Long], Array[Long]]()

Tested with Algebird 0.13.4 and Scala 2.11.12

@tovbinm
Copy link
Author

tovbinm commented Jul 28, 2018

In fact, same happens with simple tuples as well:

import com.twitter.algebird.Monoid._
import com.twitter.algebird.Operators._
val x = 1 -> 1
x + x // fails to compile

@johnynek
Copy link
Collaborator

Huh. This is very strange. Can you make a PR with a test to demonstrate the failure.

We use tuples with Semigroups all the time.

Why are you importing Monoid._? I wonder if you are setting up a conflicting implicit and scala just silently fails when two implicits can be found.

@tovbinm
Copy link
Author

tovbinm commented Jul 29, 2018

The Monoid._ is mainly needed because of the ArrayMonoid

@tovbinm
Copy link
Author

tovbinm commented Jul 29, 2018

Here you go - tovbinm@776cfe1

@tovbinm
Copy link
Author

tovbinm commented Jul 29, 2018

Also, I think I found what's the problem - import order matters (at least in the test):
This would fail:

import com.twitter.algebird.Monoid._
import com.twitter.algebird.Operators._

This works:

import com.twitter.algebird.Operators._
import com.twitter.algebird.Monoid._

But in my project import order does not matter, so the problem persists.

@tovbinm
Copy link
Author

tovbinm commented Jul 29, 2018

Here is the failure again in Ammonite:

Welcome to the Ammonite Repl 1.1.0
(Scala 2.12.4 Java 1.8.0_131)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import $ivy.`com.twitter::algebird-core:0.13.4`
import $ivy.$

@ import com.twitter.algebird.Operators._
import com.twitter.algebird.Operators._

@ import com.twitter.algebird.Monoid._
import com.twitter.algebird.Monoid._

@ Array(1,2) -> Array(1,2) + Array(1,2) -> Array(1,2)
cmd3.sc:1: diverging implicit expansion for type algebra.Monoid[(Array[Int], Array[Int])]
starting with method fromAlgebraAdditiveMonoid in trait FromAlgebraMonoidImplicit1
val res3 = Array(1,2) -> Array(1,2) + Array(1,2) -> Array(1,2)
                      ^
cmd3.sc:1: value + is not a member of (Array[Int], Array[Int])
val res3 = Array(1,2) -> Array(1,2) + Array(1,2) -> Array(1,2)
                                    ^
Compilation Failed

@johnynek
Copy link
Collaborator

johnynek commented Jul 29, 2018 via email

@tovbinm
Copy link
Author

tovbinm commented Jul 29, 2018

Well, it assumes String in + operator then:

Welcome to the Ammonite Repl 1.1.0
(Scala 2.12.4 Java 1.8.0_131)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import $ivy.`com.twitter::algebird-core:0.13.4`
import $ivy.$

@ import com.twitter.algebird._
import com.twitter.algebird._

@ import com.twitter.algebird.Operators._
import com.twitter.algebird.Operators._

@ (Array(1,2) -> Array(1,2)) + (Array(1,2) -> Array(1,2))
cmd3.sc:1: type mismatch;
 found   : (Array[Int], Array[Int])
 required: String
val res3 = (Array(1,2) -> Array(1,2)) + (Array(1,2) -> Array(1,2))
                                                    ^
Compilation Failed

@johnynek
Copy link
Collaborator

Honestly it’s not that common to use Operators with Algebird.

I can look into it, but in the meantime I recommend you use Semigroup.plus on the companion.

It is probably an ambiguous implicit somewhere.

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

No branches or pull requests

2 participants