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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:59:48+00:00 2026-05-29T08:59:48+00:00

Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something

  • 0

Say I have a higher kinded type

SuperMap[Key[_],Value[_]]`.  

Suppose now that I had something even more specific that required that the type parameter for Key must match that for Value; that is, something like:

SuperDuperMap[T, Key[T], Value[T]]

Further suppose that I didn’t want just any T, but a very specific one where T <: OtherT

SuperDuperPooperMap[T <: OtherT, Key[T], Value[T]]

Can this be done in Scala? Is this just generally a bad idea? Is there an equivalent way of doing this that’s easier to read/write/use?

  • 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-05-29T08:59:49+00:00Added an answer on May 29, 2026 at 8:59 am

    Your declaration already works as supposed to, i.e. you’re restricting the type of T as well as Key and Value. The way you’ve written it, however, scala will complain if you issue something like

    scala> class Foo[T <: OtherT, Key[T], Value[T]]
    defined class Foo
    
    scala> new Foo[SpecialOtherT, Key[SpecialOtherT], Value[SpecialOtherT]]
    <console>:13: error: Key[SpecialOtherT] takes no type parameters, expected: one
                  new Foo[SpecialOtherT, Key[SpecialOtherT], Value[SpecialOtherT]]
    

    because the types of both Key and Value are already given by your former declaration. Hence this will work

    scala> new Foo[SpecialOtherT, Key, Value]
    res20: Foo[SpecialOtherT,Key,Value] = Foo@3dc6a6fd
    

    which is probably not want you want. You could do it like this

    scala> class Foo[T <: OtherT, K <: Key[T], V <: Value[T]]
    defined class Foo
    
    scala> new Foo[SpecialOtherT, Key[SpecialOtherT], Value[SpecialOtherT]]
    res21: Foo[SpecialOtherT,Key[SpecialOtherT],Value[SpecialOtherT]] = Foo@7110506e
    

    At the bottom line, since the types of Key and Value depend solely on T it is somewhat superfluous to have all that redundant information when working with Foo. So why not use an inner type declaration like so:

    class Foo[T <: OtherT] {
      type K = Key[T]
      type V = Value[T]
    }
    

    Then you’d have access to types K and V from within the class but wouldn’t need to type it everytime you create a new answer:

    scala> new Foo[SpecialOtherT]
    res23: Foo[SpecialOtherT] = Foo@17055e90
    
    scala> new Foo[Int]
    <console>:11: error: ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table with three columns, key , value and priority
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
let say that we have an array [5,5] 01,02,03,04,05 06,07,08,09,10 11,12,13,14,15 16,17,18,19,20 21,22,23,24,25 the
Let's say I have a Blog posts table that has a rating field which
Lets say you have website that keeps balance for each user. You give each
Say we have realized a value of TDD too late. Project is already matured,
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say you have an application divided into 3-tiers: GUI, business logic, and data access.

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.