where_self.carbon 771 B

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