where_self.carbon 774 B

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