12. 変数
var one: number = 1;
var two: string = "two";
var three = 3.0; // 型推論(number)
var four: number; // Undefined Type
13. 関数の定義
function twice(x: number): number {
return x * 2;
} (x: number) => number
function identity(x) {
return x;
}
(x: any) => any
14. ちなみに F# だと ...
let identity x = x
val identity : ‘a -> ‘a
15. ラムダ式 (arrow function expression)
// 従来
var f0 = function (x: number) { return x * x; }
var f1 = (x: number) => { return x * x }
var f2 = (x: number) => x * x;
24. interface Wolf {
TypeScript
howl: () => string;
}
function howl(x: Wolf) {
alert(x.howl());
}
class WolfDog {
public howl() { return "bowwow!"; }
}
var mrchildren = { howl: () => "HOWL!" } // λ...
function start() {
howl(new WolfDog());
howl(mrchildren);
}
25. object Main {
Scala
type Wolf = { def howl(): Unit }
def howl(x: Wolf) {
println(x.howl())
}
class WolfDog {
def howl() = println("bowwow!")
}
class MrChildren {
def howl() = println("HOWL!")
}
def main(args: Array[String]) {
howl(new WolfDog())
howl(new MrChildren())
}
}
26. let inline howl x =
F#
(^a : (member Howl : unit -> unit) x)
type WolfDog() = // クラス
member this.Howl() = printfn "bowwow!"
type MrChildren = MrChildren // 判別共用体
with member this.Howl() = printfn "HOWL!"
let run () =
howl <| WolfDog()
howl MrChildren
28. F# に似てるね!
module MyModule =
let private myFunction1 () = ()
let myFunction2 () = ()
type private MyClass1() = class end
type MyClass2() = class end