abstract-type

Use of abstract type in a concrete class? [duplicate]

廉价感情. 提交于 2021-02-16 18:14:27
问题 This question already has answers here : Concrete classes with abstract type members (2 answers) Closed 7 years ago . scala> class A { type T <: String; def f(a: T) = println("foo")} defined class A scala> (new A).f("bar") <console>:9: error: type mismatch; found : java.lang.String("bar") required: _1.T where val _1: A (new A).f("bar") ^ Class A has an abstract type T , but is not an abstract class. Creating an object of A (as shown) does not define type T . My first thought was, I am allowed

Why is this invalid Scala?

孤人 提交于 2021-02-11 06:46:26
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Why is this invalid Scala?

我只是一个虾纸丫 提交于 2021-02-11 06:46:18
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Why is this invalid Scala?

本秂侑毒 提交于 2021-02-11 06:46:17
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Abstract type member of a singleton object

家住魔仙堡 提交于 2021-02-08 08:31:51
问题 Abstract member method is illegal in a singleton object scala> object Foo { | def g: Int | } def g: Int ^ On line 2: error: only traits and abstract classes can have declared but undefined members as is abstract value member scala> object Foo { | val x: Int | } val x: Int ^ On line 2: error: only traits and abstract classes can have declared but undefined members however abstract type member is legal in a singleton object scala> object Foo { | type A | } object Foo so clearly the sense in

Abstract type member of a singleton object

半腔热情 提交于 2021-02-08 08:31:37
问题 Abstract member method is illegal in a singleton object scala> object Foo { | def g: Int | } def g: Int ^ On line 2: error: only traits and abstract classes can have declared but undefined members as is abstract value member scala> object Foo { | val x: Int | } val x: Int ^ On line 2: error: only traits and abstract classes can have declared but undefined members however abstract type member is legal in a singleton object scala> object Foo { | type A | } object Foo so clearly the sense in

Scala Abstract type members - inheritance and type bounds

落爺英雄遲暮 提交于 2020-01-01 12:25:10
问题 I ran into some strange situation in Scala today while I tried to refine the type bounds on an abstract type member. I have two traits that define bounds on a type member and combine them in a concrete class. That works fine but when matching / casting with the trait combination only one of the two TypeBounds is "active" and I struggle to understand why ... I tried to prepare an example: trait L trait R trait Left { type T <: L def get: T } trait Right { type T <: R } now if I combine these

No dynamic binding when abstract type involved in Scala?

怎甘沉沦 提交于 2019-12-24 13:09:27
问题 When I was trying the Animal/Food example for abstract types in Martin Odersky's Programming in Scala , class Food abstract class Animal { type SuitableFood <: Food def eat(food:SuitableFood) } class Grass extends Food class Cow extends Animal { type SuitableFood=Grass override def eat(food:SuitableFood) {} } val bessy:Animal = new Cow bessy.eat(new Grass) I got the following error: scala> <console>:13: error: type mismatch; found : Grass required: bessy.SuitableFood bessy.eat(new Grass) ^

Constructing subclasses from base abstract class

旧街凉风 提交于 2019-12-22 10:28:59
问题 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

How to use Scala's this typing, abstract types, etc. to implement a Self type?

可紊 提交于 2019-12-17 06:28:28
问题 I couldn't find the answer to this in any other question. Suppose that I have an abstract superclass Abstract0 with two subclasses, Concrete1 and Concrete1. I want to be able to define in Abstract0 something like def setOption(...): Self = {...} where Self would be the concrete subtype. This would allow chaining calls to setOption like this: val obj = new Concrete1.setOption(...).setOption(...) and still get Concrete1 as the inferred type of obj. What I don't want is to define this: abstract