Scala - how to create a single implicit that can be used for a type constructor

rmin

I'm trying to write a method which uses the isEmpty method on types String, Option and List. These classes don't share a common base trait with that method, so I've tried to pass an implicit EmptyChecker in with them:

  trait EmptyChecker[Field] {
    def isEmpty(data: Field): Boolean
  }

  implicit val StringEmptyChecker: EmptyChecker[String] = new EmptyChecker[String] {
    def isEmpty(string: String): Boolean = string.isEmpty
  }

  def printEmptiness[Field](field: Field)(implicit emptyChecker: EmptyChecker[Field]): Unit = {
    if (emptyChecker.isEmpty(field))
      println("Empty")
    else
      println("Not empty")
  }

  printEmptiness("abc") // Works fine

The String empty checker works fine, but I've hit problems with making empty checkers for type constructors like Option and List.

For example, Option doesn't work:

  implicit val OptionChecker: EmptyChecker[Option[_]] = new EmptyChecker[Option[_]] {
    def isEmpty(option: Option[_]): Boolean = option.isEmpty
  }

  // Both fail compilation: "could not find implicit value for parameter emptyChecker: EmptyChecker[Some[Int]]
  printEmptiness(Some(3))
  printEmptiness[Option[Int]](Some(3))      

If I use a specific Option[Int] checker, it works a little better, but is a bit ugly:

  implicit val OptionIntChecker: EmptyChecker[Option[Int]] = new EmptyChecker[Option[Int]] {
    def isEmpty(optionInt: Option[Int]): Boolean = optionInt.isEmpty
  }

  // Fails like above:
  printEmptiness(Some(3))

  // Passes compilation:
  printEmptiness[Option[Int]](Some(3))

So my question is: is it possible to make a single EmptyChecker for each Option and List type and have them work with my method without needing to explicitly declare the type whenever I call it? I'm trying to get a type safe duck typing effect.

I'm using scala 2.11.6.

Thanks in advance!

Hugh

The source of your problem is that the type of Some(1) is Some[Int], not Option[Int]. There are a couple of ways around this; you can explicitly upcast the expression with a type ascription: printEmptiness(Some(3): Option[Int]). Alternatively, you can define a helper method to do this for you automatically, and if you're using Scalaz, there's one of these provided:

import scalaz.syntax.std.option._
printEmptiness(3.some)

Furthermore if you do use Scalaz, you may find looking at the PlusEmpty/ApplicativePlus/MonadPlus type classes useful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scala - how to create a single implicit that can be used for a type constructor

From Dev

How to create a prototype for this type of constructor?

From Dev

How to create a prototype for this type of constructor?

From Dev

How can I create a custom column type with Typesafe Slick in Scala?

From Dev

Java: How can i solve the Error: "implicit super constructor is undefined"

From Dev

scala type classes implicit ambiguity

From Dev

Scala nested implicit type parameters

From Dev

Implicit abstract class constructor parameter and inheritance in Scala

From Java

How can I pass a Scala UserDefinedFunction where output is a complex type (using StructType and StructField) to be used from Pyspark

From Dev

Scala and Play: how can I use my implicit object?

From Dev

Scala Inheritance of Type Constructor

From Dev

Can I tell scala how to prefer more specific implicit, rather than give "ambiguous implicit" error?

From Dev

Using Parametric Type in Implicit for Type Classes in Scala

From Dev

C++ constructor implicit type conversion

From Dev

Create akka actor that has implicit in constructor

From Dev

How can I create a constexpr function that returns a type (to be used in a template parameter)

From Dev

How can I create a constexpr function that returns a type (to be used in a template parameter)

From Dev

How long can the name of a type constructor be?

From Dev

How long can the name of a type constructor be?

From Dev

How can I create implicit Intent for file in Android

From Dev

Can reduceBykey be used to change type and combine values - Scala Spark?

From Dev

Can reduceBykey be used to change type and combine values - Scala Spark?

From Dev

Can Akka (Scala) be used to create asynchronous persistent server?

From Dev

widening conversion not implicit when used with 'object' type?

From Dev

How can I create a generic list monoid in scala that maintains the inner type of the list involved?

From Dev

How can one create a method in scala that generates the sqlcontext implicits encoder based only on type?

From Dev

Can't create instance of generated type: No parameterless constructor defined for this object

From Dev

How to create object/singleton of generic type in Scala?

From Java

How to create param for superclass constructor without early initialiser in Scala

Related Related

  1. 1

    Scala - how to create a single implicit that can be used for a type constructor

  2. 2

    How to create a prototype for this type of constructor?

  3. 3

    How to create a prototype for this type of constructor?

  4. 4

    How can I create a custom column type with Typesafe Slick in Scala?

  5. 5

    Java: How can i solve the Error: "implicit super constructor is undefined"

  6. 6

    scala type classes implicit ambiguity

  7. 7

    Scala nested implicit type parameters

  8. 8

    Implicit abstract class constructor parameter and inheritance in Scala

  9. 9

    How can I pass a Scala UserDefinedFunction where output is a complex type (using StructType and StructField) to be used from Pyspark

  10. 10

    Scala and Play: how can I use my implicit object?

  11. 11

    Scala Inheritance of Type Constructor

  12. 12

    Can I tell scala how to prefer more specific implicit, rather than give "ambiguous implicit" error?

  13. 13

    Using Parametric Type in Implicit for Type Classes in Scala

  14. 14

    C++ constructor implicit type conversion

  15. 15

    Create akka actor that has implicit in constructor

  16. 16

    How can I create a constexpr function that returns a type (to be used in a template parameter)

  17. 17

    How can I create a constexpr function that returns a type (to be used in a template parameter)

  18. 18

    How long can the name of a type constructor be?

  19. 19

    How long can the name of a type constructor be?

  20. 20

    How can I create implicit Intent for file in Android

  21. 21

    Can reduceBykey be used to change type and combine values - Scala Spark?

  22. 22

    Can reduceBykey be used to change type and combine values - Scala Spark?

  23. 23

    Can Akka (Scala) be used to create asynchronous persistent server?

  24. 24

    widening conversion not implicit when used with 'object' type?

  25. 25

    How can I create a generic list monoid in scala that maintains the inner type of the list involved?

  26. 26

    How can one create a method in scala that generates the sqlcontext implicits encoder based only on type?

  27. 27

    Can't create instance of generated type: No parameterless constructor defined for this object

  28. 28

    How to create object/singleton of generic type in Scala?

  29. 29

    How to create param for superclass constructor without early initialiser in Scala

HotTag

Archive