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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:15:58+00:00 2026-05-20T07:15:58+00:00

Given the following type and instance: type operation = (Int, Int) => Int def

  • 0

Given the following type and instance:

type operation = (Int, Int) => Int
def add: operation = _ + _

If I try to match an operation in a case statement, Scala complains about unchecked typing due to type erasure:

for (a <- elements) a match {
  case o: operation => // do stuff
}

Is there a way to achieve this kind of function-based typing while being erasure-friendly in case statements?

Note, this is similar to this thread.

  • 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-20T07:15:59+00:00Added an answer on May 20, 2026 at 7:15 am

    One easy way to deal with type erasure is to create an unparamaterized class. It’s not perfect, but it works. Make it a case class that extends Function2 and it’s not even too clunky to use either directly or in a pattern match

    scala> case class Operation(f : (Int,Int) => Int) extends ((Int,Int) => Int) {
         | def apply(x : Int, y : Int) = f(x,y)                                   
         | }
    defined class Operation
    
    scala> def add = Operation(_ + _)                                             
    add: Operation
    
    scala> val y = add(7,3)
    y: Int = 10
    
    scala> val elements = List(1, add, 2)
    elements: List[Any] = List(1, <function2>, 2)
    
    scala> for (a <- elements) yield a match {        
         | case Operation(f) => f(1,2)        
         | case x : Int => x
         | }
    res0: List[Int] = List(1, 3, 2)
    

    The limitation is that you have to have “boxed” the operation before you lose its type, not after. Also, you end up with one class per concrete function type.

    Another, arguably much better, solution is to not lose the type information. Use an Either to retain the static type info.

    scala> val elements : List[Either[Int, (Int, Int) => Int]] = List(Left(1), Right(_ + _), Left(2))
    elements: List[Either[Int,(Int, Int) => Int]] = List(Left(1), Right(<function2>), Left(2))
    
    scala> for (a <- elements) yield a match {
         | case Right(f) => f(1,2)            
         | case Left(x) => x                  
         | }
    
    res1: List[Int] = List(1, 3, 2)
    

    The limitation here is that it gets clunky if your List can have more than 2 types. But it effectively avoids forcing Scala to be a dynamically typed language, unlike the previous solution.

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

Sidebar

Related Questions

Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following: declare @a table ( pkid int, value int ) declare @b
The question more or less says it all. Given the following record structure: type
Given the following Ruby code, and given I have an instance of Klass, how
Given the following: - <ArrayOfWsParcelDocIndexIAS xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> - <wsParcelDocIndexIAS> <locatorNum xmlns=http://xxx/webservices/wsDocumentIndex/>131312</locatorNum> <docType xmlns=http://xxx/webservices/wsDocumentIndex/>KIOOLX_DOCINDEX</docType> <docID
Setup Given this user-defined type: struct T { static int x; int y; T()
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
When I type uname -a , it gives the following output. Linux mars 2.6.9-67.0.15.ELsmp
Why does the following code NOT give an error, nor any type of a
Given following Ruby statements: (Read input and store each word in array removing spaces

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.