Note that there are some explanatory texts on larger screens.

plurals
  1. POReflection with Typeclasses
    primarykey
    data
    text
    <p>I'm writing a little registration module for some application and doing this with typeclasses. Main logic is based on Spray routing:</p> <pre><code>val api = post { path("register" / Rest) { rest =&gt; rest match { case "user" =&gt; register[User] // ... other cases } } } } </code></pre> <p>To simplify i have the following data type for User:</p> <pre><code>case class User(id: String, email: String) </code></pre> <p>And to extract entity from POST request i'm using typeclass:</p> <pre><code>trait EntityExtractor[T] { def extractFromRequest: Directive[T :: String :: HNil] } </code></pre> <p>implementation for user:</p> <pre><code>object User { implicit val extractor = new EntityExtractor[User] { def extractFromRequest: Directive[User :: String :: HNil] = formFields('email, 'pass).hmap { case email :: pass :: HNil =&gt; val id = UID.genId User(id, email) :: pass :: HNil } } } </code></pre> <p>The problem arises in my register method which uses a typeclass directive:</p> <pre><code>def register[T] = extractEntity[T].apply { entity =&gt; // fails here validateEntity(entity) { completeRegistration(entity) } } } def extractEntity[T: EntityExtractor]: Directive[Entity[T] :: HNil] = { implicitly[EntityExtractor[T]].extractFromRequest.hmap { case entity :: pass :: HNil =&gt; Entity(entity, pass) :: HNil } } </code></pre> <p>And it fail with an exception: <code>could not find implicit value for evidence parameter of type EntityExtractor[T]</code>.</p> <p>Is there any way to fix this with reflection (TypeTags or Manifest) <strong>without pattern matching</strong>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload