Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:19:01+00:00 2026-06-17T08:19:01+00:00

I’m new to Scala and exploring possibilities of implicit conversions and famous cake pattern.

  • 0

I’m new to Scala and exploring possibilities of implicit conversions and famous cake pattern. I tried to create the model class which has the id value listed as abstract type to avoid leaking implementation details. I also mixed it into cake pattern trait wrapper. Everything works fine except for implicit conversion from id to JSON (within Play framework). Scala compiler just can’t find implicit conversion no matter what I do.

Here’s the code that reproduces the the problem:

import anorm._
import play.api.libs.json._

trait Card {
  type Key
  val NoId: Key
  val id: Key
  val name: String
}

trait CardModelComponent {
  val cardModel: CardModel
  trait CardModel {
    def findById(id: Long): Option[Card]
    def findAll: Seq[Card]
    def delete(id: Long)
    def create(name: String): Option[Card]
  }
}

trait CardModelComponentImpl extends CardModelComponent {
  case class CardImpl(id: Pk[Long], name: String) extends Card {
    type Key = Pk[Long]

    object Key extends Writes[Key] {
      implicit def writes(key: Key): JsValue = {
        key match {
          case Id(idVal: Long) => JsNumber(idVal)
          case _ => JsNull
        }
      }
    }
    val NoId = NotAssigned
  }

  class CardModelImpl extends CardModel {
    def findById(id: Long): Option[Card] = { None }
    def findAll: Seq[Card] = { Seq(CardImpl(Id(1), "Some card"))}
    def delete(id: Long) {}
    def create(name: String): Option[Card] = { Some(CardImpl(Id(1), name)) }
  }
}

object ComponentsRegistry extends
CardModelComponentImpl {

  val cardModel = new CardModelImpl
}

val card = ComponentsRegistry.cardModel.create("Test card").get
Json.toJson(card.id)

Error output I’m getting is like follows:

> card: Card = CardImpl(1,Test card)
> <console>:19: error: No Json deserializer found for type card.Key. Try to implem
  ent an implicit Writes or Format for this type.
                Json.toJson(card.id)
                           ^

Is there any way to make it work? Looks like cake pattern wrapping hides too much type information from compiler, as I guess from card.Key type name.

I also tried to create Writer implementation for Pk directly with same error as result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:19:02+00:00Added an answer on June 17, 2026 at 8:19 am

    Like Jesper Nordenberg wrote, you have to bring the instance of Writes[Key] into scope. One way of doing that would be to demand that the implementation of Card publishes the Writer instance and then import that whereever you call toJson, like this:

    …

    trait Card {
      type Key
    
      val NoId: Key
      val id: Key
      val name: String
      implicit val Key: Writes[Key]
    }
    

    …

    trait CardModelComponentImpl extends CardModelComponent {
      case class CardImpl(id: Pk[Long], name: String) extends Card {
        type Key = Pk[Long]
    
        implicit object Key extends Writes[Key] {
          def writes(key: Key): JsValue = {
            key match {
              case Id(idVal: Long) => JsNumber(idVal)
              case _ => JsNull
            }
          }
        }
    
        val NoId = NotAssigned
      }
    
      class CardModelImpl extends CardModel {
        def findById(id: Long): Option[Card] = { None }
        def findAll: Seq[Card] = { Seq(CardImpl(Id(1), "Some card"))}
        def delete(id: Long) {}
        def create(name: String): Option[Card] = { Some(CardImpl(Id(1), name)) }
      }
    }
    

    …

    val card = ComponentsRegistry.cardModel.create("Test card").get
    import card.Key
    Json.toJson(card.id)
    

    or pass it explicitly:

    val card = ComponentsRegistry.cardModel.create("Test card").get
    Json.toJson(card.id)(card.Key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.