Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std;
- mod ht {
- iface hash {
- fn hash() -> uint;
- fn tostr() -> str; // putting this into the interface is just a hack
- // to give us a way to print keys. This doesn't go
- // here at all.
- }
- type t<K,V> = [(K, V)]; // doesn't matter, we don't use it
- fn create<K:hash,V>() -> @t<K,V> {
- @[]
- }
- fn put<K:hash,V>(ht: @t<K,V>, k: K, v: V) {
- io::println(#fmt("ht put: %s hash to %ud", k.tostr(), k.hash()));
- }
- }
- mod Module1 {
- impl of ht::hash for uint {
- fn hash() -> uint { ret self; }
- fn tostr() -> str { ret #fmt("%ud", self); }
- }
- fn foo() {
- let h = ht::create::<uint, str>();
- ht::put(h, 3u, "hi"); // 3u.hash() == 3u here
- Module2::bar(h);
- }
- }
- mod Module2 {
- impl of ht::hash for uint {
- fn hash() -> uint { ret self / 2; }
- fn tostr() -> str { ret #fmt("%ud", self); }
- }
- fn bar(h: @ht::t<uint, str>) {
- ht::put(h, 3u, "ho"); // 3u.hash() == 1u here
- }
- }
- fn main (_args: [str]) {
- Module1::foo();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement