In-depth analysis of Scala's app features __scala

Source: Internet
Author: User

Today, a friend of mine asked me questions about the app trait call, and I went to spend some time grooming it.
Let's look at the app source first.

What is app trait

Trait App refers to Scala.app, our single object can be run with this trait, without having to define the main method, see the code

    Object Test1 {
      def main (args:array[string]) {
        println ("Run"
          }}

    object Test2 extends
        app{ println ("also run")
    }

The results of the operation will naturally print the corresponding content, so let's see what we did to Test2:
The ability to do this is because the app trait declares the main method with the appropriate signature, and is inherited by our own single Instance object, so that the code between curly braces is collected into the primary constructor of the single instance object and executed when the class is initialized, like a scala program.
This is the 45th page of Scala programming about application, so let's imitate this action:

Trait  myapp{
println ("I ' m My App")
def Main (args:array[string]) {
    Args.foreach (x =>    println (S "${system.currenttimemillis ()}, $x")}
}

First define a trait with a main function, and print "I ' m my App" when you mix that trait.
Then print the incoming arguments in turn, and add the timestamp

Here's a single example object

    /**
  * Created by Ctao on 2015/11/13.
  * *
Object MyTest extends myapp{
  println ("I ' m Test")
  Mytest.main (Array ("Hello", "World")
  println (s) ${system.currenttimemillis ()},in my Test ")

}

First print "I ' m Test" when building the single Instance object
Then pass the argument to main
Last Print time stamp

The results show that you first enter the trait MyApp, then go to the first sentence of Test, then execute the main method, and finally print my Test to exchange the order of the sentences in mytest:

/**
  * Created by Ctao on 2015/11/13.
  * *
Object MyTest extends myapp{
  println (S "${system.currenttimemillis ()},i ' m Test")
  println (S "${ System.currenttimemillis ()},in my Test ")
  Mytest.main (Array (" Hello "," World ")

}

trait  myapp{
  println (S "${system.currenttimemillis ()},i ' m my App")
  def Main (args:array[string]) {
    Args.foreach (x => println (S "${system.currenttimemillis ()}, $x")}
}

Observe the result again:

To prove that the order in which we are running is really the first time we say that we build the trait, and the operation of the single example object is performed in the order of the statement of the single instance. then we switched main and print in the character.

/**
  * Created by Ctao on 2015/11/13.
  * *
Object MyTest extends myapp{
  println (S "${system.currenttimemillis ()},i ' m Test")
  println (S "${ System.currenttimemillis ()},in my Test ")
  Mytest.main (Array (" Hello "," World ")

}

trait  myapp{
  def main (args:array[string]) {
    Args.foreach (x => println (S "${system.currenttimemillis ()}, $x))
  }
  println (S "${system.currenttimemillis ()},i ' my App")
}

Observe the result again:

The first entry here is the "I ' My App", which shows that the trait is built first, and that the location of the main function is directly related to the invocation of a single instance object. Debug

Observe debug value stack changes


Conclusion:
In the invocation process the trait is preceded by a single instance object, and the invocation of the main method of the trait is related to the invocation of a single instance object.
We can pass parameters to the app trait with a single example object. Main (Array[string] ())

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.