Scala List[Int] becomes List in Java












4















I have the following list defined within a Scala object:



object Foo {
val bar = List(1, 2, 3)
}


Which seems to become a scala.collection.immutable.List<Object> when I try to use it from Java. As a consequence, I have to use getters like Foo.bar().apply$mcII$sp(i) or apply with a cast to Integer/int.



Why is the generic type Object and not Integer? This also seems to be only the case for types that exist as Java primitives; List[MyType] becomes List<MyType> in Scala.










share|improve this question




















  • 1





    @StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

    – beatngu13
    Nov 21 '18 at 12:37
















4















I have the following list defined within a Scala object:



object Foo {
val bar = List(1, 2, 3)
}


Which seems to become a scala.collection.immutable.List<Object> when I try to use it from Java. As a consequence, I have to use getters like Foo.bar().apply$mcII$sp(i) or apply with a cast to Integer/int.



Why is the generic type Object and not Integer? This also seems to be only the case for types that exist as Java primitives; List[MyType] becomes List<MyType> in Scala.










share|improve this question




















  • 1





    @StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

    – beatngu13
    Nov 21 '18 at 12:37














4












4








4


1






I have the following list defined within a Scala object:



object Foo {
val bar = List(1, 2, 3)
}


Which seems to become a scala.collection.immutable.List<Object> when I try to use it from Java. As a consequence, I have to use getters like Foo.bar().apply$mcII$sp(i) or apply with a cast to Integer/int.



Why is the generic type Object and not Integer? This also seems to be only the case for types that exist as Java primitives; List[MyType] becomes List<MyType> in Scala.










share|improve this question
















I have the following list defined within a Scala object:



object Foo {
val bar = List(1, 2, 3)
}


Which seems to become a scala.collection.immutable.List<Object> when I try to use it from Java. As a consequence, I have to use getters like Foo.bar().apply$mcII$sp(i) or apply with a cast to Integer/int.



Why is the generic type Object and not Integer? This also seems to be only the case for types that exist as Java primitives; List[MyType] becomes List<MyType> in Scala.







java scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:00







beatngu13

















asked Nov 21 '18 at 12:00









beatngu13beatngu13

1,9021831




1,9021831








  • 1





    @StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

    – beatngu13
    Nov 21 '18 at 12:37














  • 1





    @StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

    – beatngu13
    Nov 21 '18 at 12:37








1




1





@StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

– beatngu13
Nov 21 '18 at 12:37





@StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., with List[MyType] which becomes List<MyType> in Java.

– beatngu13
Nov 21 '18 at 12:37












3 Answers
3






active

oldest

votes


















2














I have experienced a somewhat similar issue with Swagger not responding well to Scala.



I don't know why, but this bug/feature is related to Java's primitives that dont't have setters and getters (like objects). Since the Java compiler can't find a suiting object, it just compiles it down to Object.



Scala collections have made converters to fix this: https://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html



The only workaround I can think about is to use: Foo.bar.toJava



Sources:
Deserializing Scala list with Jackson



https://stackoverflow.com/a/52581955/2291510



Spring RequestParam formatter for Scala.Option



Good luck!






