logo
down
shadow

Scala, getting the type parameters of a KList as an HList


Scala, getting the type parameters of a KList as an HList

By : russman
Date : November 14 2020, 04:51 PM
this will help I'm not Miles, but it's possible to accomplish what you're trying to do pretty elegantly with Shapeless's Comapped:
code :
import shapeless._, ops.hlist.Comapped

case class MyOptionList[L <: HList, C <: HList](maybes: L)(
  implicit val comapped: Comapped.Aux[L, Option, C]
) {
  def getOrElse(default: C): C = default // Not a useful implementation
}
scala> val x: Int :: HNil = MyOptionList(Option(1) :: HNil).getOrElse(2 :: HNil)
x: shapeless.::[Int,shapeless.HNil] = 2 :: HNil
case class MyOptionList[L <: HList](maybes: L) {
  def getOrElse[C <: HList: ({ type l[x] = Comapped.Aux[L, Option, x] })#l](
    default: C
  ): C = default
}


Share : facebook icon twitter icon
Are HList/KList suitable as method parameter? How to refer to? Type List?

Are HList/KList suitable as method parameter? How to refer to? Type List?


By : Black Print Ent
Date : March 29 2020, 07:55 AM
help you fix your problem It's not completely clear to me what you're asking for, but your hlistToTypedLink looks like it could be handled using shapeless's HList and polymorphic function values,
code :
scala> import shapeless._ ; import TypeOperators._
import shapeless._
import TypeOperators._

scala> class TypedHandle[T]
defined class TypedHandle

scala> class TypedLink[L <: HList](l : L)
defined class TypedLink

scala> object MkTypedHandle extends (Id ~> TypedHandle) {
     |   def apply[T](t : T) = new TypedHandle[T]
     | }
defined module MkTypedHandle

scala> def hlistToTypedLink[L <: HList, M <: HList](a: L)
     |   (implicit mapper: MapperAux[MkTypedHandle.type, L, M]) =
     |     new TypedLink[M](a map MkTypedHandle)
hlistToTypedLink: [L <: HList, M <: HList](a: L)
  (implicit mapper: MapperAux[MkTypedHandle.type,L,M])TypedLink[M]

scala> hlistToTypedLink(23 :: "foo" :: true :: HNil)
res0: TypedLink[TypedHandle[Int] :: TypedHandle[String] ::
  TypedHandle[Boolean] :: HNil] = TypedLink@51fb5716
Map on HList fails with subtypes of generic type in Scala & Shapeless

Map on HList fails with subtypes of generic type in Scala & Shapeless


By : Manoj Kumar Yadav
Date : March 29 2020, 07:55 AM
hope this fix your issue Your best options are one of @TravisBrown's suggestion to use a view bound,
code :
object f extends Poly1 {
  implicit def caseA[T, S <% A[T]] = at[S]{s => Some(s.x)}
}
object f2 extends Poly1 {
  implicit def caseA[S, T](implicit ev : S <:< A[T]) = at[S]{s => Some(s.x)}
}
object f3 extends Poly1 {
  def asub[T](s: A[T]) = Some(s.x)
  implicit def caseA[T] = at[A[T]](asub)
  implicit def caseB[T] = at[B[T]](asub)
}
Scala, Tupler type back to an HList

Scala, Tupler type back to an HList


By : Matheus Vieira Ferna
Date : March 29 2020, 07:55 AM
I wish this help you If we look at the implementation of the two shapeless functions you are using in toHList :
productTupleOps has a type parameter with Product as upper bound, so you need to specify that TP is a Product. productElements needs an Generic type class instance for the tuple value.
code :
class Tupling[L <: HList, TP <: Product](tupler: Tupler.Aux[L, TP]) {
  def toHList(value: TP)(implicit gen: Generic.Aux[TP, L]): L = 
    shapeless.syntax.std.tuple.productTupleOps(value).productElements
}
import shapeless.syntax.std.tuple._
class Tupling[L <: HList, TP <: Product](tupler: Tupler.Aux[L, TP]) {
  def toHList(value: TP)(implicit gen: Generic.Aux[TP, L]): L = 
    value.productElements
}
Scala doesn't derive dependent type for shapeless HList (using Aux)

