class_alias.carbon 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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: b.n: 1
  9. // CHECK:STDOUT: d.Get(0): 2
  10. // CHECK:STDOUT: e.Get(1): 3
  11. // CHECK:STDOUT: result: 0
  12. package ExplorerTest api;
  13. interface Addable {
  14. fn Add[me: Self](k: i32) -> Self;
  15. }
  16. impl i32 as Addable {
  17. fn Add[me: Self](k: i32) -> Self { return me + k; }
  18. }
  19. class Class { var n: i32; }
  20. class GenericClass(T:! Addable) {
  21. var m: T;
  22. fn Get[me: Self](n: i32) -> T { return me.m.Add(n); }
  23. }
  24. alias ClassAlias = Class;
  25. alias GenericClassAlias = GenericClass;
  26. alias ClassSpecializationAlias = GenericClassAlias(i32);
  27. fn Main() -> i32 {
  28. var a: Class = {.n = 1};
  29. var b: ClassAlias = a;
  30. var c: GenericClass(i32) = {.m = 2};
  31. var d: GenericClassAlias(i32) = c;
  32. var e: ClassSpecializationAlias = c;
  33. Print("b.n: {0}", b.n);
  34. Print("d.Get(0): {0}", d.Get(0));
  35. Print("e.Get(1): {0}", e.Get(1));
  36. return 0;
  37. }