share|improve this answer































    2














    This happens due to the fact that Java does not support primitive types in generic types, generics are a compile time construct only in Java, they do not exist in JVM bytecode, thus they must be convertible to Java's Object type, which primitives cannot.



    If we compile the code using the -Xprint:jvm flag, you can see that List[Int] actually compiles to the non generic List:



    package com.yuvalitzchakov.github {
    object Foo extends Object {
    private[this] val bar: List = _;
    <stable> <accessor> def bar(): List = Foo.this.bar;
    def <init>(): com.yuvalitzchakov.github.Foo.type = {
    Foo.super.<init>();
    Foo.this.bar = scala.collection.immutable.List.apply(scala.Predef.wrapIntArray(Array[Int]{1, 2, 3}));
    ()
    }
    }
    }


    If Foo.bar was a List[Integer], this would yield a List<Integer> in Java, instead of List<Object>






    share|improve this answer
























    • Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

      – beatngu13
      Nov 21 '18 at 16:36






    • 1





      @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

      – Yuval Itzchakov
      Nov 21 '18 at 16:49



















    1














    Try to use collection decorator asJava:



    val javabar = bar.asJava





    share|improve this answer
























    • I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

      – beatngu13
      Nov 21 '18 at 12:34













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411597%2fscala-listint-becomes-listobject-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I have experienced a somewhat similar issue with Swagger not responding well to Scala.



    I don't know why, but this bug/feature is related to Java's primitives that dont't have setters and getters (like objects). Since the Java compiler can't find a suiting object, it just compiles it down to Object.



    Scala collections have made converters to fix this: https://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html



    The only workaround I can think about is to use: Foo.bar.toJava



    Sources:
    Deserializing Scala list with Jackson



    https://stackoverflow.com/a/52581955/2291510



    Spring RequestParam formatter for Scala.Option



    Good luck!






    share|improve this answer




























      2














      I have experienced a somewhat similar issue with Swagger not responding well to Scala.



      I don't know why, but this bug/feature is related to Java's primitives that dont't have setters and getters (like objects). Since the Java compiler can't find a suiting object, it just compiles it down to Object.



      Scala collections have made converters to fix this: https://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html



      The only workaround I can think about is to use: Foo.bar.toJava



      Sources:
      Deserializing Scala list with Jackson



      https://stackoverflow.com/a/52581955/2291510



      Spring RequestParam formatter for Scala.Option



      Good luck!






      share|improve this answer


























        2












        2








        2







        I have experienced a somewhat similar issue with Swagger not responding well to Scala.



        I don't know why, but this bug/feature is related to Java's primitives that dont't have setters and getters (like objects). Since the Java compiler can't find a suiting object, it just compiles it down to Object.



        Scala collections have made converters to fix this: https://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html



        The only workaround I can think about is to use: Foo.bar.toJava



        Sources:
        Deserializing Scala list with Jackson



        https://stackoverflow.com/a/52581955/2291510



        Spring RequestParam formatter for Scala.Option



        Good luck!






        share|improve this answer













        I have experienced a somewhat similar issue with Swagger not responding well to Scala.



        I don't know why, but this bug/feature is related to Java's primitives that dont't have setters and getters (like objects). Since the Java compiler can't find a suiting object, it just compiles it down to Object.



        Scala collections have made converters to fix this: https://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html



        The only workaround I can think about is to use: Foo.bar.toJava



        Sources:
        Deserializing Scala list with Jackson



        https://stackoverflow.com/a/52581955/2291510



        Spring RequestParam formatter for Scala.Option



        Good luck!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 13:37









        Mr.TurtleMr.Turtle

        9682924




        9682924

























            2














            This happens due to the fact that Java does not support primitive types in generic types, generics are a compile time construct only in Java, they do not exist in JVM bytecode, thus they must be convertible to Java's Object type, which primitives cannot.



            If we compile the code using the -Xprint:jvm flag, you can see that List[Int] actually compiles to the non generic List:



            package com.yuvalitzchakov.github {
            object Foo extends Object {
            private[this] val bar: List = _;
            <stable> <accessor> def bar(): List = Foo.this.bar;
            def <init>(): com.yuvalitzchakov.github.Foo.type = {
            Foo.super.<init>();
            Foo.this.bar = scala.collection.immutable.List.apply(scala.Predef.wrapIntArray(Array[Int]{1, 2, 3}));
            ()
            }
            }
            }


            If Foo.bar was a List[Integer], this would yield a List<Integer> in Java, instead of List<Object>






            share|improve this answer
























            • Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

              – beatngu13
              Nov 21 '18 at 16:36






            • 1





              @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

              – Yuval Itzchakov
              Nov 21 '18 at 16:49
















            2














            This happens due to the fact that Java does not support primitive types in generic types, generics are a compile time construct only in Java, they do not exist in JVM bytecode, thus they must be convertible to Java's Object type, which primitives cannot.



            If we compile the code using the -Xprint:jvm flag, you can see that List[Int] actually compiles to the non generic List:



            package com.yuvalitzchakov.github {
            object Foo extends Object {
            private[this] val bar: List = _;
            <stable> <accessor> def bar(): List = Foo.this.bar;
            def <init>(): com.yuvalitzchakov.github.Foo.type = {
            Foo.super.<init>();
            Foo.this.bar = scala.collection.immutable.List.apply(scala.Predef.wrapIntArray(Array[Int]{1, 2, 3}));
            ()
            }
            }
            }


            If Foo.bar was a List[Integer], this would yield a List<Integer> in Java, instead of List<Object>






            share|improve this answer
























            • Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

              – beatngu13
              Nov 21 '18 at 16:36






            • 1





              @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

              – Yuval Itzchakov
              Nov 21 '18 at 16:49














            2












            2








            2







            This happens due to the fact that Java does not support primitive types in generic types, generics are a compile time construct only in Java, they do not exist in JVM bytecode, thus they must be convertible to Java's Object type, which primitives cannot.



            If we compile the code using the -Xprint:jvm flag, you can see that List[Int] actually compiles to the non generic List:



            package com.yuvalitzchakov.github {
            object Foo extends Object {
            private[this] val bar: List = _;
            <stable> <accessor> def bar(): List = Foo.this.bar;
            def <init>(): com.yuvalitzchakov.github.Foo.type = {
            Foo.super.<init>();
            Foo.this.bar = scala.collection.immutable.List.apply(scala.Predef.wrapIntArray(Array[Int]{1, 2, 3}));
            ()
            }
            }
            }


            If Foo.bar was a List[Integer], this would yield a List<Integer> in Java, instead of List<Object>






            share|improve this answer













            This happens due to the fact that Java does not support primitive types in generic types, generics are a compile time construct only in Java, they do not exist in JVM bytecode, thus they must be convertible to Java's Object type, which primitives cannot.



            If we compile the code using the -Xprint:jvm flag, you can see that List[Int] actually compiles to the non generic List:



            package com.yuvalitzchakov.github {
            object Foo extends Object {
            private[this] val bar: List = _;
            <stable> <accessor> def bar(): List = Foo.this.bar;
            def <init>(): com.yuvalitzchakov.github.Foo.type = {
            Foo.super.<init>();
            Foo.this.bar = scala.collection.immutable.List.apply(scala.Predef.wrapIntArray(Array[Int]{1, 2, 3}));
            ()
            }
            }
            }


            If Foo.bar was a List[Integer], this would yield a List<Integer> in Java, instead of List<Object>







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 15:53









            Yuval ItzchakovYuval Itzchakov

            113k26168238




            113k26168238













            • Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

              – beatngu13
              Nov 21 '18 at 16:36






            • 1





              @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

              – Yuval Itzchakov
              Nov 21 '18 at 16:49



















            • Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

              – beatngu13
              Nov 21 '18 at 16:36






            • 1





              @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

              – Yuval Itzchakov
              Nov 21 '18 at 16:49

















            Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

            – beatngu13
            Nov 21 '18 at 16:36





            Do you know why it doesn't compile to the corresponding primitive wrapper types such as Integer?

            – beatngu13
            Nov 21 '18 at 16:36




            1




            1





            @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

            – Yuval Itzchakov
            Nov 21 '18 at 16:49





            @beatngu13 That's a good question, I actually do not know why there's no auto lifting to the wrapper types (unless we explicitly annotate bar with a List[Integer] type).

            – Yuval Itzchakov
            Nov 21 '18 at 16:49











            1














            Try to use collection decorator asJava:



            val javabar = bar.asJava





            share|improve this answer
























            • I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

              – beatngu13
              Nov 21 '18 at 12:34


















            1














            Try to use collection decorator asJava:



            val javabar = bar.asJava





            share|improve this answer
























            • I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

              – beatngu13
              Nov 21 '18 at 12:34
















            1












            1








            1







            Try to use collection decorator asJava:



            val javabar = bar.asJava





            share|improve this answer













            Try to use collection decorator asJava:



            val javabar = bar.asJava






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 12:27









            Vladimir BerezkinVladimir Berezkin

            1,63341930




            1,63341930













            • I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

              – beatngu13
              Nov 21 '18 at 12:34





















            • I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

              – beatngu13
              Nov 21 '18 at 12:34



















            I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

            – beatngu13
            Nov 21 '18 at 12:34







            I know that this would be a workaround for the problem. I just wonder why the Scala List[Int] gets compiled to List<Object, i.e., why the generalization to Object?

            – beatngu13
            Nov 21 '18 at 12:34




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411597%2fscala-listint-becomes-listobject-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin