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

The Archive Base Latest Questions

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

I’d like to create a generic class with type parameter T which then creates

  • 0

I’d like to create a generic class with type parameter T which then creates instances of an inner class wrapping values of type T. I thought that if I used upper bound to tell that T must be subtype of eg. String, I’ll be able to create instances of the inner class initialized with a String.

class Test[T <: String] {

    private class TestEntry(val value: T)

    def init: Unit = {
        new TestEntry("")
    }
}

But I get the following error:

<console>:12: error: type mismatch;
 found   : java.lang.String("")
 required: T
               new TestEntry("")
                         ^

Actually, for my purpose, it would do to omit the upper bound and call new TestEntry(null), but the compiler does not even accept null.

What am I missing?

Edit:

Let me rephrase my example, I need something like this: I should be able to test an abstract luxury car service with any specific car without the need to re-implement the testing method. So both the abstract class (or trait) and the methods should work on subclasses of luxury cars.

class Car
class LuxuryCar extends Car
class Mercedes extends LuxuryCar
class Ferrari extends LuxuryCar

trait LuxuryCarService[CarType <: LuxuryCar] {

    def testWithCar(car: CarType): Unit = {}

    def testWithARandomLuxuryCar: Unit = test(new Mercedes)

    def testWithNoCar: Unit = test(null)
}

trait MercedesCarService extends LuxuryCarService[Mercedes]
  • 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:50:14+00:00Added an answer on May 29, 2026 at 8:50 am

    You’ve told it that T is a subclass of String. You then turn around and try to assign a string to it! That isn’t going to work, any more than this will:

    class Car {}
    class Mercedes extends Car {}         // Mercedes <: Car (T could be Mercedes)
    def serviceLuxuryCar(m: Mercedes) {}  // This is like your TestEntry
    serviceLuxuryCar(new Car)             // This won't work, it's not a Mercedes!
    

    I’m not sure what you actually mean for the code to do, but what you wrote should not (and does not, as you’ve found out) work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.