推断的类型参数[Any,Int]不符合方法的类型参数范围[T <:Comparable [T],V <:T]

时间:2018-07-08 21:35:04

标签: scala types type-inference comparable

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。我正在尝试为我的块状态创建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();
    }
}

1 个答案:

答案 0 :(得分:1)

那是一大堆代码...

我认为您的示例可以简化为以下两行:

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

它会导致非常类似的错误:

  

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

     错误:类型不匹配;

     

found:整数
   必需: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
)