Scala's Advanced app features

Source: Internet
Author: User

The role of app traits
App特质的作用那就是延迟初始化,从代码上看它继承自DelayedInit,里面有个delayedInit方法trait App extends DelayedInit   DelayedInit特质里定义了延迟初始化方法:def delayedInit(x: => Unit): Unit开发者可以直接在初始化块里写逻辑,(这里指的是 extends App{//todo}里的//todo代码)然后编译器会把这段初始化代码块里的逻辑封装成一个函数对象(是(() => Unit)类型)override def delayedInit(body: => Unit) {initCode += (() => body)}缓存起来(并没有运行),然后放到一个集合(ListBuffer)中,之后在main方法里一行一行调用并执行,  所以只有在执行到main方法的时候才会触发,从而达到延迟初始化的效果。def main(args: Array[String]) = {...for (proc <- initCode) proc()...}不过在实际开发中,经常需要一开始就初始化,不然会报错,如空指针异常,真正使用App的机会个人感觉都不多

Object Appinternals extends app{  def testapp{    val c =new c    println ("3. Hello  Spark ")  }}trait Helper extends delayedinit{   def delayedinit (body: = = Unit) ={     println (" 1. Dummy text, printed before inititalization of C ")     body  //evaluates The initialization code of C   }}class c exte NDS helper{   println ("2. This is  the initialization code of C ")}object apptest {    def main (args:array[string]) {    Appinternals.testapp    }}

Operation Result:

   1. Dummy text, printed before inititalization of C   2. This is the initialization code of C   3. Hello Spark

  

question: How to pass the encapsulated initialization code block to Delayedinit (body: = = Unit)?

With the anti-compilation tool Jd-gui.exe the. Class generated above, you can see a lot more classes,
Among the major
Class Appinternals, Delayedinitbody,appInterNAls , C, Delayedinitbo< Span id= "mathjax-span-24" class= "Mi" >dy ,h Elp er ,h Elp er class

Delayedinit$body appears two times, once in Appinternals, once in C, it has a method
Apply ()

Each corresponds to a

Public final Object apply ()//appinternals    {this      . $outer. C_$eq (new C ());      Predef. Module$.println ("Hello Spark");      return boxedunit.unit;}    Public final Object apply () {//c    predef. Module$.println ("This is the initialization code of C");    return boxedunit.unit;}

  

As you can see from the first apply, it has encapsulated the code blocks in the example code.

  


From the second apply you can see that it puts the C

  


Code blocks are wrapped up
Finally in class HelperCLASSInSurfaceUsePUBLICSTATICVoi dd el ay ed in it (h Elp er This, Function0 body)

  

This is the order in which the initialization code blocks are executed, and the Delayedinit method is called in the Main method, first executing

Predef. MODULE$.PRINTLN ("dummy text, printed before initialization of C");

  

Then call BODY.APPLYMCVSP (); BODY.APPLYMCVSP () First Call This.outer.c_$eq (new C ()); Predef. Module$.println ("Hello Spark");

  

So the This.outer.c_$eq is executed first (new C ());

Execute statement when new C executes

println ("This is the initialization code of C")

  

Then execute

println ("Hello Spark");

  

So the result of the output is this.

Dummy text, printed before initialization of C this is the initialization code of C Hello Spark

  

Supplementary content is the majority of their own speculation, do not guarantee the right, the personal feel behind the principle of actually do not delve into, know that there is such a thing on the line.

Scala's Advanced app features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.