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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:58:47+00:00 2026-06-17T05:58:47+00:00

I have function which gets a Seq[_] as an argument and returns an immutable

  • 0

I have function which gets a Seq[_] as an argument and returns an immutable class instance with this Seq as a val member. If the Seq is mutable I obviously want to create a defensive copy to guarantee that my return class instance cannot be modified.

What are the best practice for this pattern? First I was surprised that it is not possible to overload the function

  def fnc(arg: immutable.Seq[_]) = ...
  def fnc(arg: mutable.Seq[_]) = ...

I could also pattern-match:

  def fnc(arg: Seq[_]) = arg match {
    case s: immutable.Seq[_] => { println("immutable"); s}
    case s: mutable.Seq[_] => {println("mutable"); List()++s }
    case _: ?
  }   

But I am not sure about the _ case. Is it guaranteed that arg is immutable.Seq or mutable.Seq? I also don’t know if List()++s is the correct way to convert it. I saw many posts on SO, but most of them where for 2.8 or earlier.

Are the Scala-Collections “intelligent” enough that I can just always (without pattern matching) write List()++s and I get the same instance if immutable and a deep copy if mutable?

What is the recommend way to do this?

  • 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-17T05:58:48+00:00Added an answer on June 17, 2026 at 5:58 am

    You will need to pattern match if you want to support both,. The code for Seq() ++ does not guarantee (as part of its API) that it won’t copy the rest if it’s immutable:

    scala> val v = Vector(1,2,3)
    v: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
    
    scala> Seq() ++ v
    res1: Seq[Int] = List(1, 2, 3)
    

    It may pattern-match itself for some special cases, but you know the cases you want. So:

    def fnc[A](arg: Seq[A]): Seq[A] = arg match {
      case s: collection.immutable.Seq[_] => arg
      case _ => Seq[A]() ++ arg
    }
    

    You needn’t worry about the _; this just says you don’t care exactly what the type argument is (not that you could check anyway), and if you write it this way, you don’t: pass through if immutable, otherwise copy.

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

Sidebar

Related Questions

I have a function which looks something like this, it returns a noncopyable class
i have an ajax function which gets called and returns json data. this function
I have this function which gets some variables and adds shadow to them, and
so I have this function which gets called multiple times during my program. //global
I have a function which gets 'unsigned int *& argument The parameter I want
So I have a function which returns a pdf stream. The calling procedure gets
I have the function which gets ID and returns date from table if it
Alright, I have jQUery function which adds class to a div#float once div#floatLunch gets
I have this function which gets executed when a link is clicked. But there
I have created a function which gets an encoded string (possibly UTF-16 not sure)

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.