How to solve Illegal inheritance from final class in Scala -
i have code in scala when define a value, call methods in object setstr , useful , amazing. when define aclass final, compiler throws exception illegal inheritance final class. in cases should use final classes , type of method calling after class initiation useful me, , question how solve problem
test("test function call") { val a: aclass = new aclass { setstr("pooya") func1(this) } } class aclass { // when declare class final, compiler raise error private var str:string="" def setstr(str:string)={ this.str=str } def amethod() = print(str) } def func1(a: aclass) { a.amethod() }
when new aclass { ... } creating anonymous class extends class. when class final cannot extended.
one way want that:
val a: aclass = new aclass import a._ setstr("pooya") func1(this) following @vladimir's suggestion, cleaner way:
val a: aclass = { val = new aclass import a._ setstr("pooya") func1(this) } now can repeat many times want without making setstr ambiguous. func1, current definition, should't matter if it's in {} block or not.
Comments
Post a Comment