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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:36+00:00 2026-05-26T22:56:36+00:00

I am writing an API that I want to be usable from both Scala

  • 0

I am writing an API that I want to be usable from both Scala and Java. I have a polymorphic method defined in a Scala class, and I am having trouble calling it from Java.

According to this interoperability FAQ at scala-lang.org, using a Scala class that uses advanced language features “can be tricky” but it does not say it is impossible. Since how Scala’s advanced features translate to Java is not documented, it suggests disassembling the class file to experiment with how it works.

Example Code

class Polymorphic {
  def polyMethod[T](implicit om: Manifest[T]) = {
    println(om.erasure.getName)
    om.erasure.newInstance
  }
}

object Polymorphic {
  def main(args: Array[String]) {
    val objOfT = (new Polymorphic).polyMethod[String]
  }
}

This works fine in scala, printing “java.lang.String“

I ran javap -c on the resulting class file and got the following listing for polyMethod:

public java.lang.Object polyMethod(scala.reflect.Manifest);
  Code:
   0:   getstatic   #20; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   3:   aload_1
   4:   invokeinterface #27,  1; //InterfaceMethod scala/reflect/ClassManifest.erasure:()Ljava/lang/Class;
   9:   invokevirtual   #33; //Method java/lang/Class.getName:()Ljava/lang/String;
   12:  invokevirtual   #37; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   15:  aload_1
   16:  invokeinterface #27,  1; //InterfaceMethod scala/reflect/ClassManifest.erasure:()Ljava/lang/Class;
   21:  invokevirtual   #41; //Method java/lang/Class.newInstance:()Ljava/lang/Object;
   24:  areturn

3 Major Problems:

  1. The method is polymorphic in its return type
  2. The Manifest parameter is not implicit in Java
  3. I can’t actually pass the type parameter.

The real project code I’m trying to do this with is here:
https://github.com/ConnorDoyle/EnMAS/blob/master/clientServerPrototype/src/org/enmas/pomdp/State.scala

My ultimate goal is a fully generic yet type safe data dictionary. It works great in Scala, but I would really like to be able to call this method from the Java API as well. My design motivation is to keep reflection, explicit casting, and null checks out of the client code.

Edit: 18 Nov 2011 In response to comment by @kassens below

Changing the definition of polyMethod to the following:

def polyMethod[T](implicit om: Manifest[T]): T = {
  println(om.erasure.getName)
  om.erasure.newInstance.asInstanceOf[T]
}

gives the exact same byte code when I inspect the class file with javap.

  • 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-26T22:56:36+00:00Added an answer on May 26, 2026 at 10:56 pm

    There are several problems at play:

    1. Unified generics in Scala. The fact that T is unbounded in Scala (meaning it can be instantiated with primitive types), makes the Scala compiler conservatively erase it to Object. If you change your scala code to

      def polyMethod[T <: AnyRef](implicit om: Manifest[T]) = {
         println(om.erasure.getName)
         om.erasure.newInstance.asInstanceOf[T]
      }
      

      it will return T. Note the explicit cast from newInstance, since the Scala library method does not return the correct generic type.

    2. Using Manifests. This adds another layer of generics that you need to align in order to make your call pass. As didierd suggested, better have an overload in Scala that takes a java.lang.Class instead.

    So my advice would be to add this in your Scala code:

      def polyMethod[T <: AnyRef](cls: Class[T]): T = 
        polyMethod(Manifest.classType(cls))
    

    And then call it this way from Java:

    void test(Polymorphic p) { 
        JavaScalaCall c = p.polyMethod(this.getClass());
    }
    

    As a side note, the bytecode is always erased. If you want to see the generic information in classfiles, look for the Signature attribute using, for instance, jclasslib.

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

Sidebar

Related Questions

I'm writing an API method that returns a list of objects from a web
I have an application I'm writing for Windows 8/WinRT that uses the StreamSocket API
I'm writing an API for retrieving data from a JDBC-connected Java Servlet via JSON.
I'm trying to find a solution that offers either a Java api for writing
I am writing an API that updates a LOT of different fields in a
I'm writing an API on Android that will be used by an existing application,
I'm writing C# code that uses the windows IP Helper API. One of the
Hey--I'm writing a basic Rails app that uses the digg API. I'm trying to
I am writing an API and have come across the following pattern: My API
I am writing an API that uses sockets. In the API, I allocate memory

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.