Giter Site home page Giter Site logo

Excessive usage of raw types about nbt HOT 7 CLOSED

querz avatar querz commented on June 12, 2024
Excessive usage of raw types

from nbt.

Comments (7)

Querz avatar Querz commented on June 12, 2024

I see your point and i'm working on it.

There is one Problem creating a ListTag of ListTags. Based on how you create ListTags using the constructor that takes the type class, it would be intuitive to do it this way:

ListTag<ListTag<?>> l = new ListTag<>(ListTag.class);

But obviously this doesn't compile because ListTag.class is not of type ListTag<?>.

And something like the following would be really ugly and having users of this library do this would be unreasonable:

ListTag<ListTag<?>> l = new ListTag<>((Class<ListTag<?>>)((Class<?>)ListTag.class));

Edit: Nevermind, i'm stupid. I can just modify the Constructor to accept a class of any type extending T

public ListTag(Class<? super T> typeClass)

from nbt.

Marcono1234 avatar Marcono1234 commented on June 12, 2024

public ListTag(Class<? super T> typeClass)

That should be ? extends T, otherwise it would allow stuff like new ListTag(Object.class).

from nbt.

Querz avatar Querz commented on June 12, 2024

I am allowing the type of the class to be a superclass of T, which in turn must be a subclass of Tag<?> (Tag of any type), therefore the type of the class can't be a supertype of the ListTag's type.

If i restrict the type of the class to be a subclass of T instead, i have trouble creating a ListTag of ListTags:

ListTag<ListTag<?>> l = new ListTag<>(ListTag.class);
java: incompatible types: cannot infer type arguments for net.querz.nbt.ListTag<>
    reason: cannot infer type-variable(s) T
      (argument mismatch; java.lang.Class<net.querz.nbt.ListTag> cannot be converted to java.lang.Class<? extends net.querz.nbt.ListTag<?>>)

Also, this restricts the type of the class to be exactly the type of the ListTag, even though the definition is kinda verbose.

from nbt.

Marcono1234 avatar Marcono1234 commented on June 12, 2024

which in turn must be a subclass of Tag<?>
Where is this restriction?


I am not getting any compilation problems when changing the constructor parameter.

from nbt.

Querz avatar Querz commented on June 12, 2024

Where is this restriction?

In the class definition:

public class ListTag<T extends Tag<?>> extends Tag<List<T>> implements Iterable<T> {

I am not getting any compilation problems when changing the constructor parameter.

Weird... I'm using Intellij IDEA 2018.2 to compile with JDK 1.8.0_181 and the unit tests won't compile with this error when i change the constructor parameter to Class<? extends T>:

[...]\NBT\src\test\java\net\querz\nbt\ListTagTest.java:242: error: incompatible types: cannot infer type arguments for ListTag<>
		ListTag<ListTag<?>> recursive = new ListTag<>(ListTag.class);
		                                           ^
    reason: cannot infer type-variable(s) T
      (argument mismatch; Class<ListTag> cannot be converted to Class<? extends ListTag<?>>)
  where T is a type-variable:
    T extends Tag<?> declared in class ListTag

from nbt.

Querz avatar Querz commented on June 12, 2024

Removed all raw types.
The constructor of ListTag now takes a class of type ? super T while T extends Tag<?>, which restricts the class to be the exact same type as T but still allows a ListTag of type ListTag<?>:

ListTag<ListTag<?>> l = new ListTag<>(ListTag.class);

from nbt.

Marcono1234 avatar Marcono1234 commented on June 12, 2024

Sorry that I have not said it before, but thanks for putting all this work into this project!


To the compiling problem again, it looks like it is working for me because I use Eclipse which uses its own compiler. There have been other cases where Eclipse compiled code just fine, but javac did not.

Here is the minimal test code, which should fail compiling for you:

public class ClassTest<T> {
    public ClassTest(Class<? extends T> classArg) { }
    
    public static void testNested() {
        // Compiles with Eclipse, but not with standard JDK
        ClassTest<ClassTest<?>> nested = new ClassTest<>(ClassTest.class);
    }
}

Hopefully I find the respective Eclipse bug report or get around to reporting it at some point.

from nbt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.