Oh, but it does! There's the cond! macro:
let computed_key = cond!(
(key.len() > self.block_size) { self.zero_pad(self.hash(key).digest) }
(key.len() < self.block_size) { self.zero_pad(key) }
_ { key }
);Idris [3] is another language with dependent types (and is more Rust-like) designed for systems programming. I don't think Idris has type level presberger arithmetic though making some things a bit verbose.
[1] http://www.ats-lang.org/ [2] http://bluishcoder.co.nz/2010/09/01/dependent-types-in-ats.h... [3] http://www.idris-lang.org/
I am familiar with ATS and I don't like that I am only familiar with it from the Language Shootout contest website. I haven't heard of it used in production, no blogs, no news on HN.
It is a bad excuse and rather sad but my main charge against it is that it is not popular enough.
[1] http://bluishcoder.co.nz/tags/ats/ [2] http://www.reddit.com/r/ATS/ [3] http://mmpool.bitparking.com
Yes, although the error message isn't very good:
crypto.rs:55:23: 59:5 error: non-exhaustive patterns: true not covered
crypto.rs:55 let computed_key = match (key.len() > self.block_size, key.len() < self.block_size) {
crypto.rs:56 (true, false) => self.zero_pad(self.hash(key).digest),
crypto.rs:57 (false, true) => self.zero_pad(key),
crypto.rs:58 (false, false) => key
crypto.rs:59 };
error: aborting due to previous error
And technically that's not a good thing, since (true, true) is not possible considering the input. match key.len() {
k if k > self.block_size => ..
k if k < ... => ..
k => ...
or even match key.cmp(self.block_size) {
Equal => ..
Greater => ..
Less => ... match key.cmp(self.block_size) {
Equal => ..
Greater => ..
Less => ...
}
Which is much prettier (and more succinct) than both your original code and my example. You could also use a `cond!()` macro here, but that would mean you don't have an example for a blog post, so I think the above code is the best option. Thanks to kzrdude for posting.
Also, does the compiler complain about my example above being not exhaustive? I think in OCaml it would, which is sometimes nice.