prelude.carbon 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // Explicitly convert `Self` to `T`.
  6. interface As(T:! Type) {
  7. fn Convert[me: Self]() -> T;
  8. }
  9. // Implicitly convert `Self` to `T`.
  10. interface ImplicitAs(T:! Type) {
  11. fn Convert[me: Self]() -> T;
  12. }
  13. // TODO: ImplicitAs(T) should extend As(T).
  14. impl forall [T:! Type, U:! ImplicitAs(T)] U as As(T) {
  15. fn Convert[me: Self]() -> T { return me.Convert(); }
  16. }
  17. // Every type implicitly converts to itself.
  18. impl forall [T:! Type] T as ImplicitAs(T) {
  19. fn Convert[me: Self]() -> T { return me; }
  20. }
  21. // TODO: Simplify this once we have variadics.
  22. // TODO: Should these be final?
  23. impl forall [U1:! Type, T1:! ImplicitAs(U1)]
  24. (T1,) as ImplicitAs((U1,)) {
  25. fn Convert[me: Self]() -> (U1,) {
  26. let (v1: T1,) = me;
  27. return (v1.Convert(),);
  28. }
  29. }
  30. impl forall [U1:! Type, U2:! Type, T1:! ImplicitAs(U1), T2:! ImplicitAs(U2)]
  31. (T1, T2) as ImplicitAs((U1, U2)) {
  32. fn Convert[me: Self]() -> (U1, U2) {
  33. let (v1: T1, v2: T2) = me;
  34. return (v1.Convert(), v2.Convert());
  35. }
  36. }
  37. impl forall [U1:! Type, U2:! Type, U3:! Type,
  38. T1:! ImplicitAs(U1), T2:! ImplicitAs(U2), T3:! ImplicitAs(U3)]
  39. (T1, T2, T3) as ImplicitAs((U1, U2, U3)) {
  40. fn Convert[me: Self]() -> (U1, U2, U3) {
  41. let (v1: T1, v2: T2, v3: T3) = me;
  42. return (v1.Convert(), v2.Convert(), v3.Convert());
  43. }
  44. }
  45. // Note that Print is experimental, and not part of an accepted proposal, but
  46. // is included here for printing state in tests.
  47. // TODO: Remove Print special casing once we have variadics or overloads.
  48. // fn Print(format_str: String) {
  49. // __intrinsic_print(format_str);
  50. // }
  51. class Heap {
  52. fn New[T:! Type, me: Self](x : T) -> T* {
  53. return __intrinsic_new(x);
  54. }
  55. fn Delete[T:! Type, me: Self](p : T*) {
  56. __intrinsic_delete(p);
  57. }
  58. }
  59. var heap: Heap = {};