我想Maybe
在 D 中从 Haskell 实现,只是为了它。这是我到目前为止所得到的,但并不是那么好。任何想法如何改进它?
class Maybe(a = int){ } //problem 1: works only with ints
class Just(alias a) : Maybe!(typeof(a)){ }
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!3; //problem 2: can't say 'Just!k'
else
return new Nothing;
}
Haskell 也许定义:
data Maybe a = Nothing | Just a