[SOLVED] Objects implementing traits

I have there that problem where a Companion Object extends a trait and it appears that this object is not able to resolve the trait’s (implemented) method.

Due to company policy, I am not able to post the original code. When I tried to reproduce the issue with a clean sample I was not able to.

I don’t think that there is an issue with visibility since I created the trait in the same package with the “Extract trait” feature of IntelliJ Scala.

So my - I know very vague - the question is: are there circumstances where a (companion) object is not able to resolve the methods of an implemented trait?

Hmm. My instinct is to say “no”, but more details might be useful. What do you mean “not able to resolve”? What error are you getting? I suspect that this is some other error, and the companion object aspect is a red herring, but that’s just a guess…

I get the same error as if I called a method that does not exist.

Really hard to say with so little information, but like I said – I suspect the companion-object aspect isn’t actually the problem. A companion object is mostly just an object like any other: the fact that it’s a companion generally shouldn’t change the ability to call inherited methods.

While I could be wrong, I would look for problems of visibility, spelling (many problems like this turn out to be typos), incorrect parameter types, or something like that – problems specific to the function call, not having to do with the companion-ness.

1 Like

You mean you get a NoSuchMethodError?

If your IDE didn’t detect that the method you are calling does not exist, it usually means that there are two different versions of the code, one with the method and one without, and your IDE sees the version with the method, but you are running with the version without the method.

If you just recently added the method, maybe it failed to rebuild fully. Any compile errors?

Otherwise, probably there is something funny going on with your classpath.

I now had the time to check the compiler error:
Error:(10, 56) illegal cyclic reference involving object Store import com.example.store.Store.{StoreError, _}

That happens if I extract the method to a trait in the same package. Unfortunately I have no idea what the compiler wants to tell me here.

Interesting. Do you have such an import in the file? What happens if you comment it out? What happens if you replace the _ with more-detailed imports?

What this is saying, I think, is that something is trying to import itself, or maybe a part of itself. I suspect that the trait and the object depend on each other, to a degree that just isn’t possible…

Do you get the same errore when you compile your code using sbt directly on a command prompt (not using the IDE)?

I believe that solves the mystery: When I started to extract the trait I did not realize that the StoreError was not moved to the new trait. So the object imported the trait to be mixed in and the trait imported the object to know the StoreError type.

When I moved the StoreError to the trait everything worked fine!

Thank you for putting my nose on that one :slight_smile:

1 Like