Scala - Trying to wrap occurrences of booleans using implicits

39 Views Asked by At

I'm new to implicits in scala and am sure there is a way to do this but have tried everything I can think of.

I have a trait:

trait WrappedDataframe[T] extends Rep[DataFrame]


 def apply[S](colName: String): Col[T] = {
    // Functionality to get our Col[T]
 }

This is Rep my base representation which also contains the function to use equality:

trait Rep[T] {
 def === (e2: Rep[T]): Rep[T] = Equality[T](this, e2)
}

For Col is a trait that extends Rep like so:

trait Col[T] extends Rep[T]
case class BaseCol[T](df: String, n: String) extends Col[T]
class CompCol[T](val lhs: Rep[T], val rhs: Rep[T]) extends Col[T] with Serializable
case class Equality[T](override val lhs: Rep[T], override val rhs: Rep[T]) extends CompCol[T](lhs, rhs)

and I've wrapped a DataFrame:

val wrappedIntDf = intDf.wrap()

So with this I can use similar syntax to spark to do comparisons on Reps. In my below examples h1 is working fine but h2 has the noted error.

val h1: Rep[DataFrame] = wrappedIntDf("users") === wrappedIntDf("users2")
val h2: Rep[DataFrame] = wrappedIntDf("users") === true // Type mismatch. Required: Rep[DataFrame], found: Boolean

Obviously this is because it is just a Boolean. But I really want to be able to use the above syntax so I looked into implicits and came up with:

implicit def booleanToRep(b: Boolean): Rep[DataFrame] = Literal(b)
case class Literal[DataFrame](bool: Boolean) extends Rep[DataFrame]

I've added this to the scope of my Rep trait above. Nothing changes in terms of my example that I want to work. Can anyone see what might be wrong with my approach?

0

There are 0 best solutions below