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 7744755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:50:37+00:00 2026-06-01T09:50:37+00:00

I want to create a Scala Id typeclass, so that, for example, I can

  • 0

I want to create a Scala Id typeclass, so that, for example, I can declare that an Id for type Foo takes a Long value, e.g.

val fooId: Id[Foo] = Id(12L) // type-safe at compile time
val fooIdValue: Long = fooId.value // able to get the value back out

I’ve tried various ways but I can’t seem to enforce the constraints. If I declare

trait WithId[I] {
  type Id = I
}

case class Id[A <: WithId[_]](value: A#Id) // A#Id => Any, not what I want!

class Foo extends WithId[Long] {
  type Id = Long
}

this allows

val fooId: Id[Foo] = Id("foo") // should be illegal unless a Long

If I change WithId to use an abstract type

trait WithId {
  type Id
}

case class Id[A <: WithId](value: A#Id)

class Foo extends WithId {
  type Id = Long
}

then

val fooId: Id[Foo] = Id(12L)

doesn’t compile, saying

no type parameters for method apply: (value: A#Id)net.box.Id[A] in object Id exist so that it can be applied to arguments (Long)  --- because --- argument expression's type is not compatible with formal parameter type;  found   : Long  required: ?0A#Id

How can I say and enforce that an Id[Foo] takes a Long?

  • 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-01T09:50:38+00:00Added an answer on June 1, 2026 at 9:50 am

    You’re right to get rid of that type parameter on WithId. However, the second problem that arises results from how you’re instantiating your Id. When you say val x: Foo[T], you’re specifying what type you want x to have, but you’re not really helping the compiler out with figuring out what type Foo should use when it’s being constructed. So… the compiler error that you’re getting results from saying to Foo that you want the Id out of A, but you haven’t told Foo what A even is! To fix it, simply change your usage to

    val fooId = Id[Foo](12L)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a specific generic method in Scala. It takes two parameters.
I want to create lightweight interfaces with methods that I can plug into classes.
We need to create a Server with Scala RemotActors, that can handle multiple clients.
How can I create a jar from some Scala source code? I want to
I'm new to Scala and don't know Java. I want to create a jar
I want to create a class with a nested enum. public class Foo {
I want to create a stored procedure (on SQL Server 2005) that fetches a
I am trying to create a DB management tool in Scala, and I want
I am trying to figure out how to create a method that takes a
I want to create a Scala class where one of its var is read-only

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.