class_alias.carbon 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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: result: 123
  11. package ExplorerTest api;
  12. interface Addable {
  13. fn Add[me: Self](k: i32) -> Self;
  14. }
  15. impl i32 as Addable {
  16. fn Add[me: Self](k: i32) -> Self { return me + k; }
  17. }
  18. class Class { var n: i32; }
  19. class GenericClass(T:! Addable) {
  20. var m: T;
  21. fn Get[me: Self](n: i32) -> T { return me.m.Add(n); }
  22. }
  23. alias ClassAlias = Class;
  24. alias GenericClassAlias = GenericClass;
  25. alias ClassSpecializationAlias = GenericClassAlias(i32);
  26. fn Main() -> i32 {
  27. var a: Class = {.n = 1};
  28. var b: ClassAlias = a;
  29. var c: GenericClass(i32) = {.m = 2};
  30. var d: GenericClassAlias(i32) = c;
  31. var e: ClassSpecializationAlias = c;
  32. // TODO: Switch to using Print here once it supports printing integers.
  33. return 100 * b.n + 10 * d.Get(0) + e.Get(1);
  34. }