require.carbon 746 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. // NOAUTOUPDATE
  6. package ExplorerTest api;
  7. interface Apple(T:! type) {
  8. fn F[self: Self]() -> T;
  9. }
  10. interface Banana {
  11. require Self impls Apple(i32);
  12. fn G[self: Self]();
  13. }
  14. class Carrot {
  15. var n: i32;
  16. }
  17. impl Carrot as Apple(i32) {
  18. fn F[self: Self]() -> i32 { return self.n; }
  19. }
  20. impl Carrot as Banana {
  21. fn G[self: Self]() { Print("Carrot.G"); }
  22. }
  23. fn H[T:! Banana](x: T) -> i32 {
  24. x.G();
  25. return x.(Apple(i32).F)();
  26. }
  27. fn Main() -> i32 {
  28. var c: Carrot = {.n = 5};
  29. return H(c);
  30. }
  31. // CHECK:STDOUT: Carrot.G
  32. // CHECK:STDOUT: result: 5