1

I've defined the following method:

def add(
  prefix: List[String],
  resourceName: String,
  subTree: ResourceTree): ResourceTree = {
  //import ResourceTreeInstances.semigroupInstance
  implicit val dummyInstance = ResourceTreeInstances.semigroupInstance

  // Make a resource tree with a single path.
  def mkSinglePathRT(gPrefix: List[String]): ResourceTree = ???


  this |+| mkSinglePathRT(prefix)
}

As you can see, I need to use the |+| method of Semigroup, defined in the Cats library. I've defined an instance of Semigroup for ResourceTree, however, importing it using the commented import does not work (the |+| operator is not found). Having the dummyInstance above solves the problem, however this does not looks right.

Any idea about why the instance is not brought up by this import?

Community
  • 1
  • 1
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
  • Is `ResourceTreeInstances.semigroupInstance` defined as an implicit? If not, that's your issue. – Travis Brown Sep 21 '16 at 16:51
  • it is. It's definition is given in the linked question. You pointed out there that I missed the result type at the instance. Could that be the problem? – Damian Nadales Sep 21 '16 at 16:52
  • As a side note, assuming that you have a Cats dependency in the project where `ResourceTree` is defined, putting the semigroup instance in the `ResourceTree` companion object would allow you to avoid the import altogether. What you have now is the equivalent of an orphan instance in Haskell—putting the instance in the companion object is like defining it in the same module as the data type in Haskell. – Travis Brown Sep 21 '16 at 16:53
  • I have the instances in the same file as `ResourceTree` under an object named `ResourceTreeInstances`. Does this also make the instances of `ResourceTree` orphan? – Damian Nadales Sep 21 '16 at 16:55
  • Yep. It has to be in the companion object. – Travis Brown Sep 21 '16 at 16:56
  • By the way I can't reproduce your issue with the combined code from the two questions, so it might be a good idea to post a complete example (with all imports). – Travis Brown Sep 21 '16 at 16:58
  • It seems the problem was the lack of a type in the instance. Try removing it. You should see the same error. – Damian Nadales Sep 21 '16 at 16:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123878/discussion-between-damian-nadales-and-travis-brown). – Damian Nadales Sep 21 '16 at 17:00

0 Answers0