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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:11:47+00:00 2026-06-08T18:11:47+00:00

I have the following definitions in java code: An abstract port definition (typed by

  • 0

I have the following definitions in java code:

An abstract port definition (typed by the concrete port class):

package test;

public class Port<
     PortOptions extends Port.Options, 
     ConcretePort extends Port<PortOptions, ConcretePort>> {

    public interface Options {
    }

}

An port service definition (can do some stuff with the ports via some callback mechanism)

package test;

import java.util.Set;

public class PortService {
    public interface Callback<T> {
        void processData(T data);
    }

    public void methodWithCallback(Callback<Set<Port>> callback) {

    }
}

And i want to register a callback into a port service from scala. What i tried is this:

package test

import test.PortService.Callback
import java.util

class PortServiceCaller {

    def callingMethod() {
        val portService: PortService = new PortService

        portService.methodWithCallback(new Callback[java.util.Set[Port[_, _]]] {
            def processData(data: util.Set[Port[_, _]]) {

            }
        })
    }
}

and it fails miserably with:

error: type mismatch;
found   : java.lang.Object with test.PortService.Callback[java.util.Set[test.Port[_, _]]]
required: test.PortService.Callback[java.util.Set[test.Port[_ <: test.Port.Options, _ <: test.Port[?0,?1]]]]
portService.methodWithCallback(new Callback[java.util.Set[Port[_, _]]] {

The question is: How to write the scala code in order to let me properly call the java service ?

I have looked over the scala typing system and i can’t seem to figure it out.

Later edit:

The solution is actually simple enough:

Declare the callback method as:

    public void methodWithCallback(Callback<Set<Port<?, ?>>> callback) {

    }

and call it from scala like this:

    portService.methodWithCallback(new Callback[java.util.Set[Port[_, _]]] {
        def processData(data: java.util.Set[Port[_, _]]) {
        }
    })

And also makes sense given the type system of both languages.

  • 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-08T18:11:49+00:00Added an answer on June 8, 2026 at 6:11 pm

    What you are doing in Java is something dangerous and your code compiles only because Java compiler is lazy and won’t check it. When you write :

       public void methodWithCallback(Callback<Set<Port>> callback) {
    
        }
    

    You are basically throwing away all the type information about Port:

    Port<PortOptions extends Port.Options, ConcretePort extends Port<PortOptions, ConcretePort>>
    

    The whole information is not going to be available, and the Java compiler let you omit the type parameters. In scala this is not possible, and not only you have to express the type parameters but also the type constraints correctly. The first question here is why you are writing Port without not even saying that there some unknown types there.

    public void methodWithCallback(Callback<Set<Port<?,?>>> callback) {
    
    }
    

    would be the right way of writing it.

    If now you want to express the bounds relationship among multiple unknown types, you cannot use the compact _ for existential types which is suitable for simple bounds, but you have to use forSome

      class Port[A,B<:A]
    
      def myMethod(a:Port[A,B]forSome {type B; type B<:A})
    

    However, as I tend to say, ending up in using these patterns is a such that your design is weak and you might have a cleaner and more elegant way to solve your problem. What is the point of using generics if you then need to throw your information about type-safety away?

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

Sidebar

Related Questions

I have the following definitions: public class BaseEntity { ... public BaseEntity() { }
Well, I have the following class definitions: private ObservableCollection<Node> _Nodes; public ObservableCollection<Node> Nodes {
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
I have the following property definition: public static class Extensions { public static readonly
I have the following record definition E3Vector3T = packed record public x: E3FloatT; y:
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following scala class definition (found in a paper), modeling categories: trait
I have the following definition for p: .contact p { margin: 5px 0 0
I have the following view definition in my asp.net mvc website: <% Using Ajax.BeginForm(UsrCtlChangePassword,
Suppose I have the following preprocessor definition #define MYNUMBER 10f; I would then like

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.