8. C#での型定義
User クラス
public class User
{
public readonly string Name;
public readonly int Age;
public User(string name, int age)
{
this.Name = name;
this.Age = age;
}
public override bool Equals(object obj)
{
var other = obj as User;
if (other == null)
return false;
return this.Name == other.Name && this.Age == other.Age;
}
public override bool GetHashCode()
{
return this.Name.GetHashCode() ^ this.Age.GetHashCode();
}
public override string ToString()
{
return string.Format("User(Name={0}, Age={1})", Name, Age);
}
}
面倒!
13. 愚直に実装すると・
・・
let doSomething prec time luminance =
if prec = 0 then
if 7 <= time && time <= 17 then
if luminance >= 5 then
// 晴れ
else
// 曇り
elif time <= 4 || time >= 18 then
// 晴れ
else
if luminance >= 3 then
// 晴れ
else
// 曇り
elif prec >= 20 then
// 大雨
else
// 雨
15. アクティブパターンに押し込める
let (|Sunny|Cloudy|Rain|HeavyRain|)
(prec, time, luminance) =
if prec = 0 then
if 7 <= time && time <= 17 then
if luminance >= 5 then Sunny
else Cloudy
elif time <= 4 || time >= 18 then Sunny
else
if luminance >= 3 then Sunny
else Cloudy
elif prec >= 20 then HeavyRain
else Rain
let doSomething prec time luminance =
match prec, time, luminance with
| Sunny -> // 晴れ
| Cloudy -> // 曇り
| Rain -> // 雨
| HeavyRain -> // 大雨
17. 型を導入する
type Weather =
Sunny | Cloudy | Rain | HeavyRain
with
static member Create prec time luminance =
if prec = 0 then
if 7 <= time && time <= 17 then
if luminance >= 5 then Sunny
else Cloudy
elif time <= 4 || time >= 18 then Sunny
else
if luminance >= 3 then Sunny
else Cloudy
elif prec >= 20 then HeavyRain
else Rain
let doSomething weather =
match weather with ...