我正在阅读斯卡拉之旅:抽象类型.什么时候使用抽象类型更好?
例如,
abstract class Buffer {
type T
val element: T
}
Run Code Online (Sandbox Code Playgroud)
而是那些泛型,例如,
abstract class Buffer[T] {
val element: T
}
Run Code Online (Sandbox Code Playgroud) 有路径依赖的类型,我认为这是可能的,表达了这样的语言作为警句或阿格达Scala中的几乎所有功能,但我不知道为什么斯卡拉不支持此更明确地像它非常漂亮,在其他领域(比如,DSLs)?我缺少的任何东西都像"没有必要"?
List("a").contains(5)
Run Code Online (Sandbox Code Playgroud)
因为a Int永远不会包含在列表中String,所以这应该在编译时生成错误,但事实并非如此.
它浪费并默默地测试String列表中包含的每个内容的相等性5,这些内容永远不会成立(在Scala中"5"永远不等于5).
这被称为" '包含'问题 ".而一些人暗示,如果一个类型系统无法正确地输入这样的语义,那么为什么要经过强制执行类型的额外的努力.所以我认为这是一个需要解决的重要问题.
类型参数化B >: A的List.contains输入的任何类型的是该类型的超类型A(包含在列表中的元素的类型).
trait List[+A] {
def contains[B >: A](x: B): Boolean
}
Run Code Online (Sandbox Code Playgroud)
此类型参数化是必要的,因为+A声明列表在类型上是协变的A,因此A不能在逆变位置中使用,即作为输入参数的类型.协变列表(必须是不可变的)对于扩展比强制列表(可以是可变的)更强大.
A是String在上面的例子有问题,但Int不是的超类型String,所以发生了什么事?该隐含包容 Scala中,决定Any是两者的相互超String和Int.
Scala的创建者Martin Odersky 建议修复将限制输入类型B仅限于那些具有equals方法的类型Any.
trait List[+A] …Run Code Online (Sandbox Code Playgroud) 感谢这篇文章,我正在了解依赖方法类型.我有一个类似于以下的结构
trait Environment{
type Population <: PopulationBase
protected trait PopulationBase
def evolveUs(population: Population): Population
}
object FactoredOut{
def evolvePopulation(env: Environment)(prevPopulation: env.Population): env.Population = {
env.evolveUs(prevPopulation)
}
}
Run Code Online (Sandbox Code Playgroud)
我现在想开始使用actor在FactoredOut整个集群中传播工作.要做到这一点,我需要一种方法来传递带有的不可变消息Environment.
显然以下不起作用,但演示了我正在尝试做的事情
object Messages{
case class EvolvePopulation(env: Environment)(prevPopulation: env.Population)
}
Run Code Online (Sandbox Code Playgroud)
通过人口的正确方法是什么?周围的环境是什么?
(会添加dependent-method-types标记,但是我没有足够的点来添加'new'标记)
我希望编写一个Scala方法,它接受任意大小和类型的元组以及索引,并返回该索引处元组中的元素.我知道如何做所有事情,但保留类型.我还没有想出一种方法来使返回值成为元组项的动态类型.
这是我到目前为止的功能:
def subscript_get(tup: Product, index:Int): Any={
return tup.productElement(index)
}
Run Code Online (Sandbox Code Playgroud)
例如,用法是:
subscript_get((0,1,2,3),0)
--> Int = 0
subscript_get((0,1,"asdf",3),2)
--> java.lang.String = asdf
我知道我可以将结果反馈给我正在寻找的东西,但这对我不起作用,因为我不能总是知道我应该投射到什么类型.
这样的事情甚至可能吗?谢谢!
我正在尝试使用来自Shapeless的HList.
这是我的第一次尝试:
trait Column[T] {
val name: String
}
case class CV[T](col: Column[T], value: T)
object CV {
object columnCombinator extends Poly2 {
implicit def algo[A] = at[(String, String, String), CV[A]] { case ((suffix, separator, sql), cv) ?
(suffix, separator, if (sql == "") cv.col.name+suffix else sql+separator+cv.col.name+suffix)
}
}
def combine[A <: HList](columns: A, suffix: String, separator: String = " and ")
(implicit l: LeftFolder[A, (String, String, String), columnCombinator.type]): String =
columns.foldLeft((suffix, separator, ""))(columnCombinator)._3
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道foldLeft这个例子中返回了什么.
我希望它返回(String, …
该文章通过Scala的解释依赖注入Cake Pattern.
我对这种模式的好处的理解是,特征可以与(生产对测试)和静态检查混合在一起.
在Bonér先生的例子中,他列出了这个完成的(每个例子)代码:
UserRepositoryComponent和UserServiceComponent
我根据我的理解添加了评论.
trait UserRepositoryComponent {
val userRepository: UserRepository // stand-alone component
class UserRepository {
... // actual implementation here
}
}
trait UserServiceComponent {
this: UserRepositoryComponent => //Requires a mixed-in UserRepo*Component
val userService: UserService
class UserService {
... // actual implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,这Service取决于注射Repository成分.
出于生产目的,可以使用以下内容将"生产" Repository组件连接到UserServiceComponent:
object ComponentRegistry extends
UserServiceComponent with
UserRepositoryComponent
{
val userRepository = new UserRepository
val userService = new …Run Code Online (Sandbox Code Playgroud) 我很难为这个我要组装的类层次结构制作解决方案.我有一个抽象的数据包"Vertex",以及一个在Vertex实例上运行的抽象类"VertexShader".事实上,VertexShader的派生类在特定的Vertex派生类上运行.它很像Animal类的经典例子def eat(f : Food),但是它的子类只能吃特定种类的食物.
我想问题是派生的Vertex类应该提供一个对顶点进行操作的函数"+",我需要将这个操作的结果传递给VertexShader.问题是系统不允许我将'+'操作的结果传递给VertexShader对象,即使这些类型都通过推理正确解析.
有关如何重新设计以避免类型问题的任何建议都非常受欢迎.
/// Abstract Interface Types
trait Vertex
{
type V <: Vertex
def position : Float
def + (v : V) : V
}
/// Derived class of vertex shader will use a specific derived class of
/// vertex that it can shade
trait VertexShader
{
type V <: Vertex
def shade( v : V ) : Float
}
/// Concrete Implementation Example
class MyVertex(p : Float, c : Float) extends Vertex
{ …Run Code Online (Sandbox Code Playgroud) 在斯卡拉除疣解决以下问题:
我的问题是:Scala WartRemover是否解决了杨章批评的问题?
scala ×9
generics ×2
shapeless ×2
akka ×1
contains ×1
covariance ×1
inheritance ×1
list ×1
tuples ×1
type-systems ×1
types ×1