#1921 How to override field in mixin?

Akcelisto Tue 19 Jun 2012

using concurrent::Actor

class Tree{
}

mixin TreeRef{
  abstract const Uuid idTree
  Tree tree(){
    Actor.locals[idTree.toStr]
  }
}

abstract class Thing:TreeRef{
  override const Uuid idTree
  new makeMe(Uuid idTree){
    this.idTree = idTree
  }
}

abstract const class ConstThing:TreeRef{
  override const Uuid idTree
  new makeMe(Uuid idTree){
    this.idTree = idTree
  }
}

class ThingChild : Thing{
  new makeMe(Uuid idTree):super.makeMe(idTree){

  }
}

const class ConstThingChild : ConstThing{
  new makeMe(Uuid idTree):super.makeMe(idTree){

  }
}

Compiler output:

MixinTest.fan(14,3): Const field 'idTree' cannot override field 'fantomTest::TreeRef.idTree'
MixinTest.fan(21,3): Const field 'idTree' cannot override field 'fantomTest::TreeRef.idTree'
BUILD FAILED [59ms]!

Akcelisto Tue 19 Jun 2012

I found solution. Solution is changing field in mixin to method.

mixin TreeRef{
  abstract Uuid idTree()
  Tree tree(){
    Actor.locals[idTree.toStr]
  }
}

SlimerDude Wed 22 May 2013

I was just pondering similar myself:

const mixin S58 { 
  abstract Str dude
}
const class S58Impl : S58 { 
  override const Str dude := "Stella!" (*)
}

(*) Const field 'dude' cannot override field 'S58.dude'

If there is no way to override / implement a field in a const mixin, then I'm thinking the compilation error should occur when you declare the field in the mixin - as oppose to when you attempt to override it...?

EDIT: Okay, I gettit. The compilation error is not because of the const mixin, but rather due to the const field (enforced by the const class, enforced by the const mixin).

I guess the compiler could allow const fields with a calculated setter (as they wouldn't change value) but that sounds pretty complicated.

SlimerDude Sat 10 Aug 2013

Just an update to this, to keep topics informative...

You actually can override fields in const mixins. See the topic on Synthetic fields for const mixins for details.

Login or Signup to reply.