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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:17:38+00:00 2026-06-07T21:17:38+00:00

How do you represent a rectangular 2-dimensional (or multidimensional) array data structure in Scala?

  • 0

How do you represent a rectangular 2-dimensional (or multidimensional) array data structure in Scala?

That is, each row has the same length, verified at compile time, but the dimensions are determined at runtime?

Seq[Seq[A]] has the desired interface, but it permits the user to provide a “ragged” array, which can result in a run-time failure.

Seq[(A, A, A, A, A, A)] (and similar) does verify that the lengths are the same, but it also forces this length to be specified at compile time.

Example interface

Here’s an example interface of what I mean (of course, the inner dimension doesn’t have to be tuples; it could be specified as lists or some other type):

// Function that takes a rectangular array
def processArray(arr : RectArray2D[Int]) = {
    // do something that assumes all rows of RectArray are the same length
}

// Calling the function (OK)
println(processArray(RectArray2D(
    ( 0,  1,  2,  3),
    (10, 11, 12, 13),
    (20, 21, 22, 23)
)))
// Compile-time error
println(processArray(RectArray2D(
    ( 0,  1,  2,  3),
    (10, 11, 12),
    (20, 21, 22, 23, 24)
)))
  • 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-07T21:17:40+00:00Added an answer on June 7, 2026 at 9:17 pm

    This is possible using the Shapeless library’s sized types:

    import shapeless._
    
    def foo[A, N <: Nat](rect: Seq[Sized[Seq[A], N]]) = rect
    
    val a = Seq(Sized(1, 2, 3), Sized(4, 5, 6))
    val b = Seq(Sized(1, 2, 3), Sized(4, 5))
    

    Now foo(a) compiles, but foo(b) doesn’t.

    This allows us to write something very close to your desired interface:

    case class RectArray2D[A, N <: Nat](rows: Sized[Seq[A], N]*)
    
    def processArray(arr: RectArray2D[Int, _]) = {
      // Run-time confirmation of what we've verified at compile-time.
      require(arr.rows.map(_.size).distinct.size == 1)
      // Do something.
    }
    
    // Compiles and runs.
    processArray(RectArray2D(
      Sized( 0,  1,  2,  3),
      Sized(10, 11, 12, 13),
      Sized(20, 21, 22, 23)
    ))
    
    // Doesn't compile.
    processArray(RectArray2D(
      Sized( 0,  1,  2,  3),
      Sized(10, 11, 12),
      Sized(20, 21, 22, 23)
    ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to represent a two-dimensional coordinate grid with a two-dimensional array. Problem is,
I want to visually represent my data in columns, rather than the row representation
I'm going to represent a chess game as C++ structure. I think, that the
How to represent values that exceed the max values of any particular data type
Greetings. I have an 2-D array of size [N][N] which will represent a rectangular
I'm trying to represent a class that, amongst other things, has a set of
I am trying to represent to genetic variation data in a database for my
I currently represent my Business Layer and Data Layer in a single project in
I have a sparse matrix that represents a 3D rectangular space. Along some of
I want to represent a 2D shape in such a way that it can

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.