F[_] 和 F[T] 在 Scala 中用于类型构造函数时的区别 [英] Difference between F[_] and F[T] In Scala when used in type constructors

查看:46
本文介绍了F[_] 和 F[T] 在 Scala 中用于类型构造函数时的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于在类型构造函数中使用的 _ 而不是在定义存在类型时使用.

所以问题是当 _ 用作类型参数而不是像 T 这样的变量时有什么区别.例如 F[_]F[T] 之间的区别.

我能想到的唯一区别是,使用 F[_] 参数本身可以有尽可能多的孔......那就是 F[_]可以变成 F[Int]F[Future[Option[Int]]] 等等...而当你有 F[T]T 只能是一个正确的类型...即 F[String]F[Int] 等.

这是一个正确的假设吗?这是 F[_]F[T] 之间的主要区别吗?或者还有更多?

两者作为类型参数的情况呢?例如,trait Functor [F[_]]trait Functor [F[T]] 有什么区别?

如果将 functor trait 定义为 trait Functor [F[_]] 而不是 trait Functor [F[T]],是否有任何语义差异?>

解决方案

引用 规范:

<块引用><块引用>

上述范围限制适用于声明高阶类型参数的嵌套类型参数子句的情况.高阶类型参数(类型参数 t 的类型参数)仅在其紧邻的参数子句(可能包括更深嵌套级别的子句)和 t 的边界中可见.因此,它们的名称只能与其他可见参数的名称成对不同.由于高阶类型参数的名称因此通常是不相关的,因此它们可能用 _ 表示,这在任何地方都不可见.

示例

以下是一些格式良好的类型参数子句:

[S, T][@specialized T, U][Ex <: Throwable][A <:可比[B],B <:A][A、B>:A、C>:A<:B][M[X],N[X]][M[_], N[_]]//相当于前面的子句[M[X <: 绑定[X]],绑定[_]][M[+X] <: 可迭代[X]]

因此,如果您没有边界,例如在 Functor [F[T]] 中,则与 Functor [F[_]] 没有任何区别.>

This question is about _ as used in type constructor and not when used in defining existential types.

So the question is what is the difference when _ is used as type parameter instead of a variable like T. For example difference between F[_] and F[T].

The only difference I can think of is that with F[_] the parameter itself can have as many holes as possible...that is F[_] can become F[Int] or F[Future[Option[Int]]] etc...while when you have F[T] the T can only be a proper type...that is F[String] or F[Int] etc.

Is this a correct assumption? and is that the main difference between F[_] and F[T]? or there are more?

What about the case where the two are used as type parameters? For example, what is the difference between trait Functor [F[_]] and trait Functor [F[T]]?

Is there any semantic difference if the functor trait is defined as trait Functor [F[_]] instead of trait Functor [F[T]]?

解决方案

To quote the specification:

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.

Example

Here are some well-formed type parameter clauses:

[S, T]
[@specialized T, U]
[Ex <: Throwable]
[A <: Comparable[B], B <: A]
[A, B >: A, C >: A <: B]
[M[X], N[X]]
[M[_], N[_]] // equivalent to previous clause
[M[X <: Bound[X]], Bound[_]]
[M[+X] <: Iterable[X]]

So if you have no bounds, as in Functor [F[T]], there's no difference at all from Functor [F[_]].

这篇关于F[_] 和 F[T] 在 Scala 中用于类型构造函数时的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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