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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:57:32+00:00 2026-06-02T18:57:32+00:00

Consider the following class definitions: class Person[+T <: Person[T]] class Student() extends Person[Student] class

  • 0

Consider the following class definitions:

class Person[+T <: Person[T]]
class Student() extends Person[Student]
class Professor() extends Person[Professor]

I would like a list with a student and a professor:

val persons = List(new Student(), new Professor())

But this fails to compile with the following error:

type arguments [Person[Person[Any]]] do not conform to class Person's type parameter bounds [+T <: Person[T]]

Thanks to Daniel C. Sobral’s answer to my related previous question How to define case classes with members with unbound type parameters? I know an existential type would do the trick here. This compiles:

val persons = List[Person[T] forSome {type T <: Person[T]}](new Student(), new Professor())

The problem is caused by the upper-bound <: Person[T] in the type parameter of the class Person’s declaration. Removing the upper-bound lets the compiler infer a type parameter for the list which makes it compile: List[Person[Person[Person[Any]]]] as far as I can see.

Questions

  1. Why can’t the compiler infer any type for the list which would make it compile?
  2. Existential types are to the least verbose and might furthermore be tricky (see Daniel’s answer to my previous question linked above): Is there an alternative to explicit existential types for creating a list of students and professors?
  • 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-02T18:57:33+00:00Added an answer on June 2, 2026 at 6:57 pm

    I believe you’ve mentioned a possible alternative yourself in your second comment

    val persons = List[Person[_]](new Student(), new Professor())
    

    But from my understanding, the idiomatic way in Scala for doing things like this is to use a type declaration in Person and have it defined in Student and Professor:

    trait Person {
      type PersonImpl <: Person
      def colleagues: Seq[PersonImpl]
    }
    
    class Student extends Person {
      type PersonImpl = Student
      def colleagues = Seq(this)
    }
    
    class Professor extends Person {
      type PersonImpl = Professor
      def colleagues = Seq(this)
    }
    
    val persons = List(new Student, new Professor)
    

    Martin Odersky also mentioned in scala-language, that he’s thinking about unifying type parameters and abstract type members.

    Dependent on your actual use case, the simplest solution may be to rely on method overriding:

    trait Person {
      def colleagues: Seq[Person]
    }
    
    class Student extends Person {
      def colleagues: Seq[Student] = Seq(this)
    }
    
    class Professor extends Person {
      def colleagues: Seq[Professor] = Seq(this)
    }
    
    val persons = List(new Student, new Professor)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following class definitions: import com.codahale.logula.Logging abstract class A extends Logging { log.info(from
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following definitions: class Foo a where foo :: a -> Int class
Would doing this be consider good practice... I have class A with the following
Consider the following class definitions class of2010(object): def __init__(self): self._a = 1 self._b =
Consider the following class definition: class StrToTokens { StrToTokens(const char* str, const char* delimiters
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following class public class Class1 { public int A { get; set;
consider the following class: class Order { int OrderId {get; set;} int CustomerId {get;
Consider the following code: class A { public: virtual void f() throw ( int

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.