generic_method.carbon 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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: 0
  11. package ExplorerTest api;
  12. class Cell(T:! Type) {
  13. fn Create(x: T) -> Cell(T) { return { .data = x }; }
  14. fn Get[me: Self]() -> T {
  15. return me.data;
  16. }
  17. fn Put[addr me: Self*](x: T) {
  18. (*me).data = x;
  19. }
  20. fn CreateOther[me: Self, U:! Type](x: U) -> Cell(U) {
  21. return {.data = x};
  22. }
  23. var data: T;
  24. }
  25. class Integer {
  26. var int: i32;
  27. }
  28. fn Main() -> i32 {
  29. var i: Integer = {.int = 1};
  30. var c: Cell(Integer) = Cell(Integer).Create(i);
  31. i = {.int = 0};
  32. c.Put(i);
  33. var j: Integer = c.Get();
  34. var d: Cell(Integer) = c.CreateOther(j);
  35. return c.data.int + d.data.int;
  36. }