abstract-type

Scala: Abstract types vs generics

随声附和 提交于 2019-12-16 22:55:12
问题 I was reading A Tour of Scala: Abstract Types . When is it better to use abstract types? For example, abstract class Buffer { type T val element: T } rather that generics, for example, abstract class Buffer[T] { val element: T } 回答1: You have a good point of view on this issue here: The Purpose of Scala's Type System A Conversation with Martin Odersky, Part III by Bill Venners and Frank Sommers (May 18, 2009) Update (October2009): what follows below has actually been illustrated in this new

Why is this an invalid use of Scala's abstract types?

若如初见. 提交于 2019-12-11 10:46:56
问题 I have this code: class A extends Testable { type Self <: A } class B extends A { type Self <: B } trait Testable { type Self def test[T <: Self] = {} } object Main { val h = new A // this throws an error h.test[B] } And my error is: error: type arguments [B] do not conform to method test's type parameter bounds [T <: Main.h.Self] h.test[B] On this question, it was said that this was due to path dependent types. Can anyone figure out how to have T <: Self, without having the path-dependent

What does 'do not conform to trait Builder's type parameter bounds' mean in scala?

有些话、适合烂在心里 提交于 2019-12-11 08:21:43
问题 I have the following simple program that defines 2 identical upper bounds for type parameter and abstract type alias respectively: package scala.spike.typeBoundInference object Example1 { trait Domain { } trait Impl { type DD <: Domain type GG <: StaticGraph[DD] type A1 type A2 type A3 // ... this type list can go very long // so inlining them as generic type parameters is impossible final type Builder = StaticGraph.Builder[DD, GG] } trait DSL[I <: Impl] { val impl: StaticGraph.Builder[I#DD,

What is the meaning of a type declaration without definition in an object?

蹲街弑〆低调 提交于 2019-12-10 14:55:53
问题 Scala allows to define types using the type keyword, which usually have slightly different meaning and purpose depending on when they are declared. If you use type inside an object or a package object, you'd define a type alias, i.e. a shorter/clearer name for another type: package object whatever { type IntPredicate = Int => Boolean def checkZero(p: IntPredicate): Boolean = p(0) } Types declared in classes/traits are usually intended to be overridden in subclasses/subtraits, and are also

Scala self type and this.type in collections issue

孤者浪人 提交于 2019-12-09 16:38:25
问题 I'm trying to wrap my head around abstract and explicit self types in scala. Lets consider this example: I want to create a base for extensible tree as simple as this: trait Tree { def children: Iterable[Tree] def descendants: Iterable[Tree] = { val dv = children.view; dv ++ (dv.flatMap { _.children }) } } However, I want to be able to extend tree nodes with some methods and use these methods like: tree.children foreach { _.newMethod() } For this I've tried: A. this.type: FAIL trait Tree {

Scala: Difference between 'type A = XXX' and 'final type A = XX'?

折月煮酒 提交于 2019-12-07 02:15:06
问题 assuming I have an abstract type AA and concrete type XXX: trait AA { type A = XXX final type B = XXX } In this case in any subclass of AA, both type A and B cannot be overriden, so it appears that the keyword final is completely redundant. Is this statement correct? 回答1: It's hard to prove that they're exactly identical, but I'm going to argue that they are, minus a few useless quirks. Useless quirks First and most obviously, they give different error messages. But that's not all: it's

Constructing subclasses from base abstract class

夙愿已清 提交于 2019-12-06 00:01:28
I want to define a constructor in an abstract class that will create concrete subclasses. abstract class A { type Impl <: A def construct() : Impl = { val res = new Impl() //compile error: class type required but A.this.Impl found // do more initialization with res } } class B extends A {type Impl = B} class C extends A {type Impl = C} //... val b = new B b.construct() // this should create a new instance of B What is wrong here? Is this even possible to implement? EDIT: Clarification: I want to abstract over the construct method. I do not want to call separately new B and new C from either

Mixing type parameters and abstract types in scala

怎甘沉沦 提交于 2019-12-04 11:50:35
问题 I am trying to use the answer of a preceding question to implement a small graph library. The idea is to consider graphs as colections, where vertices wrap collection elements. I would like to use abstract types to represent Vertex and Edge types (because of type safety) and I want to use type parameters to represent the type of the collection elements (because I want to define them at instantiation easily). However, when trying the most basic example I can think about, I am stuck with

Scala abstract types in classes within objects

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:24:00
问题 If I do this: object Parent { class Inner extends Testable { type Self <: Inner } def inner = new Inner() } object Child { class Inner extends Parent.Inner { type Self <: Inner } def inner = new Inner() } trait Testable { type Self def test[T <: Self] = {} } object Main { // this works val p: Parent.Inner = Child.inner // this doesn't val h = Parent.inner h.test[Child.Inner] } I get this error: error: type arguments [Child.Inner] do not conform to method test's type parameter bounds [T <:

Mixing type parameters and abstract types in scala

风格不统一 提交于 2019-12-03 06:27:56
I am trying to use the answer of a preceding question to implement a small graph library. The idea is to consider graphs as colections, where vertices wrap collection elements. I would like to use abstract types to represent Vertex and Edge types (because of type safety) and I want to use type parameters to represent the type of the collection elements (because I want to define them at instantiation easily). However, when trying the most basic example I can think about, I am stuck with compile errors. Here is the example: package graph abstract class GraphKind[T] { type V <: Vertex[T] type G <