Scala介绍
Posted natty-sky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala介绍相关的知识,希望对你有一定的参考价值。
日期: 2020-01-22
1.概述和安装
Scala是“可扩展语言”的缩写(SCAlable Language),于2003创建,为JVM平台上的函数式编程以及面向对象编程提供一个高性能开发环境。所以,Scala要求使用java运行时库。并且scala是编译型静态类型语言。
在mac环境使用HomeBrew来安装scala比较方便(需要前置安装Java),直接输入命令:
$ java -version
$ brew install scala
直接输入scala就可以启动scala REPL来交互编程。
- scala REPL提供了一个帮助系统,使用:help命令可以启动。
- 当命令返回值时,REPL会把它赋给一个新的常量变量,这些“res”变量会顺序编号,并且可以在后边命令中使用(例如 2*res2)
- 使用 :load 命令可以加载外部的scala代码并执行。
- 可以使用 :paste -raw 命令,并按回车键之后,粘贴外部的scala代码在REPL中执行。在mac terminal中尝试过无法退出粘贴模式不建议使用这种方法,使用load文件的方式更好。
1 $ scala 2 Welcome to Scala 2.13.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172). 3 Type in expressions for evaluation. Or try :help. 4 5 scala> 6 7 scala> println("Hello World!") 8 Hello World! 9 10 scala> 5*7 11 res2: Int = 35 12 13 scala> 2*res2 14 res3: Int = 70 15 16 scala> :help 17 All commands can be abbreviated, e.g., :he instead of :help. 18 :completions <string> output completions for the given string 19 :edit <id>|<line> edit history 20 :help [command] print this summary or command-specific help 21 :history [num] show the history (optional num is commands to show) 22 :h? <string> search the history 23 :imports [name name ...] show import history, identifying sources of names 24 :implicits [-v] show the implicits in scope 25 :javap <path|class> disassemble a file or class name 26 :line <id>|<line> place line(s) at the end of history 27 :load <path> interpret lines in a file 28 :paste [-raw] [path] enter paste mode or paste a file 29 :power enable power user mode 30 :quit exit the interpreter 31 :replay [options] reset the repl and replay all previous commands 32 :require <path> add a jar to the classpath 33 :reset [options] reset the repl to its initial state, forgetting all session entries 34 :save <path> save replayable session to a file 35 :sh <command line> run a shell command (result is implicitly => List[String]) 36 :settings <options> update compiler options, if possible; see reset 37 :silent disable/enable automatic printing of results 38 :type [-v] <expr> display the type of an expression without evaluating it 39 :kind [-v] <type> display the kind of a type. see also :help kind 40 :warnings show the suppressed warnings from the most recent line which had any 41 42 scala> "Hello World!" 43 res4: String = Hello World! 44 45 scala> (22.5 * 9/5) + 32 46 res5: Double = 72.5 47 48 scala> res5/2 49 res6: Double = 36.25 50 51 scala> :load /Users/natty/Develop/Learning/scala/LearningScala/Hello.scala 52 args: Array[String] = Array() 53 Loading /Users/natty/Develop/Learning/scala/LearningScala/Hello.scala... 54 Hello file world! 55 56 scala> :paste -raw 57 // Entering paste mode (ctrl-D to finish) 58 59 println("Hello file world!")
!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>
以上是关于Scala介绍的主要内容,如果未能解决你的问题,请参考以下文章