LoginSignup
0
0

More than 5 years have passed since last update.

Scala わからないことリスト

Last updated at Posted at 2016-05-17
  • this.type とシングルトンタイプ
  • explicitly typed self reference
  • Higher kinded type parameter
  • for 内包表記(sequence comprehension) ではなぜ、"{}" が推奨されるのか
  • trait A { val str: String } が new できる

Self Type Annotation

DI を考えた場合、コンストラクタ引数で注入するやり方は分かる。
trait はコンストラクタ引数を定義できない。
抽象クラスではなく、trait で定義するのは多重継承できないから。
Self Type Annotation を用いなくても、mix-in で同じことはできる。依存している事が分かり易いのがメリットか?

(http://eed3si9n.com/ja/real-world-scala-dependency-injection-di)
この記事の中の、構造的部分型というのはわかる。

Scala: Understanding the self type annotation and how it relates to the Cake Pattern
Cake Pattern in Scala / Self type annotations / Explicitly Typed Self References - explained
(http://qiita.com/mizunowanko/items/53eed059fc044c5aa5dc)
(http://daimatz.net/text/2014/0128-cake-pattern.html)
(http://tototoshi.hatenablog.com/entry/2014/06/04/195928)
(http://okapies.hateblo.jp/entry/2013/07/15/232456)

関数とメソッドの違い

継承における "=>"

Effective Scala[http://twitter.github.io/effectivescala] から

trait SocketFactory extends (SocketAddress => Socket)

Function1[SocketAddress, Socket] と同等らしい。
http://www.scala-lang.org/api/current/scala/Function1.html

trait Function1[T1, R]{
  def apply(v1: T1): R
}

A.apply() は A() だけど、case class やコンパニオンオブジェクトで定義すればいいのでは?

scalatest Prop、メソッドにブロックを渡している?

forAll { (n: Int, d: Int) =>

名前渡し引数

関数を引数にするのと何が違う?

引数: => 型

丸括弧("()") の省略

  • 文脈によって異なる
  • { ... } は ({ ... }) の場合が多い
  • ; の省略もあるので、そこにも注意する
forAll { ... }

1       // literal
(1)     // expression
{1}     // block of code
({1})   // expression with a block of code
{(1)}   // block of code with an expression
({(1)}) // you get the drift...

Abstrract Class の自己参照

Programing in Scala 3rd, Section 20.10 から

abstract class AbstractCurrency {
  type Currency <: AbstractCurrency

abstract class を継承して、さらに abstract class を作る。
なぜ具象クラスではいけないのか?

abstract class Dollar extends AbstractCurrency {
  type Currency = Dollar
  def designation = "USD"
}

型パラメータと非変、共変、反変、上限境界、下限境界

これは自分でクラスを設計しないとわからなそう。
このあと Abstract Type もあるが、自分が使うところをイメージできない

http://techblog.reraku.co.jp/entry/2016/06/12/135411
http://takafumi-s.hatenablog.com/entry/2015/08/20/122350
http://hogepiyo.hatenablog.jp/entry/2015/05/31/225103

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0