generic_method.carbon 836 B

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