impl_constraint.carbon 678 B

123456789101112131415161718192021222324252627282930313233343536
  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 Runnable {
  8. fn Run[self: Self]() -> i32;
  9. }
  10. interface Walkable {
  11. fn Walk[self: Self]() -> i32;
  12. }
  13. constraint Traversible {
  14. extend Runnable;
  15. extend Walkable;
  16. }
  17. impl i32 as Traversible {
  18. fn Run[self: i32]() -> i32 {
  19. return 10 * self;
  20. }
  21. fn Walk[self: i32]() -> i32 {
  22. return self + 1;
  23. }
  24. }
  25. fn Main() -> i32 {
  26. var n: i32 = 1;
  27. return n.(Runnable.Run)() + n.(Walkable.Walk)();
  28. }
  29. // CHECK:STDOUT: result: 12