Scala: Get value of child from parent? -


trying values of fields of child parent class this:

        (field <- this.getclass.getdeclaredfields) {           logger.debug(field.getname)           field.get(this)         } 

and got error

exception: class models.model$$anonfun$4 can not access member of class models.good modifiers "private" @ line

field.get(this) 

in class don't have private fields:

class good(id: option[string]) extends model[realgood](id){   lazy val title: string = this.load[string](realobject.get.title)   lazy val cost: double = this.load[double](realobject.get.cost) } 

what's wrong code?

as hinted in comments, scala's conversion java bytecode isn't straightforward (though it's pretty predictable, once hang of it). in particular, public fields in scala compile private field public getter in java bytecode:

fukaeri:~ dlwh$ cat zzz.scala class good(id: option[string]) {   lazy val title: string = ???   lazy val cost: double = ??? }  fukaeri:~ dlwh$ scalac zzz.scala fukaeri:~ dlwh$ javap -private compiled "zzz.scala" public class {   private java.lang.string title;   private double cost;   private volatile byte bitmap$0;   private java.lang.string title$lzycompute();   private double cost$lzycompute();   public java.lang.string title();   public double cost();   public good(scala.option<java.lang.string>); } 

you can see good has private fields each of declared public fields, in addition public getters. because fields lazy val, have computation methods initialization, , there's bitmap$0 field ensure lazy vals initialized once.

in loop, can use field.setaccessible(true) fix exception.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -