score:14
Accepted answer
you can define your own type class to gather the evidence that the record has the fields you need:
import shapeless._, ops.record.selector, record._, syntax.singleton._
val w1 = witness("foo1")
val w2 = witness("foo2")
val w3 = witness("foo3")
case class hasmyfields[l <: hlist](implicit
s1: selector[l, w1.t, string],
s2: selector[l, w2.t, int],
s3: selector[l, w3.t, double]
)
object hasmyfields {
implicit def make[l <: hlist](implicit
s1: selector[l, w1.t, string],
s2: selector[l, w2.t, int],
s3: selector[l, w3.t, double]
) = hasmyfields[l]
}
and then, for example:
def fun1[l <: hlist](xs: l)(implicit selectors: hasmyfields[l]) = {
import selectors._
(xs("foo1"), xs("foo2"), xs("foo3"))
}
it's still a little verbose, especially since the import is necessary, but much less so than requiring all of the selectors individually as implicit parameters.
score:4
the out type of a given field can be specified using:
selector[l, w1.t] { type out = string }
also, we can slightly simplify the syntax using a type constructor:
import shapeless._, ops.record.selector, record._, syntax.singleton._
val w1 = witness("foo1")
val w2 = witness("foo2")
val w3 = witness("foo3")
type hasfoo1[l <: hlist] = selector[l, w1.t] { type out = string }
type hasfoo2[l <: hlist] = selector[l, w2.t]
type hasfoo3[l <: hlist] = selector[l, w3.t]
@implicitnotfound("${l} should have foo1, foo2 and foo3")
case class hasmyfields[l <: hlist](implicit s1: hasfoo1[l], s2: hasfoo2[l], s3: hasfoo3[l])
object hasmyfields {
implicit def make[l <: hlist : hasfoo1 : hasfoo2 : hasfoo3] = hasmyfields[l]
}
def fun1[l <: hlist : hasmyfields](xs: l) = {
val selectors = implicitly[hasmyfields[l]]
import selectors._
(xs("foo1").length, xs("foo2"), xs("foo3"))
}
fun1(("foo1"->> "hello") :: ("foo2" ->> 1)::("foo3" ->> 1.2)::hnil)
// does not compile: the value in foo1 is not a string
fun1(("foo1"->> 2) :: ("foo2" ->> 1)::("foo3" ->> 1.2)::hnil)
Source: stackoverflow.com
Related Query
- Passing a Shapeless Extensible Record to a Function
- Passing a Shapeless Extensible Record to a Function (continued)
- Passing a shapeless extensible record to a function (never ending story?
- Dynamically create extensible record in shapeless 2.0
- Map[String,Object] from a DB (or Key Value store) to a Shapeless Extensible Record
- Passing function as block of code between curly braces
- Passing elements of a List as parameters to a function with variable arguments
- Scala Passing Function with Argument
- Mapping over Shapeless record
- scala, Passing an operator as argument of a function
- Issue resolving arity of function args to drive list processing, using Shapeless
- Spark / Scala: Passing RDD to Function
- Function implicit parameters not any more so after passing it to a higher order function
- How to enumerate shapeless Record and access field keys in runtime?
- How can I use Shapeless to create a function abstracting over arity
- Automatically convert a case class to an extensible record in shapeless?
- Dynamically parametrize Poly1 function in shapeless
- Creating a shapeless polymorphic function with a naked type param
- Passing a individual arguments AND a Seq to a var-arg function
- How can I write a recursive polymorphic function with Shapeless
- How to infer inner type of Shapeless record value with unary type constructor?
- Equality of shapeless extensible records depends on field order
- Passing tuples to function parameters
- Scala Implicit parameters by passing a function as argument To feel the adnvatage
- Spark Dataframe : Accessing next record in map function
- Passing function in Scala
- Passing an HList to a function (with an implicit LeftFolder)
- Passing case class into function arguments
- scala passing function with underscore produces a function not a value
- Scala passing varargs to another function that takes varargs
More Query from same tag
- spark catch all exception and print to to string
- Use Scala (or java) method in Gradle build script
- Scala: Restrict a parameter based on another type parameter value of the same hierarchy
- Enable tilde trigger with Akka and sbt
- Defer val definition in Scala
- Implicit parameter not passed to higher-order function
- import electron browser window in scala js
- Scala analysis with SonarQube 5.4
- Scala Traits - how to implement method
- Scala function to get all sorted subsets of size k
- why scala assembly include application.conf file from test folder in jar?
- How do you count both filters through RDD operations?
- SBT - Adding custom resolver is not working
- Source.fromInputStream exception handling during reading lines
- Scala Idiomatic and Fastest way for consuming multiple web services and merging output
- spark shell dependency exception
- Scala method priority
- Omitting an argument for a method in a block
- Bootstrapping data into webpack bundled javascript from html
- Error while importing scala.collection.breakOut
- Need advice on how to optimize my Scala tests
- Pattern Matching on a Lifted Type (Slick Lifted Embedding)
- What are the advantages or disadvantages of declaring function/method in companion objects versus declaring them in traits?
- spark-submit dependency resolution for spark-csv
- Abstract over repeated flatMap
- Creating a Map for methods and running all of them
- Writting function that can operate on RDD and Seq in Scala
- Question about Injector in Lift
- Why cannot use implicit in this scenario?
- Scala class not found on classpath when specifying jar in classpath