Skip to the content.

Scala 基础语法学习

Class

primary constructor all kinds of getter and setter

Object

Use an object in Scala whenever you would have used a singleton object in Java or C++: • As a home for utility functions or constants • When a single immutable instance can be shared efficiently • When a single instance is required to coordinate some service (the singleton design pattern)

The class and its companion object can access each other’s private features. They must be located in the same source file

class and object in scala implict class case class case object

questions

  1. parameters of class
    class Animal(val name: String, val species: String)
    class Human(val name: String)
    implicit def human2animal(h: Human): Animal = {
     new Animal(h.name, "Homo sapiens")
    }
    val me = new Human("Adam")
    println(me.species)
    

Inherience

Superclass Construction

Use Vim

http://seenaburns.com/vim-setup-for-scala/

  1. Scala functions that do not have any arguments do not require empty parenthese
  2. By convention, argument-less functions that do not have side effects (i.e. calling them does not change anything and they simply return a value) do not use parentheses, and functions that do have side effects (perhaps they change class variables or print stuff out) should require parentheses.
  3. Overloaded functions should be avoided, though.

vim 中的搭建

参考 https://github.com/scalameta/nvim-metals 中的文档:

安装 cs

curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs
./cs setup

chipyard 为例,在项目中执行

sbt bloopInstall

然后就可以自动索引了。

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。