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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:51:45+00:00 2026-06-15T00:51:45+00:00

I was writing code for something like an array with variable dimensions. What I

  • 0

I was writing code for something like an array with variable dimensions. What I do is to maintain a linear underlying collections and wrap it up with index access methods. Since the dimension of the data structure is not known, I write something like

def apply(i: Int*): Double = ...

And it works perfectly. However, I cannot do the same thing to update method and operators like +=, so I end up writing methods like

def set(v: Double, i: Int*) ...
def add(v: Double, i: Int*) ...

which is fine but not what I really want. I guess the problem about update may be fixed in two ways:

  1. Change the order of arguments in update function, which makes it look weird.
  2. Allow variable-length arguments not as the last one. I find this questions asked in a general setting and it can be solved by using currying functions, which does not apply here.

The problem about += seems more complicated and it even exists when the index is of fixed length. Maybe we can add an object that has += operator and use this(…) to get the object (so that this(…) += v will invoke some method as we expect), but that will conflict with the apply method.

If anyone has solution to any of the above questions or has a reason why we shouldn’t be able to write code like this, please share your ideas! Thanks~

  • 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-15T00:51:46+00:00Added an answer on June 15, 2026 at 12:51 am

    The simplest solution I see right now is to have many different overloads of update for every dimension that you want to support. Say that you can determine that the maximum dimension that you’ll ever use is 10, this means that you’ll need 10 overloads. This might not seem very practical, but i can easily be abstracted away so it is very much practical actually:

    trait MultiKeyUpdate[K, V] {
      def doUpdate( k: K* )( v: V )
      def update(k1: K, v: V) { doUpdate( k1 )( v ) }
      def update(k1: K, k2: K, v: V) { doUpdate( k1, k2 )( v ) }
      def update(k1: K, k2: K, k3: K, v: V) { doUpdate( k1, k2, k3 )( v ) }  
      // ... and so on, up until max dimension ...
    }
    

    Usage:

    class C extends MultiKeyUpdate[Int, Double] {
      def apply(i: Int*): Double = {
        println("Returning element " + i.mkString("[",",","]"))
        123
      }
      def doUpdate( i: Int* )( v: Double ) {
        println("Updating element " + i.mkString("[",",","]") + " to value " + v)
      }
    }
    

    And some test in the REPL:

    scala> val o = new C
    o: C = C@12798c1
    scala> o(1,2,3)
    Returning element [1,2,3]
    res3: Double = 123.0
    scala> o(1,2,3) = 5.0
    Updating element [1,2,3] to value 5.0
    scala> o(1,2,3) += 7.0
    Returning element [1,2,3]
    Updating element [1,2,3] to value 130.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm writing code with QtCreator (2.4.1), I'm very often in this situation: something
I am writing some Ruby code, not Rails, and I need to handle something
All I am writing a very simple C code of dynamic 2D array declaration
I am writing an application something like list view with toggle button. Here is
I am writing unit test for an existing code which is like this class
I am writing a plugin for jquery, and I have used a code like
I'm writing some optimized C code that basically runs through an array and does
I'm writing some code where I need to have a class variable that's a
When writing code, I often place debug messages in the code. The debug messages
While writing code in a file that would comprise of PHP, HTML, CSS &

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.