require.carbon 874 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // NOAUTOUPDATE
  6. package ExplorerTest api;
  7. interface MyHashable {
  8. let Result:! type;
  9. fn Hash[self: Self]() -> Result;
  10. }
  11. constraint HashToIntConvertible {
  12. require Self impls MyHashable;
  13. require Self.(MyHashable.Result) impls ImplicitAs(i32);
  14. }
  15. class MyHashValue {
  16. impl as ImplicitAs(i32) {
  17. fn Convert[self: Self]() -> i32 { return 4; }
  18. }
  19. }
  20. class Widget {
  21. impl as MyHashable where .Result = MyHashValue {
  22. fn Hash[self: Self]() -> MyHashValue { return {}; }
  23. }
  24. }
  25. fn MakeSmallHash[T:! HashToIntConvertible](x: T) -> i32 {
  26. return x.(MyHashable.Hash)();
  27. }
  28. fn Main() -> i32 {
  29. var w: Widget = {};
  30. return MakeSmallHash(w);
  31. }
  32. // CHECK:STDOUT: result: 4