Scala Case Class

Source: Internet
Author: User

Scala Case Class


Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://yjplxq.blog.51cto.com/4081353/1415533

What does case classes do again? You can just think of him as a normal class, but slightly different, summed up as follows:

    1. No need to write new, but can write

    2. The default is public, which is called from anywhere

    3. The ToString is implemented by default

    4. Cannot be inherited

Official text:

It makes define case classes if pattern matching are used to decompose data structures.

Of course, it's a bit biased to have meaning in the pattern matching, at least some old programmers have other ideas:

Get auto-generated equals, Hashcode, toString, Static apply () for shorter initialization, etc.

such as the Xia Guan Network example,

/** *  @author:  lenovo (2015-04-13 13:37)  *///  Abstract Parent class abstract class term//  case classcase class var (name: string)  extends Term// case  Classcase class fun (arg: string, body: term)  extends Term// case  Classcase class app0 (f: term, v: term)  extends termobject termtest  extends app {  def printterm (term: term)  {     Term match {      case var (n)  =>         print (n)       case fun (x, b)  =>         print ("^"  + x +  ".")         printterm (b)       case app0 (F, V)  =>        print ("(")         printterm (f)          print (" ")         printterm (v)         print (")")     }  }   Def isidentityfun (term: term): boolean = term match {     case fun (X, var (y))  if x == y => true     Case _ => false  }  val id = fun ("X",  Var ("X"))   val t = fun ("X",  fun ("Y",  app0 (Var ("X"),  var ("Y")))    Printterm (t)   println  println (Isidentityfun (ID))   println (IsIdentityFun (t))}

Run

^x.^y. (x y) TrueFalse


Scala Case Class

Related Article

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.