class_alias.carbon 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // RUN: %{explorer} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{explorer} %s
  10. // CHECK: b.n: 1
  11. // CHECK: d.Get(0): 2
  12. // CHECK: e.Get(1): 3
  13. // CHECK: result: 0
  14. package ExplorerTest api;
  15. interface Addable {
  16. fn Add[me: Self](k: i32) -> Self;
  17. }
  18. impl i32 as Addable {
  19. fn Add[me: Self](k: i32) -> Self { return me + k; }
  20. }
  21. class Class { var n: i32; }
  22. class GenericClass(T:! Addable) {
  23. var m: T;
  24. fn Get[me: Self](n: i32) -> T { return me.m.Add(n); }
  25. }
  26. alias ClassAlias = Class;
  27. alias GenericClassAlias = GenericClass;
  28. alias ClassSpecializationAlias = GenericClassAlias(i32);
  29. fn Main() -> i32 {
  30. var a: Class = {.n = 1};
  31. var b: ClassAlias = a;
  32. var c: GenericClass(i32) = {.m = 2};
  33. var d: GenericClassAlias(i32) = c;
  34. var e: ClassSpecializationAlias = c;
  35. Print("b.n: {0}", b.n);
  36. Print("d.Get(0): {0}", d.Get(0));
  37. Print("e.Get(1): {0}", e.Get(1));
  38. return 0;
  39. }