加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

我们可以在Scala中定义更高级别的类型级别的身份功能吗?

发布时间:2020-12-16 09:21:10 所属栏目:安全 来源:网络整理
导读:在Scala中,我们可以定义较低类型的类型级标识函数, type Id[A] = A 我们也可以定义类似于较高种类的类型? IE浏览器.我们可以填写空格, type HKId[A[...]] = ... 所以类似于HKId [List]的东西让我们回到List类型的构造函数? 绑定自由名字的东西,如, type Fo
在Scala中,我们可以定义较低类型的类型级标识函数,

type Id[A] = A

我们也可以定义类似于较高种类的类型? IE浏览器.我们可以填写空格,

type HKId[A[...]] = ...

所以类似于HKId [List]的东西让我们回到List类型的构造函数?

绑定自由名字的东西,如,

type Foo[X] = List[X]
val l : Foo[Int] = List(1,2,3)

可能导致我们期望更高级别的类型身份看起来像,

type HKId[A[X]] = A[X]

但是scalac抱怨在RHS上找不到X型.

有没有一些聪明的编码,会做的伎俩?还是现在不可能?

解决方法

HKId [A [X]] = …是更高阶类型参数.它的作用域为type parameter子句,通常在类型约束中引用.见§4.4规范:

The above scoping restrictions are
generalized to the case of nested type
parameter clauses,which declare
higher-order type parameters.
Higher-order type parameters (the type
parameters of a type parameter t ) are
only visible in their immediately
surrounding parameter clause (possibly
including clauses at a deeper nesting
level) and in the bounds of t .
Therefore,their names must only be
pairwise different from the names of
other visible parameters. Since the
names of higher-order type parameters
are thus often irrelevant,they may be
denoted with a ‘_’,which is nowhere
visible.

不久之后,我们讨论了为类型函数添加文字语法的可能性,例如[A] [Int,A].这在Scalaz中真的很有用.在此期间,我们使用Alexey的答案,在PartialApplyXofY traits表达的伎俩.推论会更好,但这是非常棘手,尽管innocuous entry in Trac!)

无论如何,在该线程中,Adriaan mentioned:

It obviously won’t be trivial to
implement everything that logically
follows from having anonymous type
functions,as we currently don’t have
the necessary infrastructure to allow
people to write higher-kinded type
aliases,for example:

type MyTypeFun = [X,Y] Pair[Y,X] //
desirable,but hard to support with
the current implementation (we look at
the type params of a symbol to infer
its kind)

UPDATE

原来你可以很亲密了:

def hk[_[_]] = (); 
hk[({type A[X] = X})#A]

或者有点创意:

def hk[_[_]] = (); hk[({type [X] = X}) #  ]
def hk[_[_]] = (); hk[({type λ[α]=α})#λ ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读