prelude.carbon 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. // ----------------------
  6. // Conversion interfaces.
  7. // ----------------------
  8. // Explicitly convert `Self` to `T`.
  9. interface As(T:! type) {
  10. fn Convert[self: Self]() -> T;
  11. }
  12. // Implicitly convert `Self` to `T`.
  13. interface ImplicitAs(T:! type) {
  14. extends As(T);
  15. }
  16. // TODO: This should be private.
  17. interface __EqualConverter {
  18. let T:! type;
  19. fn Convert(t: T) -> Self;
  20. }
  21. fn __EqualConvert[T:! type](t: T, U:! __EqualConverter where .T = T) -> U {
  22. return U.Convert(t);
  23. }
  24. impl forall [U:! type] U as __EqualConverter where .T = U {
  25. fn Convert(u: U) -> U { return u; }
  26. }
  27. __match_first {
  28. // Pick up implicit conversions that are built into the compiler.
  29. // TODO: Split these into individual categories and implement as many as we can
  30. // in the prelude.
  31. impl forall [U:! type, template T:! __intrinsic_implicit_as(U)] T as ImplicitAs(U) {
  32. fn Convert[self: Self]() -> U { return __intrinsic_implicit_as_convert(self, U); }
  33. }
  34. // Every type implicitly converts to single-step-equal types.
  35. impl forall [T:! type, U:! type where .Self == T] T as ImplicitAs(U) {
  36. fn Convert[self: Self]() -> U { return __EqualConvert(self, U); }
  37. }
  38. // A tuple implicitly converts to another tuple if all its elements do.
  39. // TODO: Simplify this once we have variadics.
  40. // TODO: Should these be final?
  41. impl forall [U1:! type, T1:! ImplicitAs(U1)]
  42. (T1,) as ImplicitAs((U1,)) {
  43. fn Convert[self: Self]() -> (U1,) {
  44. let (v1: T1,) = self;
  45. return (v1.Convert(),);
  46. }
  47. }
  48. impl forall [U1:! type, U2:! type, T1:! ImplicitAs(U1), T2:! ImplicitAs(U2)]
  49. (T1, T2) as ImplicitAs((U1, U2)) {
  50. fn Convert[self: Self]() -> (U1, U2) {
  51. let (v1: T1, v2: T2) = self;
  52. return (v1.Convert(), v2.Convert());
  53. }
  54. }
  55. impl forall [U1:! type, U2:! type, U3:! type,
  56. T1:! ImplicitAs(U1), T2:! ImplicitAs(U2), T3:! ImplicitAs(U3)]
  57. (T1, T2, T3) as ImplicitAs((U1, U2, U3)) {
  58. fn Convert[self: Self]() -> (U1, U2, U3) {
  59. let (v1: T1, v2: T2, v3: T3) = self;
  60. return (v1.Convert(), v2.Convert(), v3.Convert());
  61. }
  62. }
  63. // A tuple explicitly converts to another tuple if all its elements do. Note
  64. // that this fully overlaps with the previous set of impl declarations for
  65. // the case where an implicit conversion is possible.
  66. impl forall [U1:! type, T1:! As(U1)]
  67. (T1,) as As((U1,)) {
  68. fn Convert[self: Self]() -> (U1,) {
  69. let (v1: T1,) = self;
  70. return (v1.Convert(),);
  71. }
  72. }
  73. impl forall [U1:! type, U2:! type, T1:! As(U1), T2:! As(U2)]
  74. (T1, T2) as As((U1, U2)) {
  75. fn Convert[self: Self]() -> (U1, U2) {
  76. let (v1: T1, v2: T2) = self;
  77. return (v1.Convert(), v2.Convert());
  78. }
  79. }
  80. impl forall [U1:! type, U2:! type, U3:! type,
  81. T1:! As(U1), T2:! As(U2), T3:! As(U3)]
  82. (T1, T2, T3) as As((U1, U2, U3)) {
  83. fn Convert[self: Self]() -> (U1, U2, U3) {
  84. let (v1: T1, v2: T2, v3: T3) = self;
  85. return (v1.Convert(), v2.Convert(), v3.Convert());
  86. }
  87. }
  88. }
  89. // ----------------------
  90. // Comparison interfaces.
  91. // ----------------------
  92. // ----------------------
  93. // EQUAL
  94. // ----------------------
  95. interface EqWith(U:! type) {
  96. fn Equal[self: Self](other: U) -> bool;
  97. fn NotEqual[self: Self](other: U) -> bool;
  98. }
  99. constraint Eq {
  100. extends EqWith(Self);
  101. }
  102. // TODO: Simplify this once we have variadics
  103. impl forall [T2:! type, U2:! type, T1:! EqWith(T2), U1:! EqWith(U2)]
  104. (T1, U1) as EqWith((T2, U2)) {
  105. fn Equal[self: Self](other: (T2, U2)) -> bool {
  106. let (l1: T1, l2: U1) = self;
  107. let (r1: T2, r2: U2) = other;
  108. return l1 == r1 and l2 == r2;
  109. }
  110. fn NotEqual[self: Self](other: (T2, U2)) -> bool {
  111. let (l1: T1, l2: U1) = self;
  112. let (r1: T2, r2: U2) = other;
  113. return l1 != r1 or l2 != r2;
  114. }
  115. }
  116. impl bool as EqWith(Self) {
  117. fn Equal[self: Self](other: Self) -> bool {
  118. return if self then other else not other;
  119. }
  120. fn NotEqual[self: Self](other: Self) -> bool {
  121. return if self then not other else other;
  122. }
  123. }
  124. impl i32 as EqWith(Self) {
  125. fn Equal[self: Self](other: Self) -> bool {
  126. return __intrinsic_int_eq(self, other);
  127. }
  128. fn NotEqual[self: Self](other: Self) -> bool {
  129. return not __intrinsic_int_eq(self, other);
  130. }
  131. }
  132. impl String as EqWith(Self) {
  133. fn Equal[self: Self](other: Self) -> bool {
  134. return __intrinsic_str_eq(self, other);
  135. }
  136. fn NotEqual[self: Self](other: Self) -> bool {
  137. return not __intrinsic_str_eq(self, other);
  138. }
  139. }
  140. // ----------------------
  141. // COMPARE
  142. // ----------------------
  143. choice Ordering {
  144. Less,
  145. Equivalent,
  146. Greater,
  147. Incomparable
  148. }
  149. // TODO: Per the design, this should be named `OrderedWith`.
  150. interface CompareWith(U:! type) {
  151. fn Compare[self: Self](u: U) -> Ordering;
  152. // TODO: Add `default fn` for Less, LessOrEquivalent, Greater, and GreaterOrEquivalent once it's available.
  153. }
  154. constraint Ordered {
  155. extends CompareWith(Self);
  156. }
  157. impl i32 as CompareWith(Self) {
  158. fn Compare[self: Self](other: Self) -> Ordering {
  159. var comp: i32 = __intrinsic_int_compare(self, other);
  160. if (comp == -1) {
  161. return Ordering.Less;
  162. }
  163. if (comp == 0) {
  164. return Ordering.Equivalent;
  165. }
  166. if (comp == 1) {
  167. return Ordering.Greater;
  168. }
  169. return Ordering.Incomparable;
  170. }
  171. }
  172. impl String as CompareWith(Self) {
  173. fn Compare[self: Self](other: Self) -> Ordering {
  174. var comp: i32 = __intrinsic_str_compare(self, other);
  175. if (comp == -1) {
  176. return Ordering.Less;
  177. }
  178. if (comp == 0) {
  179. return Ordering.Equivalent;
  180. }
  181. if (comp == 1) {
  182. return Ordering.Greater;
  183. }
  184. return Ordering.Incomparable;
  185. }
  186. }
  187. interface LessWith(U:! type) {
  188. fn Less[self: Self](other: U) -> bool;
  189. }
  190. interface LessEqWith(U:! type) {
  191. fn LessEq[self: Self](other: U) -> bool;
  192. }
  193. interface GreaterWith(U:! type) {
  194. fn Greater[self: Self](other: U) -> bool;
  195. }
  196. interface GreaterEqWith(U:! type) {
  197. fn GreaterEq[self: Self](other: U) -> bool;
  198. }
  199. impl i32 as LessWith(Self) {
  200. fn Less[self: Self](other: Self) -> bool {
  201. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  202. match (comp) {
  203. case Ordering.Less => {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. }
  210. impl String as LessWith(Self) {
  211. fn Less[self: Self](other: Self) -> bool {
  212. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  213. match(comp){
  214. case Ordering.Less => {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. }
  221. impl i32 as LessEqWith(Self) {
  222. fn LessEq[self: Self](other: Self) -> bool {
  223. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  224. match(comp){
  225. case Ordering.Less => {
  226. return true;
  227. }
  228. case Ordering.Equivalent => {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. }
  235. impl String as LessEqWith(Self) {
  236. fn LessEq[self: Self](other: Self) -> bool {
  237. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  238. match(comp){
  239. case Ordering.Less => {
  240. return true;
  241. }
  242. case Ordering.Equivalent => {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. }
  249. impl i32 as GreaterWith(Self) {
  250. fn Greater[self: Self](other: Self) -> bool {
  251. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  252. match(comp){
  253. case Ordering.Greater => {
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. }
  260. impl String as GreaterWith(Self) {
  261. fn Greater[self: Self](other: Self) -> bool {
  262. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  263. match(comp){
  264. case Ordering.Greater => {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. }
  271. impl i32 as GreaterEqWith(Self) {
  272. fn GreaterEq[self: Self](other: Self) -> bool {
  273. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  274. match(comp){
  275. case Ordering.Greater => {
  276. return true;
  277. }
  278. case Ordering.Equivalent => {
  279. return true;
  280. }
  281. }
  282. return false;
  283. }
  284. }
  285. impl String as GreaterEqWith(Self) {
  286. fn GreaterEq[self: Self](other: Self) -> bool {
  287. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  288. match(comp){
  289. case Ordering.Greater => {
  290. return true;
  291. }
  292. case Ordering.Equivalent => {
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. }
  299. // ----------------------
  300. // Arithmetic interfaces.
  301. // ----------------------
  302. interface Negate {
  303. // TODO: = Self
  304. let Result:! type;
  305. fn Op[self: Self]() -> Result;
  306. }
  307. interface AddWith(U:! type) {
  308. // TODO: = Self
  309. let Result:! type;
  310. fn Op[self: Self](other: U) -> Result;
  311. }
  312. constraint Add {
  313. extends AddWith(Self) where .Result = Self;
  314. }
  315. interface SubWith(U:! type) {
  316. // TODO: = Self
  317. let Result:! type;
  318. fn Op[self: Self](other: U) -> Result;
  319. }
  320. constraint Sub {
  321. extends SubWith(Self) where .Result = Self;
  322. }
  323. interface MulWith(U:! type) {
  324. // TODO: = Self
  325. let Result:! type;
  326. fn Op[self: Self](other: U) -> Result;
  327. }
  328. constraint Mul {
  329. extends MulWith(Self) where .Result = Self;
  330. }
  331. interface DivWith(U:! type) {
  332. // TODO: = Self
  333. let Result:! type;
  334. fn Op[self: Self](other: U) -> Result;
  335. }
  336. constraint Div {
  337. extends DivWith(Self) where .Result = Self;
  338. }
  339. interface ModWith(U:! type) {
  340. // TODO: = Self
  341. let Result:! type;
  342. fn Op[self: Self](other: U) -> Result;
  343. }
  344. constraint Mod {
  345. extends ModWith(Self) where .Result = Self;
  346. }
  347. // Note, these impl declarations use the builtin addition for i32.
  348. external impl i32 as Negate where .Result = i32 {
  349. fn Op[self: i32]() -> i32 { return -self; }
  350. }
  351. external impl i32 as AddWith(i32) where .Result = i32 {
  352. fn Op[self: i32](other: i32) -> i32 { return self + other; }
  353. }
  354. external impl i32 as SubWith(i32) where .Result = i32 {
  355. fn Op[self: i32](other: i32) -> i32 { return self - other; }
  356. }
  357. external impl i32 as MulWith(i32) where .Result = i32 {
  358. fn Op[self: i32](other: i32) -> i32 { return self * other; }
  359. }
  360. external impl i32 as DivWith(i32) where .Result = i32 {
  361. fn Op[self: i32](other: i32) -> i32 { return self / other; }
  362. }
  363. external impl i32 as ModWith(i32) where .Result = i32 {
  364. fn Op[self: i32](other: i32) -> i32 { return self % other; }
  365. }
  366. // ---------------------------------
  367. // Bitwise and bit-shift interfaces.
  368. // ---------------------------------
  369. // Unary `^`.
  370. interface BitComplement {
  371. // TODO: = Self
  372. let Result:! type;
  373. fn Op[self: Self]() -> Result;
  374. }
  375. // Binary `&`.
  376. interface BitAndWith(U:! type) {
  377. // TODO: = Self
  378. let Result:! type;
  379. fn Op[self: Self](other: U) -> Result;
  380. }
  381. constraint BitAnd {
  382. extends BitAndWith(Self) where .Result = Self;
  383. }
  384. // Binary `|`.
  385. interface BitOrWith(U:! type) {
  386. // TODO: = Self
  387. let Result:! type;
  388. fn Op[self: Self](other: U) -> Result;
  389. }
  390. constraint BitOr {
  391. extends BitOrWith(Self) where .Result = Self;
  392. }
  393. // Binary `^`.
  394. interface BitXorWith(U:! type) {
  395. // TODO: = Self
  396. let Result:! type;
  397. fn Op[self: Self](other: U) -> Result;
  398. }
  399. constraint BitXor {
  400. extends BitXorWith(Self) where .Result = Self;
  401. }
  402. // Binary `<<`.
  403. interface LeftShiftWith(U:! type) {
  404. // TODO: = Self
  405. let Result:! type;
  406. fn Op[self: Self](other: U) -> Result;
  407. }
  408. constraint LeftShift {
  409. extends LeftShiftWith(Self) where .Result = Self;
  410. }
  411. // Binary `>>`.
  412. interface RightShiftWith(U:! type) {
  413. // TODO: = Self
  414. let Result:! type;
  415. fn Op[self: Self](other: U) -> Result;
  416. }
  417. constraint RightShift {
  418. extends RightShiftWith(Self) where .Result = Self;
  419. }
  420. external impl i32 as BitComplement where .Result = i32 {
  421. fn Op[self: i32]() -> i32 {
  422. return __intrinsic_int_bit_complement(self);
  423. }
  424. }
  425. external impl i32 as BitAndWith(i32) where .Result = i32 {
  426. fn Op[self: i32](other: i32) -> i32 {
  427. return __intrinsic_int_bit_and(self, other);
  428. }
  429. }
  430. external impl i32 as BitOrWith(i32) where .Result = i32 {
  431. fn Op[self: i32](other: i32) -> i32 {
  432. return __intrinsic_int_bit_or(self, other);
  433. }
  434. }
  435. external impl i32 as BitXorWith(i32) where .Result = i32 {
  436. fn Op[self: i32](other: i32) -> i32 {
  437. return __intrinsic_int_bit_xor(self, other);
  438. }
  439. }
  440. external impl i32 as LeftShiftWith(i32) where .Result = i32 {
  441. fn Op[self: i32](other: i32) -> i32 {
  442. return __intrinsic_int_left_shift(self, other);
  443. }
  444. }
  445. external impl i32 as RightShiftWith(i32) where .Result = i32 {
  446. fn Op[self: i32](other: i32) -> i32 {
  447. return __intrinsic_int_right_shift(self, other);
  448. }
  449. }
  450. // -----------------------------------
  451. // Assignment and compound assignment.
  452. // -----------------------------------
  453. interface AssignWith(U:! type) {
  454. fn Op[addr self: Self*](other: U);
  455. }
  456. constraint Assign { extends AssignWith(Self); }
  457. interface AddAssignWith(U:! type) {
  458. fn Op[addr self: Self*](other: U);
  459. }
  460. constraint AddAssign { extends AddAssignWith(Self); }
  461. interface SubAssignWith(U:! type) {
  462. fn Op[addr self: Self*](other: U);
  463. }
  464. constraint SubAssign { extends SubAssignWith(Self); }
  465. interface MulAssignWith(U:! type) {
  466. fn Op[addr self: Self*](other: U);
  467. }
  468. constraint MulAssign { extends MulAssignWith(Self); }
  469. interface DivAssignWith(U:! type) {
  470. fn Op[addr self: Self*](other: U);
  471. }
  472. constraint DivAssign { extends DivAssignWith(Self); }
  473. interface ModAssignWith(U:! type) {
  474. fn Op[addr self: Self*](other: U);
  475. }
  476. constraint ModAssign { extends ModAssignWith(Self); }
  477. interface BitAndAssignWith(U:! type) {
  478. fn Op[addr self: Self*](other: U);
  479. }
  480. constraint BitAssignAnd { extends BitAndAssignWith(Self); }
  481. interface BitOrAssignWith(U:! type) {
  482. fn Op[addr self: Self*](other: U);
  483. }
  484. constraint BitAssignOr { extends BitOrAssignWith(Self); }
  485. interface BitXorAssignWith(U:! type) {
  486. fn Op[addr self: Self*](other: U);
  487. }
  488. constraint BitAssignXor { extends BitXorAssignWith(Self); }
  489. interface LeftShiftAssignWith(U:! type) {
  490. fn Op[addr self: Self*](other: U);
  491. }
  492. constraint LeftShiftAssign { extends LeftShiftAssignWith(Self); }
  493. interface RightShiftAssignWith(U:! type) {
  494. fn Op[addr self: Self*](other: U);
  495. }
  496. constraint RightShiftAssign { extends RightShiftAssignWith(Self); }
  497. // TODO: This is temporary, and should eventually be replaced by
  498. // something more fine-grained. Not all class types should be
  499. // assignable.
  500. impl forall [T:! type, U:! ImplicitAs(T)]
  501. T as AssignWith(U) {
  502. fn Op[addr self: Self*](other: U) {
  503. *self = other.Convert();
  504. }
  505. }
  506. // TODO: Should `AddWith(U) & AssignWith(.Self.(AddWith(U).Result))` work?
  507. impl forall [U:! type, T:! AddWith(U) where .Self impls AssignWith(.Self.Result)]
  508. T as AddAssignWith(U) {
  509. fn Op[addr self: Self*](other: U) {
  510. *self = *self + other;
  511. }
  512. }
  513. impl forall [U:! type, T:! SubWith(U) where .Self impls AssignWith(.Self.Result)]
  514. T as SubAssignWith(U) {
  515. fn Op[addr self: Self*](other: U) {
  516. *self = *self - other;
  517. }
  518. }
  519. impl forall [U:! type, T:! MulWith(U) where .Self impls AssignWith(.Self.Result)]
  520. T as MulAssignWith(U) {
  521. fn Op[addr self: Self*](other: U) {
  522. *self = *self * other;
  523. }
  524. }
  525. impl forall [U:! type, T:! DivWith(U) where .Self impls AssignWith(.Self.Result)]
  526. T as DivAssignWith(U) {
  527. fn Op[addr self: Self*](other: U) {
  528. *self = *self / other;
  529. }
  530. }
  531. impl forall [U:! type, T:! ModWith(U) where .Self impls AssignWith(.Self.Result)]
  532. T as ModAssignWith(U) {
  533. fn Op[addr self: Self*](other: U) {
  534. *self = *self % other;
  535. }
  536. }
  537. impl forall [U:! type, T:! BitAndWith(U) where .Self impls AssignWith(.Self.Result)]
  538. T as BitAndAssignWith(U) {
  539. fn Op[addr self: Self*](other: U) {
  540. *self = *self & other;
  541. }
  542. }
  543. impl forall [U:! type, T:! BitOrWith(U) where .Self impls AssignWith(.Self.Result)]
  544. T as BitOrAssignWith(U) {
  545. fn Op[addr self: Self*](other: U) {
  546. *self = *self | other;
  547. }
  548. }
  549. impl forall [U:! type, T:! BitXorWith(U) where .Self impls AssignWith(.Self.Result)]
  550. T as BitXorAssignWith(U) {
  551. fn Op[addr self: Self*](other: U) {
  552. *self = *self ^ other;
  553. }
  554. }
  555. impl forall [U:! type, T:! LeftShiftWith(U) where .Self impls AssignWith(.Self.Result)]
  556. T as LeftShiftAssignWith(U) {
  557. fn Op[addr self: Self*](other: U) {
  558. *self = *self << other;
  559. }
  560. }
  561. impl forall [U:! type, T:! RightShiftWith(U) where .Self impls AssignWith(.Self.Result)]
  562. T as RightShiftAssignWith(U) {
  563. fn Op[addr self: Self*](other: U) {
  564. *self = *self >> other;
  565. }
  566. }
  567. // ------------------------
  568. // Increment and decrement.
  569. // ------------------------
  570. interface Inc {
  571. fn Op[addr self: Self*]();
  572. }
  573. interface Dec {
  574. fn Op[addr self: Self*]();
  575. }
  576. impl i32 as Inc {
  577. fn Op[addr self: Self*]() {
  578. *self = *self + 1;
  579. }
  580. }
  581. impl i32 as Dec {
  582. fn Op[addr self: Self*]() {
  583. *self = *self - 1;
  584. }
  585. }
  586. // ------------------------
  587. // Miscellaneous utilities.
  588. // ------------------------
  589. // Note that Print is experimental, and not part of an accepted proposal, but
  590. // is included here for printing state in tests.
  591. // TODO: Remove Print special casing once we have variadics or overloads.
  592. // fn Print(format_str: String) {
  593. // __intrinsic_print(format_str);
  594. // }
  595. fn Assert(condition: bool, message: String){
  596. __intrinsic_assert(condition, message);
  597. }
  598. fn Rand(low: i32, high: i32) -> i32{
  599. return __intrinsic_rand(low,high);
  600. }
  601. //-------------------------
  602. // Optional.
  603. //-------------------------
  604. choice OptionalElement(T:! type) {
  605. None,
  606. Element(T)
  607. }
  608. class Optional(T:! type) {
  609. fn CreateEmpty() -> Optional(T) {
  610. return {.element = OptionalElement(T).None};
  611. }
  612. fn Create(value: T) -> Optional(T) {
  613. return {.element = OptionalElement(T).Element(value)};
  614. }
  615. fn HasValue[self: Self]() -> bool {
  616. match(self.element) {
  617. case OptionalElement(T).None => { return false; }
  618. }
  619. return true;
  620. }
  621. fn Get[self: Self]() -> T {
  622. match(self.element) {
  623. case OptionalElement(T).Element(x: T) => {
  624. return x;
  625. }
  626. }
  627. Assert(false, "Attempted to unwrap empty Optional");
  628. // TODO: Drop uninitialized variable & return when we can flag unreachable paths.
  629. var y: T;
  630. return y;
  631. }
  632. var element: OptionalElement(T);
  633. }
  634. //-------------------------
  635. // Heap.
  636. //-------------------------
  637. class Heap {
  638. fn New[T:! type, self: Self](x : T) -> T* {
  639. return __intrinsic_new(x);
  640. }
  641. fn Delete[T:! type, self: Self](p : T*) {
  642. __intrinsic_delete(p);
  643. }
  644. }
  645. var heap: Heap = {};