implicit

NullPointerException on implicit resolution

ⅰ亾dé卋堺 提交于 2020-06-17 02:04:07
问题 This code results in NullPointerException: import anotherpackage.MyContext import anotherpackage.builders.aMyContext case class Context(id: String) object Context { implicit def `ContextHolder to Context`(implicit holder: ContextHolder): Context = holder.context } trait ContextHolder { def context: Context } object anotherpackage { case class MyContext(name: String, context: Context) extends ContextHolder object builders { def aMyContext(name: String)(implicit context: Context = Context("test

How does given keyword work in Scala 3 or dotty?

試著忘記壹切 提交于 2020-06-16 11:22:12
问题 I was going through Scala 3 documentation. They have introduced given keyword which is considered as the alternative of Scala 2 implicit . The code is here trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] { def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match { case

How does given keyword work in Scala 3 or dotty?

白昼怎懂夜的黑 提交于 2020-06-16 11:21:42
问题 I was going through Scala 3 documentation. They have introduced given keyword which is considered as the alternative of Scala 2 implicit . The code is here trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] { def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match { case

How to use given in Dotty?

依然范特西╮ 提交于 2020-06-12 07:35:25
问题 I was looking at Dotty docs under Contextual Abstractions page and I saw the Given Instances . Given instances (or, simply, "givens") define "canonical" values of certain types that serve for synthesizing arguments to given clauses. Example: trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T])

Conditional Implicit Definitions Scala [duplicate]

你说的曾经没有我的故事 提交于 2020-05-17 05:51:10
问题 This question already has answers here : Understanding implicit in Scala (7 answers) Closed last month . I have to solve this quinz but i can't find the correct answer. trait Physics { implicit def air: Gaz, implicit def condense(implicit gaz: Gaz): Liquid, implicit def freeze(implicit liquid: Liquid): Solid implicitly[Solid] } Can you rewrite the last line with the inferred parameter explicitly written? Hint: It should look like implicitly[Solid](... Thank you so much! 回答1: Here is a hint:

When using the singleton type feature of Scala shapeless, how to force the compiler to use narrow/singleton type as an implicit parameter?

只愿长相守 提交于 2020-05-14 07:22:05
问题 This is a follow-up of one of my previous questions: In scala shapeless, is it possible to use literal type as a generic type parameter? I'm trying to write scala code for vector multiplication, while using shapeless to make the compiler aware of the dimension of each vector: trait IntTypeMagnet[W <: Witness.Lt[Int]] extends Serializable { def witness: W } object IntTypeMagnet { case class Impl[W <: Witness.Lt[Int]](witness: W) extends IntTypeMagnet[W] {} implicit def fromInt[W <: Int](v: W):

When using the singleton type feature of Scala shapeless, how to force the compiler to use narrow/singleton type as an implicit parameter?

余生颓废 提交于 2020-05-14 07:21:44
问题 This is a follow-up of one of my previous questions: In scala shapeless, is it possible to use literal type as a generic type parameter? I'm trying to write scala code for vector multiplication, while using shapeless to make the compiler aware of the dimension of each vector: trait IntTypeMagnet[W <: Witness.Lt[Int]] extends Serializable { def witness: W } object IntTypeMagnet { case class Impl[W <: Witness.Lt[Int]](witness: W) extends IntTypeMagnet[W] {} implicit def fromInt[W <: Int](v: W):

Is there a way to define multiple implicit evidences via a single HList?

Deadly 提交于 2020-05-13 19:02:26
问题 I have a piece of code, conceptually similar to the following one: //library code trait Support[K, V] def partialHandler[K, V](key: K, value: V)(implicit ev: Support[K, V]) = ??? //user code implicit val intIntSupport = new Support[Int, Int] {} implicit val intStringSupport = new Support[Int, String] {} ... partialHandler(1, "foo) partialHandler(1, 1) I wonder if there is a way to let users of this library define supported (K, V) types more elegantly, e.g.: val supportedTypes = new Support

Scala学习之字符串篇(七):使用隐式方法来扩展字符串类

為{幸葍}努か 提交于 2020-04-09 11:10:42
通常情况下我们会把一些共用的字符串处理方法封装到一个工具类中,比如 StringUtils 类。假定我们实现了一个方法 increment ,它接收一个字符串,然后把字符串中的每一个字符加1,然后返回新的字符串。要使用这个方法我们需要调用: StringUtils.increment(s) 。 但是Scala为我们提供了一种更加灵活的方式-隐式方法,它可以直接让你以" s.incrment "的方式进行调用,就好像 String 类为我们提供了这个方法一样。 scala> implicit class StringUtils(s: String) { | def increment = s.map(c => (c + 1).toChar) | def decrement = s.map(c => (c - 1).toChar) | def hideAll = s.replaceAll(".", "*") | } defined class StringUtils scala> "HAL".increment res28: String = IBM scala> "IBM".decrement res29: String = HAL scala> "IBM".hideAll res30: String = *** 来源: oschina 链接: https://my.oschina

In scala 2 or 3, is it possible to debug implicit resolution process in runtime?

偶尔善良 提交于 2020-02-23 06:27:51
问题 In scala language, implicit resolution is often done in compile-time and sometimes throws obfuscating error information, one famous example of such error is when shapeless Generic throws error information like: error: could not find implicit value for parameter encoder: CsvEncoder[Foo] (see https://books.underscore.io/shapeless-guide/shapeless-guide.html for detail) A solution to this problem is to run implicit resolution algorithm (should be a graph query algorithm internally) in runtime,