Scala List[Int] becomes List in Java
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
add a comment |
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
1
@StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., withList[MyType]
which becomesList<MyType>
in Java.
– beatngu13
Nov 21 '18 at 12:37
add a comment |
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
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
java scala
edited Nov 21 '18 at 13:00
beatngu13
asked Nov 21 '18 at 12:00
beatngu13beatngu13
1,9571932
1,9571932
1
@StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., withList[MyType]
which becomesList<MyType>
in Java.
– beatngu13
Nov 21 '18 at 12:37
add a comment |
1
@StuartLC during design time in my IDE. I just wonder why this doesn't happen with non-primitives, e.g., withList[MyType]
which becomesList<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
add a comment |
3 Answers
3
active
oldest
votes
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!
add a comment |
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>
Do you know why it doesn't compile to the corresponding primitive wrapper types such asInteger
?
– 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 annotatebar
with aList[Integer]
type).
– Yuval Itzchakov
Nov 21 '18 at 16:49
add a comment |
Try to use collection decorator asJava:
val javabar = bar.asJava
I know that this would be a workaround for the problem. I just wonder why the ScalaList[Int]
gets compiled toList<Object
, i.e., why the generalization toObject
?
– beatngu13
Nov 21 '18 at 12:34
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 21 '18 at 13:37
Mr.TurtleMr.Turtle
1,13821228
1,13821228
add a comment |
add a comment |
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>
Do you know why it doesn't compile to the corresponding primitive wrapper types such asInteger
?
– 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 annotatebar
with aList[Integer]
type).
– Yuval Itzchakov
Nov 21 '18 at 16:49
add a comment |
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>
Do you know why it doesn't compile to the corresponding primitive wrapper types such asInteger
?
– 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 annotatebar
with aList[Integer]
type).
– Yuval Itzchakov
Nov 21 '18 at 16:49
add a comment |
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>
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>
answered Nov 21 '18 at 15:53
Yuval ItzchakovYuval Itzchakov
116k26174243
116k26174243
Do you know why it doesn't compile to the corresponding primitive wrapper types such asInteger
?
– 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 annotatebar
with aList[Integer]
type).
– Yuval Itzchakov
Nov 21 '18 at 16:49
add a comment |
Do you know why it doesn't compile to the corresponding primitive wrapper types such asInteger
?
– 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 annotatebar
with aList[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
add a comment |
Try to use collection decorator asJava:
val javabar = bar.asJava
I know that this would be a workaround for the problem. I just wonder why the ScalaList[Int]
gets compiled toList<Object
, i.e., why the generalization toObject
?
– beatngu13
Nov 21 '18 at 12:34
add a comment |
Try to use collection decorator asJava:
val javabar = bar.asJava
I know that this would be a workaround for the problem. I just wonder why the ScalaList[Int]
gets compiled toList<Object
, i.e., why the generalization toObject
?
– beatngu13
Nov 21 '18 at 12:34
add a comment |
Try to use collection decorator asJava:
val javabar = bar.asJava
Try to use collection decorator asJava:
val javabar = bar.asJava
answered Nov 21 '18 at 12:27
Vladimir BerezkinVladimir Berezkin
1,66342030
1,66342030
I know that this would be a workaround for the problem. I just wonder why the ScalaList[Int]
gets compiled toList<Object
, i.e., why the generalization toObject
?
– beatngu13
Nov 21 '18 at 12:34
add a comment |
I know that this would be a workaround for the problem. I just wonder why the ScalaList[Int]
gets compiled toList<Object
, i.e., why the generalization toObject
?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 becomesList<MyType>
in Java.– beatngu13
Nov 21 '18 at 12:37