impl_as.carbon 926 B

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