impl_as_not_self.carbon 696 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. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: 5
  9. package ExplorerTest api;
  10. interface ConvertsFromInt {
  11. impl i32 as ImplicitAs(Self);
  12. }
  13. fn ConvertIntTo(T:! ConvertsFromInt, n: i32) -> T {
  14. return n;
  15. }
  16. class IntHolder {
  17. var n: i32;
  18. }
  19. external impl i32 as ImplicitAs(IntHolder) {
  20. fn Convert[me: Self]() -> IntHolder { return {.n = me}; }
  21. }
  22. external impl IntHolder as ConvertsFromInt {}
  23. fn Main() -> i32 {
  24. return ConvertIntTo(IntHolder, 5).n;
  25. }