impl_as.carbon 872 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. // AUTOUPDATE
  6. // CHECK:STDOUT: result: 4
  7. package ExplorerTest api;
  8. interface MyHashable {
  9. let Result:! type;
  10. fn Hash[self: Self]() -> Result;
  11. }
  12. constraint HashToIntConvertible {
  13. impl as MyHashable;
  14. impl Self.(MyHashable.Result) as ImplicitAs(i32);
  15. }
  16. class MyHashValue {
  17. external impl as ImplicitAs(i32) {
  18. fn Convert[self: Self]() -> i32 { return 4; }
  19. }
  20. }
  21. class Widget {
  22. external impl as MyHashable where .Result = MyHashValue {
  23. fn Hash[self: Self]() -> MyHashValue { return {}; }
  24. }
  25. }
  26. fn MakeSmallHash[T:! HashToIntConvertible](x: T) -> i32 {
  27. return x.(MyHashable.Hash)();
  28. }
  29. fn Main() -> i32 {
  30. var w: Widget = {};
  31. return MakeSmallHash(w);
  32. }