A deeper look at the Scala Syntax (Scala block)

Source: Internet
Author: User
Scala

is a object-oriented, statically-typed and functional programming language for the JVM. The strength of Scala lies in it's powerful syntax which allows to write readable, manageable code which are short and conc Ise–and It comes with great support for common design pattern (i.e. a special keyword for implementing the Singleton des IGN pattern).

There is plenty of good tutorials and introductions for learning Scala on the web, but only a few mention the great Possi Bilities programmers has to extend Scala or even build an own domain specific language using Scala without being forced t o use macros as seen in Lisp-like languages.

One interesting feature of Scala is "blocks" which work quite different then code blocks in Java and C + + although they us e the same syntax. A block in Scala is rather a sequence of expressions which are evaluated successively and it returns the value of the last Expression. Just think of them as the ' do ' function in LISP. Blocks is surrounded by curly braces and the expressions they hold is separated by semicolons.

1 2 scala> {val x = 2; x + 5} res 0:int = 7

Thanks to some syntactic sugar the semicolon can is replaced by a line breaks which becomes handy in larger blocks.

1 2 3) 4 5 scala> {val x = 5 x + 5} res 1:int = 10

Because blocks is just expressions they can be used, of course, like every other expression. They can stored in variables, returned by functions and also is used as parameters for functions:

1 2 3 4 5 6 7 8 Scala> println (2 + 5) 7

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.