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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:26:23+00:00 2026-06-04T20:26:23+00:00

Scala: Is it possible to constrain a type parameter to be non abstract? Are

  • 0

Scala: Is it possible to constrain a type parameter to be non abstract?

Are any other constraints possible except view bounds, upper bounds and lower bounds for type parameters and abstract type members? In C# for example, which I’m familiar with, you have the additional generic constraints:

where T: Class //Not sure if this covered in Scala by T<: AnyRef

where T: interface

where T: struct

where U: T //naked type constraint

where T: new () //This ensures that a type parameter is non-abstract and allows one to instantiate an object of the type in the generic class.

The last one is particularly important as it allows you to construct your unknown type, although its a shame you can only proscribe a parameterless constructor.

Can =:= <:< and <%< only be used on method parameters?

In response to the comments, the immediate trigger for the question was the need for a “T: new()” restraint or some equivalent mechanism.

class ExampleClass[T <: AnyRef] {
  val example: T = new T()//Won't compile as the compiler  
} //doesn't know if such a constructor exists

The uses of some of the C# constraints are particular to the needs of C#. For example one restraint that you don’t have in C# that people are always wanting is “T: numericType” That issue is already solved in Scala. I’m still very much boot strapping my way up the Scala language, so aside from the above, I was just trying to clarify exactly what tools are and are not available to me in this facet of Scala syntax, even though I don’t know yet exactly how I might want to use them in the Scala context.

I’m not sure if this is fully related but sometimes it seems the compiler (Eclipse 2.1.0.M1 with Eclipse 3.7.2) won’t let me instantiate collections of unknown element type. The following code now seems to compiles fine. So I’d like to know what the rules are:

abstract class Descrip [T <: DTypes]()
{      
  val hexs: MutableList[T#HexT] = new  MutableList[T#HexT] //compiles fine
  val sides: MutableList[T#SideT] = new MutableList[T#SideT] //compiles 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-04T20:26:24+00:00Added an answer on June 4, 2026 at 8:26 pm

    The need of new T() is normally resolved by using Manifests. For example this code compiles:

    class ExampleClass[T: Manifest] {
      val example: T = manifest[T].erasure.newInstance().asInstanceOf[T]
    }
    

    The drawback is that if the T for which ExampleClass is instantiated doesn’t have a no-arg constructor it will fail at runtime…

    Regarding Numeric, Ordering, etc., they are type classes and are part of the standard library, not part of the language. You can build your own type classes. The case of Manifest is special because it do have support from the language to provide the implicit object.

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

Sidebar

Related Questions

Is it possible to use Morphia in Scala? Are there any other lightweight ORMs
Possible Duplicate: How do I setup multiple type bounds in Scala? I'm a little
Possible Duplicate: Scala traits vs abstract classes What is the conceptual difference between abstract
Possible Duplicate: Scala map containing mix type values I have a situation where in
Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable
It is possible to define sealed classes in Scala, which are basically final except
Possible Duplicate: Any way to use some Scala for iOS coding? Would it be
In Scala, is it possible to get the string representation of a type at
The following is possible in Scala: scala> val l = List l: scala.collection.immutable.List.type =
Is it possible to create, in Scala, a type which is parametrized by a

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.