This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 以前にちょっとだけ触ってみたNiceについて調べてみる。 Niceは、Javaをベースに高階関数、マルチメソッド、パラメータ型、キーワード引数、省略可能引数、Option Typesなどの機能を追加した言語で、処理系は、Java VM用のコードを吐くコンパイラとなっている。 | |
| マルチメソッド、キーワード引数などは、他の言語でもよく見かけるが、 Option Typesは、他の言語ではあまり見かけない機能だ。Option Typesとは、値としてnullを許す型で、それだけ見ると、珍しくもなんともないのだが、Niceでは面白いことに、変数の型はデフォルトでnullを代入できないのだ。例えば、以下のようなコードを書くと、sはnullを代入できない型になり、もしsにnullが代入され得るようなコードを書くと、コンパイルエラーになる。 | |
| let String s = "Hello";//nullは代入できない | |
| もし、nullを代入可能にしたければ、型名の頭に'?'を付けて、次のように宣言する。 | |
| let ?String s = "Hello";//nullが代入可能 | |
| この機能は、大変面白い機能なんじゃないかと思う。例えば、Mapにキーを与えて値を取得するとき、返って来た値がnullかどうかをチェックする必要がある場合は多いが、この機能があれば、もし仮にnullかどうかのチェックを忘れたとしても、コンパイルエラーになってくれる。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object StringLiteralType { | |
| def isAType(arg: String): Unit = arg match { | |
| case _ : "a" => println("This is `a` type") | |
| case _ => println("not `a`") | |
| } | |
| def a1 = isAType("a") | |
| def a2 = isAType(new String("a")) | |
| def a3 = new String("a").isInstanceOf["a"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object StringLiteralType { | |
| def isAType(arg: String): Unit = arg match { | |
| case _ : "a" => println("This is `a` type") | |
| case _ => println("not `a`") | |
| } | |
| def a1 = isAType("a") | |
| def a2 = isAType(new String("a")) | |
| def a3 = new String("a").isInstanceOf["a"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class LargeStringInterpolation { | |
| public int a(); | |
| Code: | |
| 0: aload_0 | |
| 1: getfield #15 // Field a:I | |
| 4: ireturn | |
| public java.lang.String s33(); | |
| Code: | |
| 0: aload_0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object LargeStringInterpolation extends App { | |
| val a = 1 | |
| val s32 = s"$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a" | |
| val s33 = s"$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a" | |
| val s34 = s"$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a" | |
| val s35 = s"$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a" | |
| println("Hello") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A { | |
| def foo(x: Int): Int = 1 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final object ExplicitlyFinalObject { | |
| val NonFinalVal = 1 | |
| final val FinalVal = 2 | |
| } | |
| object ImplicitlyFinalObject { | |
| val NonFinalVal = 1 | |
| final val FinalVal = 2 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class M a where | |
| (+) :: a -> a -> a | |
| add x y = x + y | |
| main = do print "Hello" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed abstract class Nest | |
| case class NLeaf(code: Char) extends Nest | |
| case class NList(elements: List[Nest]) extends Nest | |
| def group(tokens: List[Char]): List[Nest] = { | |
| def process(nest: List[Nest], acc: List[Nest], remain: List[Char]): (List[Nest], List[Nest], List[Char]) = { | |
| remain match { | |
| case Nil => | |
| (nest, Nil, Nil) | |
| case ('(')::rest => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Hello extends App { | |
| def main(args: Array[String]): Unit = { | |
| println("Hello") | |
| } | |
| } |