ref01

https://sites.google.com/site/scalatohoku/dai-49-kai-bennkyoukai/scalanyuumon----scalade-kaki-hajime-ta-javadzukai-muke

Introduction to Scala - Scala began to write in Java for use


Usually people are using Java, Better Java as, Scala at a time when you try to touch
I tried to enumerate some useful things to know. As much as possible, java and that could be compared in scala
Focus on the items listed. Introducing Scala is less specific features.


  • Development Environment
  • Versions (2.8 and 2.7)
    • Stable: 2.7.6 (2009 year of September)
      • Since there is a bug in 2.7.5, 2.7.6 recommended
      • Soon, the'm 2.7.7. Almost the same as 2.7.5?
      • Work with JDK 1.4
      • Compatible with 2.7 .* sources do not have much in the series.
        • Older sample program does not work.
      • 2.7 .* was not in the series is binary compatible.
        • Generated, class file content unusual.
        • must re-compile.
    • 2.8 (under development)
      • The version I made to fix bugs here
      • Since moving Java5 (Java6 seems to be recommended)
      • New features (named parameter, etc.)
      • Re-designed collection classes
      • The release date still undecided
    • If you start writing the new software 2.8 or larger?
  • Compiler (scalac)
    • Full of options:-X,-Y
    • Check the status of the middle: Xshow-Phases-,-Xprint: cleanup
    • Extending your own: compiler plug-in
    • Interdependent sources to compile java and scala
      • Java parser has a unique
      • scalac then the javac
    • fsc (Fast Scala Compiler)
      • daemon program
  • Calling Java from scala
    • scala-library.jar class path
    • Note the name of the object Scala
      • The scala javap class files to determine
    • @ Scala . Reflect. Beanproperty Annotations
      • Friendly Java getter, setter
      • class Foo (@ reflect.BeanProperty var x) in setX, getX generated.
      • scala not seen.
  • Calling Java from scala
    • Java methods, scala what I'm supposed to reserved words are surrounded by ``
      • Thread. `Yield`
  • Grammar (Java where different)
    • The great import
      • _ * Not a complete
        • import java.awt._
      • The import line
        • import java.awt._, javax.swing._
      • Enumeration
        • import scala.collection.immutable. {Set, Map}
      • skip scala
        • import collection.immutable. {Set, Map}
      • Some exceptions
        • import collection.immutable. {Stack => _, _}
      • Aliasing
        • import java.lang. {List => JList}
      • Relative import
        • import Map._
        • Has been used frequently in Liftweb. twitter, Inc. in terms of internal use is prohibited.
      • "_root_" Package
        • The relative import used in order to remove the ambiguity of.
      • The final instance field, method import
        • class Foo {def foo (){....}}; val x = (new Foo); import x._

    • Comment / * * / can nest
    • Multiple public classes can be defined in one file.
    • More than one file defines the package possible.
    • Whatever variable name method name
      • UTF-8, symbol:>> -> \ \ etc.
      • Including $ is useless.
    • Use reserved words as variable names for method names
      • Need to invoke Java
      • Cases: Thread. `Yield`
    • alias
      • Aliasing types: type Foo = X
      • import java.lang. {List => JList}
      • put this alias:
        • from outside this inner Class convenient to specify
        • class Foo {self => ...}
    • You do not have to catch Exception
    • To override, override and useless to write
    • Create an array: Array (1,2,3)
    • Access array [] and not ()
      • Array (1,2,3) (0) ⇒ Array (1,2,3). Apply (0)
      • [] Is used only when specifying generics
    • : Methods ending in the tip side
      • /: (FoldLeft),::
    • -> Tuple2 in creating an instance of
      • Useful method of passing
        • Map.emtpy [Int, Int] + (1, 2) / / error
        • Map.emtpy [Int, Int] + ((1, 2))
        • Map.empty [Int, Int] + (1 -> 2)
    • default parameter [2.8]
      • def sum (n: Int, total: Int = 0): Int = if (n == 0) total else sum (n-1, n + total)
    • named parameter [2.8]
      • def foo (widht: Int, height: Int) = {...}; foo (height = 100, width = 200)
    • You can not specify a primitive type
      • Useless boxed, unboxed may be inefficient code generation occurs in the operation
      • @ Specialized annotations [2.8]
    • Methods can be nested
    • Method arguments are always final
    • trait in a Mix-in multiple inheritance
    • var Variable name: type = Expression
    • val Variable name: type = Expression
    • def method name (parameter name: type): return type = {.....}
    • class class name [type parameter ( parameter name: type) extends ... with ... {...}
  • Flexible access modifiers
    • The default is public (public has no keywords)
    • private [this]
      class Foo {
      private [this] var x = 1
      def foo (f: Foo) {
      fx / / compile error
      }
      }
    • private [Class name], private [package]
      package outerpkg.innerpkg
      class Outer {
      class Inner {
      private [Outer] def f ()
      private [innerpkg] def g ()
      private [outerpkg] def h ()
      }
      }
    • protected [C] has protected or private [C] (may be wrong.)
    • If object is a companion class to the relationship between the private and unlimited
  • Simplified notation
    • Delimited text; can substitute newline
    • Method call. (Period) may be omitted
    • Multi-Line String Literals: "" "create a simple string 
      "" "Foo
      | Bar "" ". StripMargin

    • omit return
      • Returns the last value
      • De無I end of the block is useless and expression. Definition statement (val, var, def, etc.) are useless.
    • skip to apply
    • update method
    • Assignment Operators: + =,:: =, etc.
      class Foo {def * (x: Int )=...}; var x = (new Foo) 
      a *= x ⇒ x = x * 1
    • skip the match match case
      • val foo: Int => Int = {case 1 => 1; case n => n * foo (n-1)}
      • val foo: PartialFunction [Int, Int] = {case 1 => 1; case n if (n> 1) => n * foo (n-1)}
    • implicit anonymous functions
      • Placeholder Systax for Anonymous Function
      • __ + __   ⇒ (x, y) => x + y
    • new {....} ⇒ new AnyRef {...}
    • Type can be omitted if the type inference
    • Class definition: class Foo (var name: String)
      • setter, getter automatically generated
  • Control constructs
    • if else, for, while, do while, try catch finallly, match case
    • if, try returns the value
    • is syntactic sugar for (map, filter, flatmap, foreach) (6.19 For-Comprehensions)
    • synchronized, asInstanceOf, isInstanceOf methods of the Any class
    • break, continue to define yourself is necessary if not ⇒
    • while the return value
      • Always return type Unit
      • 書Ki換Emashou tail recursive
  • Lazy evaluation
    • lazy val: Delay initialization of variables
    • call-by-name (call name)
      • The introduction of its own control constructs
      • def If [X] (b: => Boolean) (x: => X) (y: => X) = if (b) x else y
  • Variadic method
    • 4.6.2 Repeated Parameters
    • Variable-length argument, Seq [_] be treated as
      • def foo (x: Int *) {x.foreach (println (_))}
    • Variable-length argument lists apply
      • foo (List (1,2,3 ):_*)
  • Scala API
    • http://www.scala-lang.org/node/216
      • scaladoc, vscaladoc (javascript searched, stopped development)
    • scala.Predef
    • scala.actors
    • scala.concurrent
      • easily use the java Thread
      • spawn, MailBox, Channel
    • scala.collection
      • mutable, immutable collection classes have
      • The Predef, immutable collection is default
    • scala.xml
    • scala.io
    • scala.util.parsing.combinator
      • Parser combinators
    • class scala.Option
      • I no longer want to write null
      • if (b! = null ){...} ⇒ b.foreach (...)
      • if (b! = null ){...} else {...} ⇒ b.map (...). getOrElse (...)
    • class scala.Either
      • To hold the two states. For example, the normal value error
      • The Either type is Often Used as an Alternative to Scala.option WHERE Left Represents failure (by Convention) and Right is Akin to Some .
    • trait scala.Iterable
      • map, flatMap, foldLeft, foldRight, filter, foreach, mkString, etc.
    • trait scala.Functionn, n where the 22-0
    • trait scala.PartialFunction
    • class scala.Tuplen, n where the 22-0
  • Variable definition
    • var: of re-assignment
    • val: re-assignment of non-java equivalent of the final
    • val Variable name: type = Expression
    • Type can be omitted if possible type inference
    • Class, when implemented, must specify the initial value
    • var foo: String = _ Specify the null
  • Method Definitions
    • def method name (parameter name: type): return type = {.....}
    • All methods ( no built-in operator )
    • Methods can be nested
    • Method arguments are always final
    • Prefix unary operator can be defined + -! ~ (6.12.1 Prefix Operations)
    • A final tail-recursive function optimization
    • Tail recursion optimization between [2.8, scala.util.control.TailRec]
  • Type
    • Any root class.
    • The Any and AnyVal AnyRef
    • int, byte type is not primitive and
    • Void is equivalent to the Java Unit
    • Generics: covariant, contravariant
    • Structural types: duck typing
      • val foo: {def length (): Int} = "foo"
  • Function objects
    • an implementation of the trait Function *
    • I have to apply methods
    • Optional argument types in the type inference
    • (X: Int) => x +1 is a new Function1 [Int, Int] {def apply (x: Int) = x +1} abbreviation
    • Function1 trait is, compose andThen you have a
    • Function. {Curried, uncurried}
    • Method to generate a function object
      • def foo = (); foo _
  • Trait 
    • Interface with the implementation
    • Mix-in multiple inheritance
    • Class Foo extends Bar with T1 with T2
    • Trait linearlize specified in the order
  • case class
    • getter, stter
    • apply
    • extractor (unapply, unapplySeq)
    • For one argument, andThen, compose 
    • copy [2.8]
    • Implementing Function * automatically
  • object: Singleton language-level support
    • Instead of Java's static
    • Dynamicvariable: Safe to have a thread state
      • Cases: scala.Console
    • 5.4 Object Definitions
  • pattern match
    • case, for, pattern matching assignment
      • val hd:: tail = List (1,2,3)
    • Regular expression pattern matching
    • extractor: unapply, unapplySeq objects with
    • case class
      • extractor automatically generated
    • Infix Operation Pattern
      • 2 extractor with an argument between the write
      • case class <--->( x: Int, y: Int); foo match {case x <-> y =>}
    • Pattern matching can not use the type parameter.
    • Actor receive messages
    • try catch
  • implicit conversion
  • xml value
    • XPath-like access: \, \ \ 
    • <foo> <bar> <boo> goo </ boo> </ bar> </ foo> \ \ "boo"
Comments