Reputation: 461
I am trying to understand the implicit method in Scala, in the SlashSyntax.scala I defined a customed operator method:
package com.example.hello.impl
object SlashSyntax {
final class IntOps(private val intSrc: Int) {
def \(a2: Int): Int = (intSrc.toString + a2.toString).toInt
}
}
trait SlashSyntax {
import SlashSyntax._
implicit def intOps(src: Int) = new IntOps(src)
}
then in the main method I try to use the operator but it says cannot resolve symbol :
object Test {
def main(args: Array[String]): Unit = {
import com.example.hello.impl.SlashSyntax._
val s = 10 \ 2 //cannot resolve symbol \
}
}
How can I use my own operator?
Upvotes: 0
Views: 122
Reputation: 51271
The simplest solution is to make the IntOps
class an implicit class
. Then
SlashSyntax
trait (or you can use it for other things)implicit conversion method ...
warning goes awayUpvotes: 2
Reputation: 3390
The issue caused by incorrect import.
import com.example.hello.impl.SlashSyntax._
Problem here is that you imported all members from object SlashSyntax
instead of trait SlashSyntax
. This happened because scala considered com.example.hello.impl.SlashSyntax
as path. All paths can be composed only from Stable members
. Traits are not Stable members
but objects
are stable members, so scala imported members from object SlashSyntax
.
You can just move your implicit method to object SlashSyntax
and this will fix the issue.
Upvotes: 1
Reputation: 27535
Your object SlashSyntax
doesn't extends trait SlashSyntax
so your extension methods are not seen when you do SlashSyntax._
.
Additionally you didn't do import scala.languageFeatures.implicitConversions
. though you might have added -language:implicitConversions
flag to scalac.
Sometimes lack of type annotation break things. Not in your case but it's good in general to these things there.
This works:
object SlashSyntax extends SlashSyntax {
final class IntOps(private val intSrc: Int) {
def \(a2: Int): Int = (intSrc.toString + a2.toString).toInt
}
}
trait SlashSyntax {
import SlashSyntax.IntOps
implicit def intOps(src: Int): IntOps = new IntOps(src)
}
Upvotes: 1