class_alias.carbon 1.0 KB

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