extend.carbon 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 Maker(T:! type) {
  8. fn Make[self: Self]() -> T;
  9. }
  10. constraint IndirectMaker(T:! type) {
  11. extend Maker(T*);
  12. }
  13. constraint MoreIndirectMaker {
  14. extend IndirectMaker(i32);
  15. }
  16. class PointerFactory {
  17. var p: i32*;
  18. impl as Maker(i32*) {
  19. fn Make[self: Self]() -> i32* { return self.p; }
  20. }
  21. }
  22. fn CallIndirect[T:! IndirectMaker(i32)](x: T) -> i32 {
  23. return *x.Make();
  24. }
  25. fn CallMoreIndirect[T:! MoreIndirectMaker](x: T) -> i32 {
  26. return *x.Make();
  27. }
  28. fn Main() -> i32 {
  29. var n: i32 = 5;
  30. var f: PointerFactory = {.p = &n};
  31. Print("{0}", *f.(Maker(i32*).Make)());
  32. Print("{0}", *f.(IndirectMaker(i32).Make)());
  33. Print("{0}", *f.(MoreIndirectMaker.Make)());
  34. Print("{0}", CallIndirect(f));
  35. Print("{0}", CallMoreIndirect(f));
  36. return 0;
  37. }
  38. // CHECK:STDOUT: 5
  39. // CHECK:STDOUT: 5
  40. // CHECK:STDOUT: 5
  41. // CHECK:STDOUT: 5
  42. // CHECK:STDOUT: 5
  43. // CHECK:STDOUT: result: 0