Code
Scala 怎样添加事件, 获得打断信号
启动 Scala 工程, 我们有时候可能需要做这样的处理: 获取打断(Interrupt )信号, 在程序关闭前做最后一些操作.
在 C 中, 我们一般可以用 signal 来实现, 而在 scala 中,我们同样有类似的机制. (more…)
Scala (/ˈskɑːlɑː/ skah-lah) is a general purpose programming language. Scala has full support for functional programming and a very strong static type system. Designed to be concise, many of Scala’s design decisions were inspired by criticism of the shortcomings of Java.
Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Java libraries may be used directly in Scala code and vice versa (language interoperability). Like Java, Scala is object-oriented, and uses a curly-brace syntax reminiscent of the C programming language. Unlike Java, Scala has many features of functional programming languages like Scheme, Standard ML and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), and anonymous types. Other features of Scala not present in Java include operator overloading, optional parameters, named parameters, raw strings, and no checked exceptions.
The name Scala is a portmanteau of “scalable” and “language”, signifying that it is designed to grow with the demands of its users.
启动 Scala 工程, 我们有时候可能需要做这样的处理: 获取打断(Interrupt )信号, 在程序关闭前做最后一些操作.
在 C 中, 我们一般可以用 signal 来实现, 而在 scala 中,我们同样有类似的机制. (more…)
scala在编译&优化的时候,有时候会返回个inline warning.但是,实际上我们写代码已经不能再优化了. 比如如下这个例子:
yu:~ yu$ scala -version Scala code runner version 2.11.7 -- Copyright 2002-2013, LAMP/EPFL yu:~ yu$ scala -optimize -Yinline-warnings Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05). Type in expressions to have them evaluated. Type :help for more information. scala> object O { Map( | "a" -> 0, "b" -> 1, "c" -> 2, "d" -> 3, "e" -> 4, | "a" -> 0, "b" -> 1, "c" -> 2, "d" -> 3, "e" -> 4, | "a" -> 0, "b" -> 1, "c" -> 2, "d" -> 3, "e" -> 4, | "f" -> 5, "g" -> 6, "h" -> 7, "i" -> 8) } warning: At the end of the day, could not inline @inline-marked method ->$extension warning: At the end of the day, could not inline @inline-marked method ->$extension warning: At the end of the day, could not inline @inline-marked method ->$extension warning: At the end of the day, could not inline @inline-marked method ->$extension defined object O
我们可以看到有个issue ,讲的就是这件事.它是12年底建立的,但一直没啥好的回答...可怜我为了关掉这个warning连带好几个其它warning都不能用了.
不过,今年五月终于有结果了.
一般情况下,我们会倾向于打开各种warning,以便规范我们的代码.不过,scala有时候有些warning也是蛮扯的.比如这么一段
$ scala -Xlint:missing-interpolator Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05). Type in expressions to have them evaluated. Type :help for more information. scala> val key="value"; println("$key") <console>:12: warning: possible missing interpolator: detected interpolated identifier `$key` println("$key") ^ $key key: String = value
psjrs是一个做了一些修改的scala web工程, 你可以在此基础上很容易得开发出一个你要的工程.
工程基于play, silhouette 等框架. play让人可以很容易搭建一个web service, 而silhouette则提供了用户认证相关的服务. 此外, 我还对配置文件,文件内容进行了一些定制, 使得你可以更容易得组织内容.
一个简单的方法是, 配置环境变量添加一个 option 到 JAVA_TOOL_OPTIONS
比如如下:
export JAVA_TOOL_OPTIONS="-Dfile.encoding=UTF8"
在windows 下, 可以在环境变量里面添加个key为 JAVA_TOOL_OPTIONS, value 为 "-Dfile.encoding=UTF8"
scala 一般用于网络上消息的处理,比如读写 mongodb, 处理 http request. 但偶尔写个 script, 还是要读写个文件的.
scala 包装了一个 Source 类,可以读取各种来源的数据, url, file 都可以. 但似乎并没有对写数据做什么处理.