【转】Scala: Example use for early definition / early initializer / pre-initialized fields

原文链接 http://stackoverflow.com/questions/16348541/scala-example-use-for-early-definition-early-initializer-pre-initialized-fi#


 

Let‘s see a example from the Programming in Scala book (page 451). If we have a definition like this:

trait RationalTrait {
   val numerArg: Int
   val denomArg: Int
}

Then numerArg and denomArg are called abstract vals & the trait can be used directly without extends, like this:

val x = new RationalTrait {
   val numerArg = 1
   val denomArg = 2
}

Or

val y = new {
   val numerArg = 1
   val denomArg = 1
} with RationalTrait

The above two are both valid Pre-initializing of abstract val in trait, except that when you need to put an expression value to abstract vals, you can only use the later form, like this:

val z = new {
  val numerArg = 1 * x
  val denomArg = 2 * x
} with RationalTrait

Another interesting example in book is to Pre-initialized fields in a class definition.

class RationalClass(n: Int, d: Int) extends {
  val numerArg = n
  val denomArg = d
} with RationalTrait {
  def + (that: RationalClass) = new RationalClass(
    numer * that.denom + that.numer * denom,
    denom * that.denom
  )
}

时间: 2024-08-12 11:57:28

【转】Scala: Example use for early definition / early initializer / pre-initialized fields的相关文章

快学Scala第一部分

转载: 1.变量声明 [java] view plaincopy val answer = 8 * 5 + 2; //常量 var counter = 0;    //变量 //在必要的时候 ,可以指定类型 val greeting:String = null val greeting:Any = "Hello" //可以将多个值或变量放在一起声明 val xmax, ymax = 100 //xmax 和 ymax设为100 var greeting, message:String 

Scala课堂(2):基础(一)

<p> <strong style="color: #555555;">这里我们转载<a style="font-weight: inherit; font-style: inherit; color: #0085cf;">Twitter的Scala课堂</a>  ,转载的内容基本来自Twitter的Scala课堂中文翻译,部分有小改动.</strong> </p> <p> <

Linux Programe/Dynamic Shared Library Entry/Exit Point &amp;&amp; Glibc Entry Point/Function

目录 1. 引言 2. C/C++运行库 3. 静态Glibc && 可执行文件 入口/终止函数 4. 动态Glibc && 可执行文件 入口/终止函数 5. 静态Glibc && 共享库 入口/终止函数 6. 动态Glibc && 共享库 入口/终止函数 1. 引言 0x1: glibc Any Unix-like operating system needs a C library: the library which defines t

debian内核代码执行流程(一)

本文根据debian开机信息来查看内核源代码. 系统使用<debian下配置dynamic printk以及重新编译内核>中内核源码来查看执行流程. 使用dmesg命令,得到下面的开机信息: [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Linux version 3.2.57 ([email protected]) (gcc versio

la3211

2-sat+二分... 每次二分答案然后连边2-sat...边要开到n*n 样例水得跟没有一样... #include<bits/stdc++.h> using namespace std; const int N = 4010; struct edge { int nxt, to; } e[N * N << 1]; int n, cnt = 1, top, Time, cot; int dfn[N], low[N], vis[N], st[N], early[N], late[N

make menuconfig简介

Y--将该功能编译进内核  N--不将该功能编译进内核 M--将该功能编译成可以在需要时动态插入到内核中的模块  如果你是使用的是 make xconfig,那使用鼠标就可以选择对应的选项.这里使用的是 make  menuconfig,所以需要使用空格键进行选取.在每一个选项前都有一个括号, 有的是中括号有的是尖括号,还有圆括号.用空格键选择时可以发现,中括号里要么是空,要么是"*": 而尖括号里可以是空,"*"表示编译到内核里:"M"表示编译

sandy bridge

  SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Linley Gwennap  {9/27/10-01} ................................................................................................................... Intel’s

Android电源管理-休眠简要分析

工作需要,需要对这一块深入学习.故在此做一点分析记录,存疑解惑. 一.开篇 1.Linux 描述的电源状态 - On(on)                                                 S0 -  Working - Standby (standby)                              S1 -  CPU and RAM are powered but not executed - Suspend to RAM(mem)        

Linux电源管理【转】

转自:http://www.cnblogs.com/sky-zhang/archive/2012/06/05/2536807.html PM notifier机制: 应用场景: There are some operations that subsystems or drivers may want to carry out before hibernation/suspend or after restore/resume, but they require the system to be