Understanding Scala Implicit Conversion and Typical Type Inference

trait LowPriorityOrderingImplicits {
  implicit def ordered[A <: Ordered[A]]: Ordering[A] = new Ordering[A] {
    def compare(x: A, y: A) = x.compare(y)
  }
}

      

What does this code do?

  • What does A <: Ordered [A] mean? How can A be a subtype of ordered [A]?
  • What exactly does "new order [A] {..." do? Does it create a new anonymous class for order [A]?

Also, for this code (taken from DataStax Cassandra Connector )

        object TableWriter{
          def apply[T : RowWriterFactory](
          connector: CassandraConnector,
          keyspaceName: String,
          tableName: String,
          columnNames: ColumnSelector,
          writeConf: WriteConf): TableWriter[T] = {
            ...//some code
            val rowWriter = implicitly[RowWriterFactory[T]].rowWriter(tableDef, selectedColumns)
            new TableWriter[T](connector, tableDef, rowWriter, writeConf)
          }
        }
        val writer = TableWriter(connector, keyspaceName, tableName, columns, writeConf) // first call

        def saveToCassandra(keyspaceName: String,
                          tableName: String,
                          columns: ColumnSelector = AllColumns,
                          writeConf: WriteConf = WriteConf.fromSparkConf(sparkContext.getConf))
                         (implicit connector: CassandraConnector = CassandraConnector(sparkContext.getConf),
                          rwf: RowWriterFactory[T]): Unit = {
             val writer = TableWriter(connector, keyspaceName, tableName, columns, writeConf)// 2nd call
        }

      

  • How is T type determined?
  • What is the purpose of the implicit rwf parameter?
  • What's the difference between the first and second calls to TableWriter?
+3


source to share


1 answer


What does A <: Ordered [A] mean? How can A be a subtype of ordered [A]?

Let's look at the definition Ordered

:

trait Ordered[A] extends Any with java.lang.Comparable[A] {

  def compare(that: A): Int

  /* ... */
}

      

If we say that A <: Ordered[A]

, then we indicate that the instances are A

comparable to other instances A

. Why is this necessary in this case? Well ... let's say we have

implicit def ordered[B, A <: Ordered[B]]: Ordering[A] = new Ordering[A] {
  def compare(x: A, y: A) = x.compare(y)
}

      

This code won't compile because it Ordered[B]

doesn't have a method compare(x: A)

(note that it y

has a type A

).

What exactly does "new order [A] {..." do? Does it create a new anonymous class for order [A]?

Yes, it is an anonymous class.

How is T type determined?



T: RowWriterFactory

is context sensitive. See also Scala doc about tags and type manifests .

As far as I know,

def foo[T: RowWriterFactory](x: Int) 

      

matches the entry

def foo[T](x: Int)(implicit evidence: RowWriterFactory[T])

      

What is the purpose of the rwf implicit parameter?

This follows from the answer to the previous question: this is the context. Although I don't know the usage in this particular example. Edit . Since the apply

-method TableWriter

requires RowWriterFactory

through context binding, target rfw

should be used as an implicit parameter for TableWriter.apply

.

What's the difference between the first and second calls to TableWriter?

Without knowing the arguments of the first call, they are identical, right?

0


source







All Articles