prelude.carbon 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. package Carbon api;
  5. // Implicitly convert `Self` to `T`.
  6. interface ImplicitAs(T:! Type) {
  7. fn Convert[me: Self]() -> T;
  8. }
  9. // TODO: Simplify this once we have variadics.
  10. // TODO: Should these be final?
  11. impl forall [U1:! Type, T1:! ImplicitAs(U1)]
  12. (T1,) as ImplicitAs((U1,)) {
  13. fn Convert[me: Self]() -> (U1,) {
  14. let (v1: T1,) = me;
  15. return (v1.Convert(),);
  16. }
  17. }
  18. impl forall [U1:! Type, U2:! Type, T1:! ImplicitAs(U1), T2:! ImplicitAs(U2)]
  19. (T1, T2) as ImplicitAs((U1, U2)) {
  20. fn Convert[me: Self]() -> (U1, U2) {
  21. let (v1: T1, v2: T2) = me;
  22. return (v1.Convert(), v2.Convert());
  23. }
  24. }
  25. impl forall [U1:! Type, U2:! Type, U3:! Type,
  26. T1:! ImplicitAs(U1), T2:! ImplicitAs(U2), T3:! ImplicitAs(U3)]
  27. (T1, T2, T3) as ImplicitAs((U1, U2, U3)) {
  28. fn Convert[me: Self]() -> (U1, U2, U3) {
  29. let (v1: T1, v2: T2, v3: T3) = me;
  30. return (v1.Convert(), v2.Convert(), v3.Convert());
  31. }
  32. }
  33. // Note that Print is experimental, and not part of an accepted proposal, but
  34. // is included here for printing state in tests.
  35. fn Print(format_str: String) {
  36. __intrinsic_print(format_str);
  37. }
  38. class Heap {
  39. fn New[T:! Type, me: Self](x : T) -> T* {
  40. return __intrinsic_new(x);
  41. }
  42. fn Delete[T:! Type, me: Self](p : T*) {
  43. __intrinsic_delete(p);
  44. }
  45. }
  46. var heap: Heap = {};