class_alias.carbon 1.0 KB

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