#Behaviour of generics wrt my code

1 messages · Page 1 of 1 (latest)

latent mist
#
    public Set<TagKey<?>> getTagPool(V v) {
        return this.entrySet().stream()
                .filter(entry -> entry.getValue().contains(v))
                .<TagKey<?>>mapMulti((entry, consumer) -> {
                    if (v.getClass() == Item.class) {
                        consumer.accept(TagUtils.getItemTagKeyFromResource(entry.getKey()));
                    } else {
                        consumer.accept(TagUtils.getBlockTagKeyFromResource(entry.getKey()));
                    }
                })
                .collect(Collectors.toSet());
    }```
Whereas `V` is declared in the class containing this method as a type parameter, would this method work as intended? Namely:

- The `consumer` is accepting instances of class `TagKey<V>`.
- The method returns a `Set<TagKey<V>>` where `V` is passed in as an argument of `#getTagPool`.
- `v.getClass() == Item.class` is a valid statement
- `#getItemTagKeyFromResource` and `#getBlockTagKeyFromResource` returns a `TagKey<V>` of respective type
cold apexBOT
#

This post has been reserved for your question.

Hey @latent mist! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

latent mist
limpid swan
#

Dude, we aren't going to guess what you're doing

#

But anyway, V is a type, so it cannot be passed as parameter

latent mist
#

im gonna cut the code down a bit so i can try and illustrate what im trying to do better

#
  public Set<TagKey<?>> getTagPool(V v) //V is defined as a type parameter in this class {
    return (v.class == Item.class) ? Static.util1(v) : Static.util2(v);
  }
//The goal of this method is to resolve whether the object passed as the argument is of class Item, then return a value from a static method whose type is either TagKey<Item> if the the statement is true, or TagKey<Block> if false.

//The method works as intended if this method can return as either Set<TagKey<Item>> or Set<TagKey<Block>>. 
#

I'm unsure how to use wildcards and generics in general