raw-types

Java Generics and Raw Types

断了今生、忘了曾经 提交于 2020-01-30 05:17:46
问题 I have the next code: ArrayList value = new ArrayList<Integer>(); // 1 value.add("Test"); // 2 I'm trying to understand line 2. Although I can see that value.add("Test"); compiles without errors, I can't see the reason it doesn't throw a runtime exception. If value is referencing a generic ArrayList object, why Java allows to add a String to it? Can anyone explain it to me? The closest explanation I've found about this is described here, but I still don't understand the core reason: Stack s =

convert java to scala code - change of method signatures

孤者浪人 提交于 2020-01-17 06:52:43
问题 Trying to convert some java to scala code I face the problem of a different method signature which compiled fine in the java world: The following code in java (from https://github.com/DataSystemsLab/GeoSpark/blob/master/babylon/src/main/java/org/datasyslab/babylon/showcase/Example.java#L122-L126) visualizationOperator = new ScatterPlot(1000,600,USMainLandBoundary,false,-1,-1,true,true); visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true); visualizationOperator

Implementing Comparable with a generic class

我怕爱的太早我们不能终老 提交于 2019-12-28 12:32:29
问题 I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T . In order to implement the interface, I delegate the comparison to T . Here is my code: public class Item<T extends Comparable<T>> implements Comparable<Item> { private int s; private T t; public T getT() { return t; } @Override public int compareTo(Item o) { return getT().compareTo(o.getT()); } } When I try to compile it, I get the following error information:

Creating a Class[T] from a Manifest[T] without casting

别说谁变了你拦得住时间么 提交于 2019-12-24 07:43:06
问题 Given an ev: Manifest[T] I can get a Class[T] using ev.erasure.asInstanceOf[Class[T]] . It's a shame that ev.erasure alone returns a static type of Class[_] . Can I get a Class[T] from a manifest without casting? If not is there a reason why the blessed Scala creators have gone for a raw return type in the erasure method? I understand this may have a negligible impact on most code but I've run into this issue in an arguably non-idiomatic piece of Scala code and am curious more than anything

Java generics and design patterns: not parameterizing a reference to a generic type is always a bad thing?

混江龙づ霸主 提交于 2019-12-23 10:59:07
问题 this question is partially related to my last question. I have a generic class representing a collection of generic objects: public interface MyObject<N extends Number>{} public interface MyCollecion<N extends Number> { public foo(MyObject<N> obj) } In my design these collections of objects are constructed by a client class (let's call it CollectionBuilder) through an abstract class approach: public interface AbstractCollectionFactory { public abstract <N extends Number> MyCollection<MyObject

Scala class cant override compare method from Java Interface which extends java.util.comparator

我怕爱的太早我们不能终老 提交于 2019-12-19 07:53:43
问题 I'm currently working on a port of a jEdit plugin to write all code in Scala. However Im forced at a certain point to implement my own Comparator. My simplified code is as follows: class compare extends MiscUtilities.Compare { def compare(obj1: AnyRef, obj2: AnyRef): Int = 1 } The MiscUtilities.Compare has the following signature when looking from IntelliJ public static interface Compare extends java.util.Comparator { int compare(java.lang.Object o, java.lang.Object o1); } However when im

Scala class cant override compare method from Java Interface which extends java.util.comparator

半腔热情 提交于 2019-12-19 07:50:42
问题 I'm currently working on a port of a jEdit plugin to write all code in Scala. However Im forced at a certain point to implement my own Comparator. My simplified code is as follows: class compare extends MiscUtilities.Compare { def compare(obj1: AnyRef, obj2: AnyRef): Int = 1 } The MiscUtilities.Compare has the following signature when looking from IntelliJ public static interface Compare extends java.util.Comparator { int compare(java.lang.Object o, java.lang.Object o1); } However when im

Method references to raw types harmful?

巧了我就是萌 提交于 2019-12-19 06:04:58
问题 The code below contains a reference to Enum::name (notice no type parameter). public static <T extends Enum<T>> ColumnType<T, String> enumColumn(Class<T> klazz) { return simpleColumn((row, label) -> valueOf(klazz, row.getString(label)), Enum::name); } public static <T, R> ColumnType<T, R> simpleColumn(BiFunction<JsonObject, String, T> readFromJson, Function<T, R> writeToDb) { // ... } Javac reports a warning during compilation: [WARNING] found raw type: java.lang.Enum missing type arguments

Method references to raw types harmful?

一世执手 提交于 2019-12-19 06:04:22
问题 The code below contains a reference to Enum::name (notice no type parameter). public static <T extends Enum<T>> ColumnType<T, String> enumColumn(Class<T> klazz) { return simpleColumn((row, label) -> valueOf(klazz, row.getString(label)), Enum::name); } public static <T, R> ColumnType<T, R> simpleColumn(BiFunction<JsonObject, String, T> readFromJson, Function<T, R> writeToDb) { // ... } Javac reports a warning during compilation: [WARNING] found raw type: java.lang.Enum missing type arguments

Is this raw type assignment type-safe? List<T> = new ArrayList();

假如想象 提交于 2019-12-17 10:01:32
问题 I have some code like this: @SuppressWarnings({"unchecked", "rawtypes"}) List<String> theList = new ArrayList(); Is this type-safe? I think it is safe because I don't assign the raw type to anything else. I can even demonstrate that it performs type checking when I call add : theList.add(601); // compilation error I have read "What is a raw type and why shouldn't we use it?" but I don't think it applies here because I only create the list with a raw type. After that, I assign it to a