scala-2.13

In scala, how to make type class working for Aux pattern? - Part 2

随声附和 提交于 2021-02-19 03:46:28
问题 This is a follow up question of: In scala, how to make type class working for Aux pattern? Considering the following example: trait Base { type Out def v: Out } object Base { type Aux[T] = Base { type Out = T } type Lt[T] = Base { type Out <: T } class ForH() extends Base { final type Out = HNil override def v: Out = HNil } object ForH extends ForH } trait TypeClasses { class TypeClass[B] def summon[B](b: B)(implicit ev: TypeClass[B]): TypeClass[B] = ev } object T1 extends TypeClasses {

In scala 2.13, why is it sometimes impossible to summon type class explicitly?

牧云@^-^@ 提交于 2021-02-08 04:42:13
问题 Here is a simple example in shapeless 2.3.3: val book = ("author" ->> "Benjamin Pierce") :: ("title" ->> "Types and Programming Languages") :: ("id" ->> 262162091) :: ("price" ->> 44.11) :: HNil val v1 = book.values assert(v1.head == "Benjamin Pierce") // works fine // summoning Values[_] type class explicitly, the HList & TypeTag are optional case class HasValues[T <: HList: TypeTag](v: T) { def vs(implicit v: Values[T]): Values[T] = v } val _vs = HasValues(book).vs val v2 = book.values(_vs)

Difference between size and sizeIs

谁说胖子不能爱 提交于 2021-02-05 05:45:05
问题 What is the semantic difference between size and sizeIs? For example, List(1,2,3).sizeIs > 1 // true List(1,2,3).size > 1 // true Luis mentions in a comment that ...on 2.13+ one can use sizeIs > 1 which will be more efficient than size > 1 as the first one does not compute all the size before returning Add size comparison methods to IterableOps #6950 seems to be the pull request that introduced it. Reading the scaladoc Returns a value class containing operations for comparing the size of this

Is there anyway, in Scala, to get the Singleton type of something from the more general type?

亡梦爱人 提交于 2021-02-04 08:35:24
问题 I have a situation where I'm trying to use implicit resolution on a singleton type. This works perfectly fine if I know that singleton type at compile time: object Main { type SS = String with Singleton trait Entry[S <: SS] { type out val value: out } implicit val e1 = new Entry["S"] { type out = Int val value = 3 } implicit val e2 = new Entry["T"] { type out = String val value = "ABC" } def resolve[X <: SS](s: X)(implicit x: Entry[X]): x.value.type = { x.value } def main(args: Array[String])

In scala, how to make type class working for Aux pattern?

非 Y 不嫁゛ 提交于 2021-01-25 07:18:09
问题 Here is a simple example: trait Base { type Out def v: Out } object Base { type Aux[T] = Base { type Out = T } class ForH() extends Base { type Out = HNil override def v: Out = HNil } object ForH extends ForH } class TypeClass[B] trait TypeClassLevel1 { def summon[B](b: B)(implicit ev: TypeClass[B]): TypeClass[B] = ev } object TypeClass extends TypeClassLevel1 { implicit def t1: TypeClass[Base.Aux[HNil]] = new TypeClass[Base.Aux[HNil]] implicit def t2: TypeClass[Int] = new TypeClass[Int] } it

Getting MatchError when using a placeholder for an unused variable

泪湿孤枕 提交于 2021-01-23 04:49:37
问题 With Scala 2.13.x, I am getting scala.MatchError: null when I use a placeholder for an unused variable: scala> object Test { | val _: Any = null | } object Test scala> Test scala.MatchError: null ... 41 elided But with Scala 2.12.x, I am not getting scala.MatchError: null : scala> object Test { | val _: Any = null | } defined object Test scala> Test res1: Test.type = Test$@784c5ef5 Any reason? 回答1: As stated in scala 2.13 release notes: Underscore is no longer a legal identifier unless

Generate companion object for case class with methods (field = method)

二次信任 提交于 2020-01-15 11:46:06
问题 Generate companion object for case class with scala-macros some code example that i tried, it works i can get list of tuple (name -> type) but how to generate the object in the same scope? import c.universe._ val tpe = weakTypeOf[T] val fields = tpe.decls.collectFirst { case m: MethodSymbol if m.isPrimaryConstructor => m } .get .paramLists .head val extractParams = fields.map { field => val name = field.asTerm.name val fieldName = name.decodedName.toString val NullaryMethodType(fieldType) =

How to pattern match in scala 2.13?

天涯浪子 提交于 2019-12-24 01:17:18
问题 I have the following regex, that I would like to pattern match in Scala 2.13 . The regex: \/brokers\/ids\/\d{1,}$ The following string, that is going to be validate: scala> ("echo dump" #| "nc localhost 32773" #| "grep brokers").!! res2: String = " /brokers/ids/1 " How can I do it in Scala 2.13? 回答1: Scala 2.13 introduced interpolated string patterns, so you could avoid using regex and just do: "/brokers/ids/1" match { case s"/brokers/ids/$ids" => ids //return 1 } 来源: https://stackoverflow

Why am I getting this error when running Scala 2.13 tests in IntelliJ, but not with Scala 2.12?

陌路散爱 提交于 2019-12-18 18:55:12
问题 I am getting the following error when I try to run tests in IntelliJ (2019.1), Scala IntelliJ plugin v2019.1.8, with Scala 2.13: Exception in thread "ScalaTest-dispatcher" java.lang.NoSuchMethodError: scala.collection.JavaConverters.seqAsJavaListConverter(Lscala/collection/Seq;)Lscala/collection/convert/Decorators$AsJava; at org.jetbrains.plugins.scala.testingSupport.scalaTest.treeBuilder.ParallelTreeBuilder.getOrdinalList(ParallelTreeBuilder.java:21) at org.jetbrains.plugins.scala

Missing par method from Scala collections

被刻印的时光 ゝ 提交于 2019-12-01 10:34:03
问题 I tried to convert a sequential list to a parallel one in Intellij, but I get the error Cannot resolve symbol par on the .par method call: import scala.collection.parallel.immutable._ ... val parList = List(1,2,3).par According to https://docs.scala-lang.org/overviews/parallel-collections/overview.html, one must simply invoke the par method on the sequential collection, list. After that, one can use a parallel collection in the same way one would normally use a sequential collection. What