Scala type error for cyclically referenced traits

I'm having trouble getting this code to work. I want to make a trait that allows a class that inherits it to have "children", but apparently, Child's setParent method wants a P, but gets a Parent[P, C] instead.

package net.fluffy8x.thsch.entity import scala.collection.mutable.Set trait Parent[P, C <: Child[C, P]] { protected val children: Set[C] def register(c: C) = { children += c c.setParent(this) // this doesn't compile } } trait Child[C, P <: Parent[P, C]] { protected var parent: P def setParent(p: P) = parent = p }

-------------Problems Reply------------

You need to use self types to indicate that this is P and not Parent[P, C]. This will also require extra bounds P <: Parent[P, C] and C <: Child[C, P]

trait Parent[P <: Parent[P, C], C <: Child[C, P]] { this: P =>
protected val children: scala.collection.mutable.Set[C]
def register(c: C) = {
children += c
c.setParent(this)
}
}

trait Child[C <: Child[C, P], P <: Parent[P, C]] { this: C =>
protected var parent: P
def setParent(p: P) = parent = p
}

Category:scala Views:0 Time:2015-02-04

Related post

  • Can't understand type errors in Scala 2009-09-23

    Here is the POC code: object TypeTest extends Application { val stuff = List(1,2,3,4,5) def joined:String = stuff.reduceLeft(_ + ", " + _) println(joined) } Upon compiling, it gives the following error: tt.scala:4: error: type mismatch; found : java.

  • Scala type programming resources 2010-12-11

    According to this question, Scala's type system is Turing complete. What resources are available that enable a newcomer to take advantage of the power of type-level programming? Here are the resources I've found so far: Daniel Spiewak's High Wizardry

  • Difference in Scala type inference, Eclipse vs. Maven 2010-11-14

    I'm getting a compilation failure using the Maven Scala plugin that I'm not getting using The Eclipse Scala IDE. First the code: package com.example trait SomeTrait[OUTPUT, INPUT] { def apply(request: INPUT, f: OUTPUT => Unit); } class SomeClass e

  • scala type woes: SoftReference, ReferenceQueues, SoftHashMap 2011-02-26

    EDIT Most answers thus far are focusing on the fact that I was incorrectly extending Map. I have corrected this in the example code, but the type woes persist, and the question still stands. I'm trying to implement a SoftHashMap in Scala, but am runn

  • Can a Scala type parameter reference itself when used as a parameter to a base-class? 2011-08-27

    If I have the following Scala type hierarchy: // Base traits trait TA[X <: TA[X,Y], Y <: TB[X,Y]] trait TB[X <: TA[X,Y], Y <: TB[X,Y]] trait TC[X <: TA[X,_]] // More specific traits trait TI[X <: TI[X,Y], Y <: TJ[X,Y]] extends TA

  • hector scala type mismatch 2011-08-31

    What could be wrong? val is = IntegerSerializer.get mutator.addInsertion(deviceId, COLUMN_FAMILY_CARSTATUS, createColumn("mileage", 111, ss, is))} ModelOperation.scala:96: error: type mismatch; [INFO] found : me.prettyprint.cassandra.serializers.Inte

  • Complex Scala Type Inference w/ Lambda Expressions 2011-10-05

    I'm working on a DSL for a experimental library I'm building in Scala, and I've run into some vexing peculiarities of Scala's type inference as it pertains to lambda expression arguments that don't seem to be covered in the book Programming In Scala.

  • Scala type inference fails to note that these types are identical, whatever they are 2012-02-18

    I have a design pattern here where there is an object generator (MorselGenerator and its children), any instance of which always generates the same exact type of object (Morsels and its children), but the type checker will not let me perform any oper

  • In ASP.NET MVC I encounter an incorrect type error when rendering a page with the correct typed object 2008-10-07

    I am developing an application using MVC Preview 5. I have used typed views. After setting debug="false", I have found that I am receiving errors of the form: "The model item passed into the dictionary is of type 'blah.Models.UserAdmin.IndexData' but

  • Scala type system : basic type mismatch 2010-04-08

    I have a basic type system type mismatch problem: I have a class with a method def Create(nodeItem : NodeItem) = {p_nodeStart.addEndNode(nodeItem)} where p_nodeStart is NodeCache class NodeCache[END_T<:BaseNode] private(node: Node) extends BaseNod

  • Problem with expressions variable 'buffer' of type 'System.Text.StringBuilder' referenced from scope '', but it is not defined 2010-07-20

    private Func GenerateWriter() { MethodInfo appendMethod = typeof(StringBuilder).GetMethod("Append", new[] { typeof(string) }); var buffer = Expression.Variable(typeof(StringBuilder), "buffer"); var writer = new List<Expression>(); var source =

  • Disadvantages of Scala type system versus Haskell? 2010-09-11

    I have read that Scala's type system is weakened by Java interoperability and therefore cannot perform some of the same powers as Haskell's type system. Is this true? Is the weakness because of type erasure, or am I wrong in every way? Is this differ

  • Scala: type mismatch issue with type Any and templated class 2011-03-31

    I have the following problem with the scala type system and I have currently no idea how to fix this. Basically there is the follow situation: I have a class, lets call it Actor. This class is templated. class Actor[T](){ def setValue(value: T): Int

  • wired scala type design 2011-10-03

    my env is : Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). watch this example first: scala> var a = 1 a: Int = 1 scala> var c = 'c c: Symbol = 'c scala> var d = 1.1 d: Double = 1.1 scala> var b = "123" b: ja

  • Scala type inference on overloaded method 2011-10-21

    Given this code: class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numerator = n / g val denominator = d / g def this(n: Int) = this(n, 1) override def toString = numerator + "/" + denominator def +(r: Rational) =

  • Why do I need an explicit evidence type / why does this Scala type bound fail? 2011-12-12

    Below, the first case succeeds and the second fails. Why do I need an explicit evidence type / why does this Scala type bound fail? What's the type inferencer's particular limitation here in solving for A? scala> implicit def view[A, C](xs: C)(imp

  • Scala type inference with generics and covariance, possible issue in scalac 2011-12-20

    I have a short list of questions that I've found today. I'm a scala novice, therefore my questions may be trivial. Let's assume we have a class like this one: abstract class A[+T] { def foo[S >: T](x: S): String } And we should provide useful chil

  • Scala type system - help understanding mismatch on type alias 2012-03-15

    I'm new to the Scala type system, and I'm trying to explore it via this JAXB Marshalling example. It works if you change the type of the parameter to toString to AnyRef. However, I would like to express via the type system that the parameter to toStr

  • Delphi: "Invalid field type" error with master/detail datasets 2008-11-09

    I asked about setting up an in-memory dataset with TClientDataset, and was told I had to use the "Create Data Set" command in the form designer. That works just fine until I try to create a master-detail database relation. If I have a field def of ty

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.115 (s). 11 q(s)