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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:27:11+00:00 2026-06-11T16:27:11+00:00

I have a case class, taking a Seq[T] as parameter: case class MyClass(value: Seq[T])

  • 0

I have a case class, taking a Seq[T] as parameter:

case class MyClass(value: Seq[T])

I now want to be able to write

MyClass(t1,t2,t3)

So I defined

object MyClass {
    def apply(value: T*) = new MyClass(value.toSeq)
}

It doesn’t work, because the case class defines

object MyClass {
    def apply(value: Seq[T])
}

and Seq[T] and T* have the same type after erasure, so I can’t overload them.

But I’d like to allow both access ways. Both ways should be allowed:

MyClass(t1,t2,t3)
MyClass(some_seq_of_T)

Since Seq[T] and T* are almost the same type (at least after erasure; and inside of the function having the parameter T* becomes Seq[T]), I think there should be a way to allow both ways of calling it.

Is there?

  • 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-11T16:27:12+00:00Added an answer on June 11, 2026 at 4:27 pm

    You could cheat a little bit and define your companion like this:

    case class MyClass[T](value: Seq[T])
    
    object MyClass {
      def apply[T](first: T, theRest: T*) = new MyClass(first :: theRest.toList)
      def apply[T]() = new MyClass[T](Nil)
    }
    

    The first apply handles the MyClass(1,2,3) as long as there is at least one argument.
    The scond apply handles the 0-argument case. They don’t conflict with the regular constructor.

    This way you can write MyClass(Seq(1,2,3)), MyClass(1,2,3), and MyClass(). Just note that for the empty one you’ll have to tell it what type to return, or else it will assume a MyClass[Nothing]

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

Sidebar

Related Questions

For example is it better to have: public class Case : EntityIdentifiable { public
I have a case, public class dictLanguage { public string EnglishText { get; set;
I have the following use case: There's a class called Template and with that
Case One Say you have a little class: class Point3D { private: float x,y,z;
I have the following test case: @ContextConfiguration(/spring/test-context.xml) @TransactionConfiguration(transactionManager=txManager) @Transactional() public class MyEntityDaoTestCase extends AbstractJUnit4SpringContextTests
I have a class mapped with a table, in my case in a declarative
I have a case where i want to submit a form and get the
Right now I have classes like: abstract class Record { // Required fields val
I have a case class that has a few parameters for its constructor, and
In C++, I want to have a class whose constructors are the following: class

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.