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

  • Home
  • SEARCH
  • 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 8711741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:57:26+00:00 2026-06-13T04:57:26+00:00

Given: class Foo[T] { def get: T } class Bar class FooBar extends Foo[Bar]

  • 0

Given:

class Foo[T] {
 def get: T
}

class Bar
class FooBar extends Foo[Bar] {
 def get = new Bar
}

object Baz {
    def something [T, U <: Foo[T]] (foo : Class[U]): T = foo.newInstance.get
}

I should be able to do something like this, right?

Baz.something(classOf[FooBar])

Strangely this is throwing:

inferred type arguments [Nothing,this.FooBar] do not conform to method something's type parameter bounds [T,U <: this.Foo[T]]

Which is weird :S. BTW I’m having this issue while migrating some java code that’s equivalent to what I write here and it’s working fine.

  • 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-13T04:57:27+00:00Added an answer on June 13, 2026 at 4:57 am

    You’ve run into one of the more annoying limitations of Scala’s type inference! See this answer for a clear explanation of why the compiler is choking here.

    You have a handful of options. Most simply you can just provide the types yourself:

    Baz.something[Bar, FooBar](classOf[FooBar])
    

    But that’s annoyingly verbose. If you really don’t care about U, you can leave it out of the type argument list:

    object Baz {
      def something[T](foo: Class[_ <: Foo[T]]): T = foo.newInstance.get
    }
    

    Now FooBar will be inferred correctly in your example. You can also use a trick discussed in the answer linked above:

    object Baz {
      def something[T, U <% Foo[T]](foo: Class[U]): T = foo.newInstance.get
    }
    

    Why this works is a little tricky—the key is that after the view bound is desugared, T no longer appears in U‘s bound.

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

Sidebar

Related Questions

given this code: class Foo def bar return Bar.new end end class Bar ...
Given this definition: class Foo(var x: String) {} object Helper { def model[T](get: ⇒
class ClassName(object): def __init__(self, foo, bar): self.foo = foo # read-write property self.bar =
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
given module Foo def bar puts foobar end end I can do String.extend(Foo) and
Given a class Foo (whether it is a new-style class or not), how do
This is just an example, but given the following model: class Foo(models.model): bar =
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
Please point where a bug in my code. class Foo: def get(self): return self.a
Foo = Class.new Foo.class_eval do def class_bar class_bar end end Foo.instance_eval do def instance_bar

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.