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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:40:33+00:00 2026-05-22T00:40:33+00:00

I am search for the right signature of a method that takes a function

  • 0

I am search for the right signature of a method that takes a function func and an argument arg, copies them over the network to a remote computer and returns the result. Currently the signature looks like this:

def invokeRemote[A,B](address: String, func: A => B, arg: A): B

The problem with this is that the method throws a NotSerializable Exception if the arguments are not Serializable or one of Java’s primitive types.

I came up with the following solution to catch this error at compile time …

type Func = (A => B) with Serializable

def invokeRemote[A <: Serializable, B <: Serializable](address: String, func: Func, arg: A): B

… but now it is not possible anymore to pass arguments of type AnyVal like Int, Float or Double which do not explicitly implement Serializable.

How should the method signature look like such that it accepts only Serializable objects or objects of type AnyVal as argument?

  • 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-22T00:40:33+00:00Added an answer on May 22, 2026 at 12:40 am

    You can use a context bound implicit with a custom trait and provide implicit conversions for AnyVal and Serializable to that trait.

    trait Ser[M]
    
    implicit def toSer1[T <: AnyVal]: Ser[T] = new Ser[T] {}
    implicit def toSer2[T <: java.io.Serializable]: Ser[T] = new Ser[T] {}
    
    def f[T: Ser](a:T): T = a
    f(1)
    // res2: Int = 1
    f("123")
    // res3: java.lang.String = 123
    f(new Object)
    // could not find implicit value for evidence parameter of type Ser[java.lang.Object]
    

    The compiler will look for an implicit parameter based on the type and because some are provided for T <: AnyVal and T <: java.io.Serializable it will compile in that case.

    You can stick the implicit definitions in the companion object for Ser so that they are available where needed.

    Then, your signature becomes:

    def invokeRemote[A:Ser, B:Ser](address: String, func: A => B, arg: A): B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have padding to the right of my archives and search page and I
Can search engines such as Google index JavaScript generated web pages? When you right
I have to implement a search algorithm for a school assignment. Right now, i'm
I Have site change.is, where right side menu & search bar in header is
I'm sure this has been asked but I can't quite find the right search
I have a search box that has a default value as Search,i need to
I have a binary tree that I need to search through. I'm not searching
I'm writing an application that analyses search engine results. With the Google Search API
I'm using codeigniter I have this BuildTemplate function in my [HOME] controller that authenticates
I need to remove the search button which is normally placed on the right

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.