Scala doesn't derive dependent type for shapeless HList (using Aux)


By : Imran
Date : March 29 2020, 07:55 AM
seems to work fine The problem is that I explicitly stated the return type of forHList as Converter[Head :: Tail]. That return type is incomplete, and it's missing exactly the part that I needed.
Just changing the declared return type to Converter.Aux[Head :: Tail, HeadConverted :: TailConverted] (or even just leaving it out) fixes the problem.
how to show the type of a HList in scala shapeless

how to show the type of a HList in scala shapeless


By : davidos00
Date : March 29 2020, 07:55 AM
should help you out How do I get the type of a HList as a String so I can print it. eg "Int :: Long :: String :: HNil" , Use shapeless.Typeable:
code :
scala> import shapeless._
import shapeless._

scala> case class A(i: Int, s: String)
defined class A

scala> val gen = Generic[A]
gen: shapeless.Generic[A]{type Repr =
  shapeless.::[Int,shapeless.::[String,shapeless.HNil]]} =
    anon$macro$14$1@56639061

scala> println(Typeable[gen.Repr].describe)
Int :: String :: HNil
Related Posts Related Posts :
  • How to reaload scala application after code change using actors and sbt
  • akka non-blocking BoundedMailbox
  • Why are constructor parameters made into members for case classes?
  • Higher order tail recursive function needs to terminate early
  • How can I reusably filter based on type in Scala?
  • Why does Scala sbt update every time I start it from command prompt?
  • Recommended Scala io library
  • Thread-safely transforming a value in a mutable map
  • Why does chaining match expressions does not compile?
  • Ebean not persisting foreign keys
  • Difference between def m(p: T forSome {type T} and def m1(p:Any), is there any ? Explanation needed based on Scala Langu
  • macro does not find out enclosing vals
  • Spark : how to run spark file from spark shell
  • Flattening a list of lists to a set with exceptions in scala
  • flatMap implementation in Scala
  • Confused about a few lines code in a scala official document page
  • How to input parameters when running bash command with Scala
  • Location header is lost if max-redirects > 1
  • Controller Spec is using FakeApplication, but can't load test configuration
  • Scala code analyzer targets case variable names that are identical to the outer matched varables - "suspicous shado
  • Why does authorize directive execute after the code it's supposed to protect?
  • Scala. Checking if a Type is Comparable
  • Does having a private constructor on a value class negate the benefits of a value class?
  • How to transform submitted json in Play 2.0?
  • Scala warning match may not be exhaustive
  • Pure not a member of objective Promise in PlayFramework
  • How to unmarshal POST params and JSON body in a single route?
  • Spark:How to use join method?
  • is client thread-safe in Twitter Finagle
  • Why is the method accepts only one argument?
  • Scala Play 2.3 Working with gCloud Storage - any libs to go async?
  • spray.io strange get/delete/detach directives behavior
  • SBT cannot resolve class declared in src/main/scala in a src/test/scala test class
  • Scala typeclass without function argument
  • Configuring actor behavior using typesafe Config and HOCON
  • Scalatra: Migrating Jersey Filters to Scalatra
  • Compilation error when using Scaldi
  • Scalac hanging in phase typer
  • how to have different source code when cross-compiling Scala with sbt? (changes in MurmurHash)
  • How to set different scalacOptions per Scala version when cross-compiling using Build.scala?
  • Possible Bug in JDBC?
  • Is there a Scala compiler flag to warn when tail recursion is applied without annotation?
  • scala case class put methods in companion object?
  • multiproject sbt doesn't generate file structure
  • Scala "multilevel" abstract class / abstract objects replacement
  • Why does Play refuse form reporting "Cannot resolve method apply with such signature: Missing arguments"?
  • How to split string with trailing empty strings in result?
  • Scala group by list of list and subtracts grouped values
  • Scala - Creating a function to produce Unary string or integer values
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org