推断类型参数[Any,Int]不符合方法的类型参数bounds [T.

问题描述 投票:-1回答:1
val PROP_FEMALE:IProperty[java.lang.Integer] = PropertyInteger.create("female", 0,1)

worldIn.setBlockState(pos, state.withProperty(PROP_FEMALE, if (isFemale) 1 else 0), 2)

我在Scala中编写了一个Minecraft Forge mod。我正在尝试为我的blockstate创建PropertyInteger。这在普通Java中完全正常。然而,Scala被证明很难让它与我合作被称为隐式类型?

由于某种原因,这会产生此错误。

Error:(53, 45) inferred type arguments [Any,Int] do not conform to method withProperty's type parameter bounds [T <: Comparable[T],V <: T]
  worldIn.setBlockState(pos, withAge(0).withProperty(PROP_FEMALE, if (createFemale || isFemale) 1 else 0))
Error:(53, 58) type mismatch;
 found   : net.minecraft.block.properties.IProperty[Integer]
 required: net.minecraft.block.properties.IProperty[T]
      worldIn.setBlockState(pos, withAge(0).withProperty(PROP_FEMALE, if (createFemale || isFemale) 1 else 0))

这是withProperty方法。

<T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty<T> property, V value);

这是PropertyInteger.create方法

public static PropertyInteger create(String name, int min, int max)
{
    return new PropertyInteger(name, min, max);
}

这是所有PropertyInteger

public class PropertyInteger extends PropertyHelper<Integer>
{
    private final ImmutableSet<Integer> allowedValues;

    protected PropertyInteger(String name, int min, int max)
    {
        super(name, Integer.class);

        if (min < 0)
        {
            throw new IllegalArgumentException("Min value of " + name + " must be 0 or greater");
        }
        else if (max <= min)
        {
            throw new IllegalArgumentException("Max value of " + name + " must be greater than min (" + min + ")");
        }
        else
        {
            Set<Integer> set = Sets.<Integer>newHashSet();

            for (int i = min; i <= max; ++i)
            {
                set.add(Integer.valueOf(i));
            }

            this.allowedValues = ImmutableSet.copyOf(set);
        }
    }

    public Collection<Integer> getAllowedValues()
    {
        return this.allowedValues;
    }

    public boolean equals(Object p_equals_1_)
    {
        if (this == p_equals_1_)
        {
            return true;
        }
        else if (p_equals_1_ instanceof PropertyInteger && super.equals(p_equals_1_))
        {
            PropertyInteger propertyinteger = (PropertyInteger)p_equals_1_;
            return this.allowedValues.equals(propertyinteger.allowedValues);
        }
        else
        {
            return false;
        }
    }

    public int hashCode()
    {
        return 31 * super.hashCode() + this.allowedValues.hashCode();
    }

    public static PropertyInteger create(String name, int min, int max)
    {
        return new PropertyInteger(name, min, max);
    }

    public Optional<Integer> parseValue(String value)
    {
        try
        {
            Integer integer = Integer.valueOf(value);
            return this.allowedValues.contains(integer) ? Optional.of(integer) : Optional.absent();
        }
        catch (NumberFormatException var3)
        {
            return Optional.<Integer>absent();
        }
    }

    /**
     * Get the name for the given value.
     */
    public String getName(Integer value)
    {
        return value.toString();
    }
}
scala types type-inference comparable
1个回答
1
投票

这是一个巨大的代码墙......

我认为你的例子可以简化为以下两行:

def withProperty[T <: Comparable[T], V <: T](k: T, v: V) = ()
withProperty(42: java.lang.Integer, 42)

它导致一个非常相似的错误:

错误:推断类型参数[Any,Int]不符合方法withProperty的类型参数bounds [T <:Comparable [T],V <:T] withProperty(42:java.lang.Integer,42)^:13:

错误:类型不匹配;

发现:整数 要求:T

所以我认为明显的解决方法也应该适用于您的代码。

简单地归因于第二个参数的类型明确地消除了错误:

withProperty(42: java.lang.Integer, (if (true) 1 else 0): java.lang.Integer)

如果将其传回到代码中,它应该看起来像这样:

worldIn.setBlockState(
  pos, 
  state.withProperty(PROP_FEMALE, (if (isFemale) 1 else 0): java.lang.Integer), 
  2
)
© www.soinside.com 2019 - 2024. All rights reserved.