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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:48+00:00 2026-05-23T09:30:48+00:00

How can I define a function that is accepting all the tuples(1 to 22)

  • 0

How can I define a function that is accepting all the tuples(1 to 22) as argument, I have something as follows in mind:

def foo (v=Tuple) =...

foo((1,2))
foo((1,2,3))

EDIT:

To answer the comment: I am actually trying to create a Tensor class which is a set of values and a set of indices. The indices can be covariant and/or contravariant (cf Wikipedia1 and Wikipedia2). I wanted to have a special syntax like Tensor((1,2),(3,4),values) which would create a tensor with values, two covariant indices having length (2,3) and two contravariant indices with length (3,4). So using this syntax I could also write Tensor((1,2,3),3,values) (with an implicit Int=>Tuple1).

I agree that Tuples are not suitable for this, better to use Lists. However the syntax is not so nice then…

  • 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-05-23T09:30:48+00:00Added an answer on May 23, 2026 at 9:30 am

    There is no trait specifically for tuples, but you could use a typeclass approach, as demonstrated in this answer.

    If your goal is really to have a List but allow callers to pass in tuples (for convenience), you can modify that solution so that the type class produces a List rather than a Product.

    In brief, the idea is that you provide implicit conversions from the types that callers can pass to the type you’re actually going to use:

    def foo(x: IndexList) = x.indices
    
    sealed case class IndexList(indices: List[Int])
    
    object IndexList {
       implicit def val2indices(i: Int) = IndexList(List(i))
       implicit def tuple2toIndices(t: (Int, Int)): IndexList = 
          product2indices(t)
       // etc
       implicit def list2indices(l: List[Int]) = IndexList(l)
    
       private def product2indices(p: Product) = 
          IndexList(p.productIterator.toList.asInstanceOf[List[Int]])
    }
    

    You can then call your method with any type for which you’ve provided a conversion:

    foo(1)
    foo((2,3))
    foo(List(1,2,3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One can easily define a function that accepts a 1d array argument like this:
In Scala, I can define structural types as follows: type Pressable = { def
How can I define a function that will accept my type and return its
We all know that you can overload a function according to the parameters: int
How can I declare and define a function so that it is only accessible
Can I define a function which, when called, inserts new locals into the caller's
I need an array to hold member-function-pointers of different classes. How can I define
How can I hook up an event to a function name I have defined
In C# we can define a generic type that imposes constraints on the types
In Java you can define generic class that accepts only types that extend 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.