Quantcast

Eta-expansion of zero-argument method values deprecation and implicits / type parameters

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Eta-expansion of zero-argument method values deprecation and implicits / type parameters

Michael Thaler
Hi,

here is a (rather contrived) example of using implicits that cause eta-expansion of zero-argument method values deprecation warnings in Scala 2.12:

object Implicits {
trait Show[T] {
def show(value: T): String
}

implicit val showString = new Show[String] {
override def show(value: String): String = value
}

class ConsoleShow[T](show: Show[T]) {

def show(value: T): Unit = {
println(show(value))
}
}

implicit def makeConsoleShow[T]()(implicit show: Show[T]) = new ConsoleShow[T](show)
}

object Main extends App {

import Implicits._

show("test")

def show[T](value: T)(implicit f: () => ConsoleShow[T]): Unit = {
f().show(value)
}
}

I get the following warning when compiling the code:

[warn] /home/michael/etaexpansion/src/main/scala/etaexpansion/Main.scala:7: Eta-expansion of zero-argument method values is deprecated. Did you intend to write Implicits.makeConsoleShow[String]()?
[warn]     show("test")
[warn]         ^
[warn] one warning found

Without type parameters it would be straight-forward to solve this:

implict def makeA(): newA

could be replaced by

implicit val makeA: () => A = () => new A

But how can I get rid of the eta-expansion deprecation warnings in the above case?

Thanks,
Michael

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Eta-expansion of zero-argument method values deprecation and implicits / type parameters

Jasper-M
Hi,

The usual way to do this would be:

object Implicits {
  trait Show[T] {
    def show(value: T): String
  }

  implicit val showString = new Show[String] {
    override def show(value: String): String = value
  }

  class ConsoleShow[T](show: Show[T]) {

    def show(value: T): Unit = {
      println(show(value))
    }
  }

  implicit def makeConsoleShow[T](implicit show: Show[T]) = new ConsoleShow[T](show)
}

object Main extends App {

  import Implicits._

  show("test")

  def show[T](value: T)(implicit f: ConsoleShow[T]): Unit = {
      f.show(value)
  }
}

If f being a 0 argument function is really important to you, then:

object Implicits {
  trait Show[T] {
    def show(value: T): String
  }

  implicit val showString = new Show[String] {
    override def show(value: String): String = value
  }

  class ConsoleShow[T](show: Show[T]) {

    def show(value: T): Unit = {
      println(show(value))
    }
  }

  implicit def makeConsoleShow[T](implicit show: Show[T]) = () => new ConsoleShow[T](show)
}

object Main extends App {

  import Implicits._

  show("test")

  def show[T](value: T)(implicit f: () => ConsoleShow[T]): Unit = {
      f().show(value)
  }
}

Kind regards,
Jasper

Op woensdag 4 januari 2017 10:35:19 UTC+1 schreef Michael Thaler:
Hi,

here is a (rather contrived) example of using implicits that cause eta-expansion of zero-argument method values deprecation warnings in Scala 2.12:

object Implicits {
trait Show[T] {
def show(value: T): String
}

implicit val showString = new Show[String] {
override def show(value: String): String = value
}

class ConsoleShow[T](show: Show[T]) {

def show(value: T): Unit = {
println(show(value))
}
}

implicit def makeConsoleShow[T]()(implicit show: Show[T]) = new ConsoleShow[T](show)
}

object Main extends App {

import Implicits._

show("test")

def show[T](value: T)(implicit f: () => ConsoleShow[T]): Unit = {
f().show(value)
}
}

I get the following warning when compiling the code:

[warn] /home/michael/etaexpansion/src/main/scala/etaexpansion/Main.scala:7: Eta-expansion of zero-argument method values is deprecated. Did you intend to write Implicits.makeConsoleShow[String]()?
[warn]     show("test")
[warn]         ^
[warn] one warning found

Without type parameters it would be straight-forward to solve this:

implict def makeA(): newA

could be replaced by

implicit val makeA: () => A = () => new A

But how can I get rid of the eta-expansion deprecation warnings in the above case?

Thanks,
Michael

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Eta-expansion of zero-argument method values deprecation and implicits / type parameters

Michael Thaler
Thanks Jasper,

that was actually quite easy,

Best regards,
Michael


On Wednesday, January 4, 2017 at 11:10:51 AM UTC+1, Jasper-M wrote:
Hi,

The usual way to do this would be:

object Implicits {
  trait Show[T] {
    def show(value: T): String
  }

  implicit val showString = new Show[String] {
    override def show(value: String): String = value
  }

  class ConsoleShow[T](show: Show[T]) {

    def show(value: T): Unit = {
      println(show(value))
    }
  }

  implicit def makeConsoleShow[T](implicit show: Show[T]) = new ConsoleShow[T](show)
}

object Main extends App {

  import Implicits._

  show("test")

  def show[T](value: T)(implicit f: ConsoleShow[T]): Unit = {
      f.show(value)
  }
}

If f being a 0 argument function is really important to you, then:

object Implicits {
  trait Show[T] {
    def show(value: T): String
  }

  implicit val showString = new Show[String] {
    override def show(value: String): String = value
  }

  class ConsoleShow[T](show: Show[T]) {

    def show(value: T): Unit = {
      println(show(value))
    }
  }

  implicit def makeConsoleShow[T](implicit show: Show[T]) = () => new ConsoleShow[T](show)
}

object Main extends App {

  import Implicits._

  show("test")

  def show[T](value: T)(implicit f: () => ConsoleShow[T]): Unit = {
      f().show(value)
  }
}

Kind regards,
Jasper

Op woensdag 4 januari 2017 10:35:19 UTC+1 schreef Michael Thaler:
Hi,

here is a (rather contrived) example of using implicits that cause eta-expansion of zero-argument method values deprecation warnings in Scala 2.12:

object Implicits {
trait Show[T] {
def show(value: T): String
}

implicit val showString = new Show[String] {
override def show(value: String): String = value
}

class ConsoleShow[T](show: Show[T]) {

def show(value: T): Unit = {
println(show(value))
}
}

implicit def makeConsoleShow[T]()(implicit show: Show[T]) = new ConsoleShow[T](show)
}

object Main extends App {

import Implicits._

show("test")

def show[T](value: T)(implicit f: () => ConsoleShow[T]): Unit = {
f().show(value)
}
}

I get the following warning when compiling the code:

[warn] /home/michael/etaexpansion/src/main/scala/etaexpansion/Main.scala:7: Eta-expansion of zero-argument method values is deprecated. Did you intend to write Implicits.makeConsoleShow[String]()?
[warn]     show("test")
[warn]         ^
[warn] one warning found

Without type parameters it would be straight-forward to solve this:

implict def makeA(): newA

could be replaced by

implicit val makeA: () => A = () => new A

But how can I get rid of the eta-expansion deprecation warnings in the above case?

Thanks,
Michael

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...