未定义的抽象类型成员和存在类型之间的区别 [英] Difference between an undefined abstract type member and an existential type

查看:130
本文介绍了未定义的抽象类型成员和存在类型之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于未初始化的抽象类型成员=:=等于存在类型

Given an uninitialised abstract type member is =:= equal to an existential type

implicitly[Undefined =:= x forSome { type x }]   // ok

那为什么它们之间似乎有所不同

then why there seems to be a difference between them in

object O {
  type Undefined

  implicitly[Undefined =:= _]   // ok

  def g[F[_]](fun: F[_] => F[_]) = ???
  def h[F[_]](fun: F[Undefined] => F[Undefined]) = ???

  g[List](l => List(42))   // ok
  h[List](l => List(42))   // error
}

请注意g如何编译,而h会引发类型不匹配错误.再考虑

Note how g compiles whilst h raises type mismatch error. Furthermore consider

object O {
  type Undefined
  type Existential = x forSome { type x }

  implicitly[Undefined =:= x forSome { type x }]   // ok
  implicitly[Undefined =:= Existential]            // error
}

如果Undefined等于x forSome { type x },并且x forSome { type x }等于Existential,那么为什么Undefined不等于Existential?

If Undefined equals x forSome { type x }, and x forSome { type x } equals Existential, then why does Undefined not equal Existential?

推荐答案

您错过了方括号:

implicitly[Undefined =:= (x forSome { type x })]

因此它无法编译.

它们之间应该有所区别.他们是不同的.

There should be difference between them. They are different.

implicitly[Undefined <:< (x forSome { type x })] 

反之亦然.

实际上x forSome { type x }只是Any.

implicitly[Undefined =:= _]是什么意思?

implicitly[Undefined =:= _]implicitly[(Undefined =:= x) forSome {type x}].

Undefined =:= x对于某些x是正确的.就是Undefined.

And Undefined =:= x is true for some x. Namely for Undefined.

这篇关于未定义的抽象类型成员和存在类型之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