什么是什么时候使用Scala的forSome关键字? [英] What is and when to use Scala's forSome keyword?

查看:125
本文介绍了什么是什么时候使用Scala的forSome关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List[T] forSome {type T}List[T forSome {type T}]有什么区别?如何用英语"阅读它们?我应该如何 grok forSome关键字? forSome的一些实际用途是什么?有什么比简单的T forSome {type T}用法有用的实用且更复杂的方法?

What is the difference between List[T] forSome {type T} and List[T forSome {type T}]? How do I read them in "English"? How should I grok the forSome keyword? What are some practical uses of forSome? What are some useful practical and more complex than simple T forSome {type T} usages?

推荐答案

注意:(更新2016-12-08 )forSome关键字很可能与根据Martin Odersky在ScalaX 2016上的演讲,显示了Scala 2.13或2.14.将其替换为路径依赖类型或匿名类型属性(A[_]).在大多数情况下,这是可能的.如果您遇到不可能的情况,请重构代码或放松类型限制.

Attention: (Update 2016-12-08) The forSome keyword is very likely going away with Scala 2.13 or 2.14, according to Martin Odersky's talk on the ScalaX 2016. Replace it with path dependent types or with anonymous type attributes (A[_]). This is possible in most cases. If you have an edge case where it is not possible, refactor your code or loosen your type restrictions.

通常,当您使用通用API时,API会向您保证,它可以与您提供的任何类型一起使用(不超过某些给定的约束).因此,当您使用List[T]时,List API向您保证它将与您提供的任何T 类型一起使用.

Usually when you use a generic API, the API guarantees you, that it will work with any type you provide (up to some given constraints). So when you use List[T], the List API guarantees you that it will work with any type T you provide.

forSome(所谓的存在量化类型参数)相反. API 将提供一种类型(不是您),并且向您保证,它将与您提供的这种类型一起使用.语义是,一个具体对象将为您提供T类型的东西.同一对象也将接受它为您提供的东西.但是,其他任何对象都不能与这些T一起使用,也没有其他对象可以为您提供类型为T的东西.

With forSome (so called existentially quantified type parameters) it is the other way round. The API will provide a type (not you) and it guarantees you, it will work with this type it provided you. The semantics is, that a concrete object will give you something of type T. The same object will also accept the things it provided you. But no other object may work with these Ts and no other object can provide you with something of type T.

存在量化"的想法是:(在实现中)存在(至少)一种类型的T以履行API的约定.但是我不会告诉你它是哪种类型.

The idea of "existentially quantified" is: There exists (at least) one type T (in the implementation) to fulfill the contract of the API. But I won't tell you which type it is.

forSome的读法类似于:对于某些T类型,API合同成立.但是,并非所有类型的T都必须为true.因此,当您提供某种类型的T(而不是API实现中隐藏的类型)时,编译器无法保证您使用了正确的T.因此它将引发类型错误.

forSome can be read similar: For some types T the API contract holds true. But it is not necessary true for all types T. So when you provide some type T (instead of the one hidden in the implementation of the API), the compiler cannot guarantee that you got the right T. So it will throw a type error.

因此,当您在API中看到List[T] forSome {type T}时,您可以像这样阅读:API将为您提供某种未知类型TList.它将很乐意接受此列表,并且可以使用它.但是它不会告诉您T是什么.但是至少您知道列表的所有元素都是相同的T类型.

So when you see List[T] forSome {type T} in an API, you can read it like this: The API will provide you with a List of some unknown type T. It will gladly accept this list back and it will work with it. But it won't tell you, what T is. But you know at least, that all elements of the list are of the same type T.

第二个比较棘手.同样,API将为您提供List.而且它将使用某种类型的T而不告诉您T是什么.但是可以为每个元素选择不同的类型.真实的API会为T建立一些约束,因此它实际上可以与列表的元素一起使用.

The second one is a little bit more tricky. Again the API will provide you with a List. And it will use some type T and not tell you what T is. But it is free to choose a different type for each element. A real world API would establish some constraints for T, so it can actually work with the elements of the list.

forSome在编写API时很有用,其中每个对象代表该API的实现.每个实现都会为您提供一些对象,并将接受这些对象.但是您既不能混合来自不同实现的对象,也不能自己创建对象.相反,您必须始终使用相应的API函数来获取一些可与该API一起使用的对象. forSome启用非常严格的封装.您可以通过以下方式阅读forSome:

forSome is useful, when you write an API, where each object represents an implementation of the API. Each implementation will provide you with some objects and will accept these objects back. But you can neither mix objects from different implementations nor can you create the objects yourself. Instead you must always use the corresponding API functions to get some objects that will work with that API. forSome enables a very strict kind of encapsulation. You can read forSome in the following way:

对于某些类型,API合同折叠正确.但是你不知道 它适用于哪种类型.因此,您不能提供自己的类型和 您不能创建自己的对象.您必须使用提供的 通过使用forSome的API.

The API contract folds true for some types. But you don't know for which types it holds true. Hence you cannot provide you own type and you cannot create your own objects. You have to use the ones provided through the API that uses forSome.

这是非常非正式的,在某些极端情况下甚至可能是错误的.但这应该可以帮助您理解这个概念.

This is quite informal and might even be wrong in some corner cases. But it should help you to grok the concept.

这篇关于什么是什么时候使用Scala的forSome关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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