where_self.carbon 825 B

1234567891011121314151617181920212223242526272829303132
  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: 12
  9. package ExplorerTest api;
  10. interface SwizzleWith(T:! type) {
  11. fn Op[self: Self](x: T) -> i32;
  12. }
  13. // TODO: There should be some way to write this that includes
  14. // `SwizzleWith(.Self)` in the list of lookup contexts.
  15. alias Swizzle = type where .Self impls SwizzleWith(.Self);
  16. impl i32 as SwizzleWith(i32) {
  17. fn Op[self: Self](x: Self) -> Self { return self * 10 + x; }
  18. }
  19. fn F[T:! Swizzle](v: T, w: T) -> i32 {
  20. return v.(SwizzleWith(T).Op)(w);
  21. }
  22. fn Main() -> i32 {
  23. var one: i32 = 1;
  24. var two: i32 = 2;
  25. return F(one, two);
  26. }