Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala中的抽象参数类型_Scala_Parametric Polymorphism_Abstract Type - Fatal编程技术网

Scala中的抽象参数类型

Scala中的抽象参数类型,scala,parametric-polymorphism,abstract-type,Scala,Parametric Polymorphism,Abstract Type,我有: trait A[B[_]] 我有: 特征库{ 键入AImpl此代码可以满足您的要求,但我认为您不是有意这样做的: trait A[B[_]] trait Base { type AImpl <: A[B] forSome { type B[_] } def foo: AImpl } 这要求它的实现符合这样的类型。但是,由于 B隐藏在存在类型的内部,因此外部未知 A是不变的,因此它强制实现为A[B]的子类型,用于精确隐藏的B 这两者一起禁止AImpl实现。解决方法是将A

我有:

trait A[B[_]]
我有:

特征库{

键入AImpl此代码可以满足您的要求,但我认为您不是有意这样做的:

trait A[B[_]]

trait Base {
  type AImpl <: A[B] forSome { type B[_] }
  def foo: AImpl
}
这要求它的实现符合这样的类型。但是,由于

  • B
    隐藏在存在类型的内部,因此外部未知
  • A
    是不变的,因此它强制实现为
    A[B]
    的子类型,用于精确隐藏的
    B
  • 这两者一起禁止
    AImpl
    实现。解决方法是将
    A
    变为协变:

    trait A[+B[_]]
    
    trait Base {
      type AImpl <: A[B] forSome { type B[_] }
      def foo: AImpl
    }
    
    trait BB[T]
    trait AA extends A[BB]
    
    object Child extends Base {
      override type AImpl = A[BB]
    
      def foo = ???
    }
    
    trait A[+B[_]]
    性状基础{
    
    键入AImpl这很难是您想要的。具体来说,您也可能希望使
    AImpl
    更高级。谢谢。您的解决方案几乎可以工作,但仍然会给我带来错误。我已更新了预期用法。基本上,我想要“类型族”@Mathsnoob这段示例代码是在我的IDE中编译的。你可能需要在编译器选项中启用更高级的类型。不管怎样,让我检查一下你想要什么。为什么你不能删除
    A[B[\u],\u]
    ?很明显
    A
    这里是
    (*,*)->*
    ,这不可能是正确的。从这方面来看,您想要什么还不清楚。另外,scala中的存在类型从根本上被打破了,在大多数情况下,它不是您想要的。那么您想解决什么问题呢?很抱歉造成混淆。上面是我的代码的简化,我忘了更改错误。哟你是对的。最初的错误是:
    A[B[\u]]对于某些{type B[\u]}
    。我在你的代码中得到的错误是:
    error:(11,18)用bounds@Mathsnoob覆盖trait Base中的类型AImpl。我已经在更新中解决了这个错误。
    
    trait A[+B[_]]
    
    trait Base {
      type AImpl <: A[B] forSome { type B[_] }
      def foo: AImpl
    }
    
    trait BB[T]
    trait AA extends A[BB]
    
    object Child extends Base {
      override type AImpl = A[BB]
    
      def foo = ???
    }