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

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -