只是为了踢,我想看看如果我在Haskell中定义一个函数会发生什么Int -> Int,知道它会溢出并且必须返回一个Integer.考虑以下:
factorial :: Int -> Int
factorial n = product [1..n]
Run Code Online (Sandbox Code Playgroud)
现在我知道如果我跑factorial 50,我会得到一个在'codomain'之外的数字factorial.由于Haskell是如此强类型,我希望它会返回一个错误.相反,GHCi返回一个奇怪的负面Int:
ghci> factorial 50
-3258495067890909184
Run Code Online (Sandbox Code Playgroud)
请注意
ghci> maxBound :: Int
9223372036854775808
Run Code Online (Sandbox Code Playgroud)
因为我在64位上运行.
我发现这种行为有点可怕:为什么Haskell没有引发错误?为什么factorial 50返回一个随机的负数?任何澄清将不胜感激.
我正在尝试在Rust中编写一个回合制游戏,而且我正在用语言碰壁(除非我不理解某些东西 - 我是语言新手).基本上,我想改变游戏中每个州有不同行为的状态.例如,我有类似的东西:
struct Game {
state: [ Some GameState implementer ],
}
impl Game {
fn handle(&mut self, event: Event) {
let new_state = self.handle(event);
self.state = new_state;
}
}
struct ChooseAttackerPhase {
// ...
}
struct ResolveAttacks {
// ...
}
impl ResolveAttacks {
fn resolve(&self) {
// does some stuff
}
}
trait GameState {
fn handle(&self, event: Event) -> [ A New GateState implementer ]
}
impl GameState for ChooseAttackerPhase {
fn handle(&self, event: Event) -> …Run Code Online (Sandbox Code Playgroud)