Advertisement
Guest User

Untitled

a guest
Jun 29th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. use std;
  2.  
  3. mod ht {
  4. iface hash {
  5. fn hash() -> uint;
  6. fn tostr() -> str; // putting this into the interface is just a hack
  7. // to give us a way to print keys. This doesn't go
  8. // here at all.
  9. }
  10.  
  11. type t<K,V> = [(K, V)]; // doesn't matter, we don't use it
  12.  
  13. fn create<K:hash,V>() -> @t<K,V> {
  14. @[]
  15. }
  16.  
  17. fn put<K:hash,V>(ht: @t<K,V>, k: K, v: V) {
  18. io::println(#fmt("ht put: %s hash to %ud", k.tostr(), k.hash()));
  19. }
  20. }
  21.  
  22. mod Module1 {
  23. impl of ht::hash for uint {
  24. fn hash() -> uint { ret self; }
  25. fn tostr() -> str { ret #fmt("%ud", self); }
  26. }
  27. fn foo() {
  28. let h = ht::create::<uint, str>();
  29. ht::put(h, 3u, "hi"); // 3u.hash() == 3u here
  30. Module2::bar(h);
  31. }
  32. }
  33.  
  34. mod Module2 {
  35. impl of ht::hash for uint {
  36. fn hash() -> uint { ret self / 2; }
  37. fn tostr() -> str { ret #fmt("%ud", self); }
  38. }
  39. fn bar(h: @ht::t<uint, str>) {
  40. ht::put(h, 3u, "ho"); // 3u.hash() == 1u here
  41. }
  42. }
  43.  
  44. fn main (_args: [str]) {
  45. Module1::foo();
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement