demo.odin 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. package main
  2. import "core:fmt"
  3. import "core:mem"
  4. import "core:os"
  5. import "core:thread"
  6. import "core:time"
  7. import "core:reflect"
  8. import "core:runtime"
  9. import "core:intrinsics"
  10. /*
  11. The Odin programming language is fast, concise, readable, pragmatic and open sourced.
  12. It is designed with the intent of replacing C with the following goals:
  13. * simplicity
  14. * high performance
  15. * built for modern systems
  16. * joy of programming
  17. # Installing Odin
  18. Getting Started - https://odin-lang.org/docs/install/
  19. Instructions for downloading and install the Odin compiler and libraries.
  20. # Learning Odin
  21. Overview of Odin - https://odin-lang.org/docs/overview/
  22. An overview of the Odin programming language.
  23. Frequently Asked Questions (FAQ) - https://odin-lang.org/docs/faq/
  24. Answers to common questions about Odin.
  25. */
  26. the_basics :: proc() {
  27. fmt.println("\n# the basics");
  28. { // The Basics
  29. fmt.println("Hellope");
  30. // Lexical elements and literals
  31. // A comment
  32. my_integer_variable: int; // A comment for documentaton
  33. // Multi-line comments begin with /* and end with */. Multi-line comments can
  34. // also be nested (unlike in C):
  35. /*
  36. You can have any text or code here and
  37. have it be commented.
  38. /*
  39. NOTE: comments can be nested!
  40. */
  41. */
  42. // String literals are enclosed in double quotes and character literals in single quotes.
  43. // Special characters are escaped with a backslash \
  44. some_string := "This is a string";
  45. _ = 'A'; // unicode codepoint literal
  46. _ = '\n';
  47. _ = "C:\\Windows\\notepad.exe";
  48. // Raw string literals are enclosed with single back ticks
  49. _ = `C:\Windows\notepad.exe`;
  50. // The length of a string in bytes can be found using the built-in `len` procedure:
  51. _ = len("Foo");
  52. _ = len(some_string);
  53. // Numbers
  54. // Numerical literals are written similar to most other programming languages.
  55. // A useful feature in Odin is that underscores are allowed for better
  56. // readability: 1_000_000_000 (one billion). A number that contains a dot is a
  57. // floating point literal: 1.0e9 (one billion). If a number literal is suffixed
  58. // with i, is an imaginary number literal: 2i (2 multiply the square root of -1).
  59. // Binary literals are prefixed with 0b, octal literals with 0o, and hexadecimal
  60. // literals 0x. A leading zero does not produce an octal constant (unlike C).
  61. // In Odin, if a numeric constant can be represented by a type without
  62. // precision loss, it will automatically convert to that type.
  63. x: int = 1.0; // A float literal but it can be represented by an integer without precision loss
  64. // Constant literals are “untyped” which means that they can implicitly convert to a type.
  65. y: int; // `y` is typed of type `int`
  66. y = 1; // `1` is an untyped integer literal which can implicitly convert to `int`
  67. z: f64; // `z` is typed of type `f64` (64-bit floating point number)
  68. z = 1; // `1` is an untyped integer literal which can be implicitly converted to `f64`
  69. // No need for any suffixes or decimal places like in other languages
  70. // CONSTANTS JUST WORK!!!
  71. // Assignment statements
  72. h: int = 123; // declares a new variable `h` with type `int` and assigns a value to it
  73. h = 637; // assigns a new value to `h`
  74. // `=` is the assignment operator
  75. // You can assign multiple variables with it:
  76. a, b := 1, "hello"; // declares `a` and `b` and infers the types from the assignments
  77. b, a = "byte", 0;
  78. // Note: `:=` is two tokens, `:` and `=`. The following are equivalent,
  79. /*
  80. i: int = 123;
  81. i: = 123;
  82. i := 123;
  83. */
  84. // Constant declarations
  85. // Constants are entities (symbols) which have an assigned value.
  86. // The constant’s value cannot be changed.
  87. // The constant’s value must be able to be evaluated at compile time:
  88. X :: "what"; // constant `X` has the untyped string value "what"
  89. // Constants can be explicitly typed like a variable declaration:
  90. Y : int : 123;
  91. Z :: Y + 7; // constant computations are possible
  92. _ = my_integer_variable;
  93. _ = x;
  94. }
  95. }
  96. control_flow :: proc() {
  97. fmt.println("\n# control flow");
  98. { // Control flow
  99. // For loop
  100. // Odin has only one loop statement, the `for` loop
  101. // Basic for loop
  102. for i := 0; i < 10; i += 1 {
  103. fmt.println(i);
  104. }
  105. // NOTE: Unlike other languages like C, there are no parentheses `( )` surrounding the three components.
  106. // Braces `{ }` or a `do` are always required
  107. for i := 0; i < 10; i += 1 { }
  108. // for i := 0; i < 10; i += 1 do fmt.print();
  109. // The initial and post statements are optional
  110. i := 0;
  111. for ; i < 10; {
  112. i += 1;
  113. }
  114. // These semicolons can be dropped. This `for` loop is equivalent to C's `while` loop
  115. i = 0;
  116. for i < 10 {
  117. i += 1;
  118. }
  119. // If the condition is omitted, an infinite loop is produced:
  120. for {
  121. break;
  122. }
  123. // Range-based for loop
  124. // The basic for loop
  125. for j := 0; j < 10; j += 1 {
  126. fmt.println(j);
  127. }
  128. // can also be written
  129. for j in 0..<10 {
  130. fmt.println(j);
  131. }
  132. for j in 0..=9 {
  133. fmt.println(j);
  134. }
  135. // Certain built-in types can be iterated over
  136. some_string := "Hello, 世界";
  137. for character in some_string { // Strings are assumed to be UTF-8
  138. fmt.println(character);
  139. }
  140. some_array := [3]int{1, 4, 9};
  141. for value in some_array {
  142. fmt.println(value);
  143. }
  144. some_slice := []int{1, 4, 9};
  145. for value in some_slice {
  146. fmt.println(value);
  147. }
  148. some_dynamic_array := [dynamic]int{1, 4, 9};
  149. defer delete(some_dynamic_array);
  150. for value in some_dynamic_array {
  151. fmt.println(value);
  152. }
  153. some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4};
  154. defer delete(some_map);
  155. for key in some_map {
  156. fmt.println(key);
  157. }
  158. // Alternatively a second index value can be added
  159. for character, index in some_string {
  160. fmt.println(index, character);
  161. }
  162. for value, index in some_array {
  163. fmt.println(index, value);
  164. }
  165. for value, index in some_slice {
  166. fmt.println(index, value);
  167. }
  168. for value, index in some_dynamic_array {
  169. fmt.println(index, value);
  170. }
  171. for key, value in some_map {
  172. fmt.println(key, value);
  173. }
  174. // The iterated values are copies and cannot be written to.
  175. // The following idiom is useful for iterating over a container in a by-reference manner:
  176. for _, idx in some_slice {
  177. some_slice[idx] = (idx+1)*(idx+1);
  178. }
  179. // If statements
  180. x := 123;
  181. if x >= 0 {
  182. fmt.println("x is positive");
  183. }
  184. if y := -34; y < 0 {
  185. fmt.println("y is negative");
  186. }
  187. if y := 123; y < 0 {
  188. fmt.println("y is negative");
  189. } else if y == 0 {
  190. fmt.println("y is zero");
  191. } else {
  192. fmt.println("y is positive");
  193. }
  194. // Switch statement
  195. // A switch statement is another way to write a sequence of if-else statements.
  196. // In Odin, the default case is denoted as a case without any expression.
  197. switch arch := ODIN_ARCH; arch {
  198. case "386":
  199. fmt.println("32-bit");
  200. case "amd64":
  201. fmt.println("64-bit");
  202. case: // default
  203. fmt.println("Unsupported architecture");
  204. }
  205. // Odin’s `switch` is like one in C or C++, except that Odin only runs the selected case.
  206. // This means that a `break` statement is not needed at the end of each case.
  207. // Another important difference is that the case values need not be integers nor constants.
  208. // To achieve a C-like fall through into the next case block, the keyword `fallthrough` can be used.
  209. one_angry_dwarf :: proc() -> int {
  210. fmt.println("one_angry_dwarf was called");
  211. return 1;
  212. }
  213. switch j := 0; j {
  214. case 0:
  215. case one_angry_dwarf():
  216. }
  217. // A switch statement without a condition is the same as `switch true`.
  218. // This can be used to write a clean and long if-else chain and have the
  219. // ability to break if needed
  220. switch {
  221. case x < 0:
  222. fmt.println("x is negative");
  223. case x == 0:
  224. fmt.println("x is zero");
  225. case:
  226. fmt.println("x is positive");
  227. }
  228. // A `switch` statement can also use ranges like a range-based loop:
  229. switch c := 'j'; c {
  230. case 'A'..='Z', 'a'..='z', '0'..='9':
  231. fmt.println("c is alphanumeric");
  232. }
  233. switch x {
  234. case 0..<10:
  235. fmt.println("units");
  236. case 10..<13:
  237. fmt.println("pre-teens");
  238. case 13..<20:
  239. fmt.println("teens");
  240. case 20..<30:
  241. fmt.println("twenties");
  242. }
  243. }
  244. { // Defer statement
  245. // A defer statement defers the execution of a statement until the end of
  246. // the scope it is in.
  247. // The following will print 4 then 234:
  248. {
  249. x := 123;
  250. defer fmt.println(x);
  251. {
  252. defer x = 4;
  253. x = 2;
  254. }
  255. fmt.println(x);
  256. x = 234;
  257. }
  258. // You can defer an entire block too:
  259. {
  260. bar :: proc() {}
  261. defer {
  262. fmt.println("1");
  263. fmt.println("2");
  264. }
  265. cond := false;
  266. defer if cond {
  267. bar();
  268. }
  269. }
  270. // Defer statements are executed in the reverse order that they were declared:
  271. {
  272. defer fmt.println("1");
  273. defer fmt.println("2");
  274. defer fmt.println("3");
  275. }
  276. // Will print 3, 2, and then 1.
  277. if false {
  278. f, err := os.open("my_file.txt");
  279. if err != 0 {
  280. // handle error
  281. }
  282. defer os.close(f);
  283. // rest of code
  284. }
  285. }
  286. { // When statement
  287. /*
  288. The when statement is almost identical to the if statement but with some differences:
  289. * Each condition must be a constant expression as a when
  290. statement is evaluated at compile time.
  291. * The statements within a branch do not create a new scope
  292. * The compiler checks the semantics and code only for statements
  293. that belong to the first condition that is true
  294. * An initial statement is not allowed in a when statement
  295. * when statements are allowed at file scope
  296. */
  297. // Example
  298. when ODIN_ARCH == "386" {
  299. fmt.println("32 bit");
  300. } else when ODIN_ARCH == "amd64" {
  301. fmt.println("64 bit");
  302. } else {
  303. fmt.println("Unsupported architecture");
  304. }
  305. // The when statement is very useful for writing platform specific code.
  306. // This is akin to the #if construct in C’s preprocessor however, in Odin,
  307. // it is type checked.
  308. }
  309. { // Branch statements
  310. cond, cond1, cond2 := false, false, false;
  311. one_step :: proc() { fmt.println("one_step"); }
  312. beyond :: proc() { fmt.println("beyond"); }
  313. // Break statement
  314. for cond {
  315. switch {
  316. case:
  317. if cond {
  318. break; // break out of the `switch` statement
  319. }
  320. }
  321. break; // break out of the `for` statement
  322. }
  323. loop: for cond1 {
  324. for cond2 {
  325. break loop; // leaves both loops
  326. }
  327. }
  328. // Continue statement
  329. for cond {
  330. if cond2 {
  331. continue;
  332. }
  333. fmt.println("Hellope");
  334. }
  335. // Fallthrough statement
  336. // Odin’s switch is like one in C or C++, except that Odin only runs the selected
  337. // case. This means that a break statement is not needed at the end of each case.
  338. // Another important difference is that the case values need not be integers nor
  339. // constants.
  340. // fallthrough can be used to explicitly fall through into the next case block:
  341. switch i := 0; i {
  342. case 0:
  343. one_step();
  344. fallthrough;
  345. case 1:
  346. beyond();
  347. }
  348. }
  349. }
  350. named_proc_return_parameters :: proc() {
  351. fmt.println("\n# named proc return parameters");
  352. foo0 :: proc() -> int {
  353. return 123;
  354. }
  355. foo1 :: proc() -> (a: int) {
  356. a = 123;
  357. return;
  358. }
  359. foo2 :: proc() -> (a, b: int) {
  360. // Named return values act like variables within the scope
  361. a = 321;
  362. b = 567;
  363. return b, a;
  364. }
  365. fmt.println("foo0 =", foo0()); // 123
  366. fmt.println("foo1 =", foo1()); // 123
  367. fmt.println("foo2 =", foo2()); // 567 321
  368. }
  369. explicit_procedure_overloading :: proc() {
  370. fmt.println("\n# explicit procedure overloading");
  371. add_ints :: proc(a, b: int) -> int {
  372. x := a + b;
  373. fmt.println("add_ints", x);
  374. return x;
  375. }
  376. add_floats :: proc(a, b: f32) -> f32 {
  377. x := a + b;
  378. fmt.println("add_floats", x);
  379. return x;
  380. }
  381. add_numbers :: proc(a: int, b: f32, c: u8) -> int {
  382. x := int(a) + int(b) + int(c);
  383. fmt.println("add_numbers", x);
  384. return x;
  385. }
  386. add :: proc{add_ints, add_floats, add_numbers};
  387. add(int(1), int(2));
  388. add(f32(1), f32(2));
  389. add(int(1), f32(2), u8(3));
  390. add(1, 2); // untyped ints coerce to int tighter than f32
  391. add(1.0, 2.0); // untyped floats coerce to f32 tighter than int
  392. add(1, 2, 3); // three parameters
  393. // Ambiguous answers
  394. // add(1.0, 2);
  395. // add(1, 2.0);
  396. }
  397. struct_type :: proc() {
  398. fmt.println("\n# struct type");
  399. // A struct is a record type in Odin. It is a collection of fields.
  400. // Struct fields are accessed by using a dot:
  401. {
  402. Vector2 :: struct {
  403. x: f32,
  404. y: f32,
  405. };
  406. v := Vector2{1, 2};
  407. v.x = 4;
  408. fmt.println(v.x);
  409. // Struct fields can be accessed through a struct pointer:
  410. v = Vector2{1, 2};
  411. p := &v;
  412. p.x = 1335;
  413. fmt.println(v);
  414. // We could write p^.x, however, it is to nice abstract the ability
  415. // to not explicitly dereference the pointer. This is very useful when
  416. // refactoring code to use a pointer rather than a value, and vice versa.
  417. }
  418. {
  419. // A struct literal can be denoted by providing the struct’s type
  420. // followed by {}. A struct literal must either provide all the
  421. // arguments or none:
  422. Vector3 :: struct {
  423. x, y, z: f32,
  424. };
  425. v: Vector3;
  426. v = Vector3{}; // Zero value
  427. v = Vector3{1, 4, 9};
  428. // You can list just a subset of the fields if you specify the
  429. // field by name (the order of the named fields does not matter):
  430. v = Vector3{z=1, y=2};
  431. assert(v.x == 0);
  432. assert(v.y == 2);
  433. assert(v.z == 1);
  434. }
  435. {
  436. // Structs can tagged with different memory layout and alignment requirements:
  437. a :: struct #align 4 {}; // align to 4 bytes
  438. b :: struct #packed {}; // remove padding between fields
  439. c :: struct #raw_union {}; // all fields share the same offset (0). This is the same as C's union
  440. }
  441. }
  442. union_type :: proc() {
  443. fmt.println("\n# union type");
  444. {
  445. val: union{int, bool};
  446. val = 137;
  447. if i, ok := val.(int); ok {
  448. fmt.println(i);
  449. }
  450. val = true;
  451. fmt.println(val);
  452. val = nil;
  453. switch v in val {
  454. case int: fmt.println("int", v);
  455. case bool: fmt.println("bool", v);
  456. case: fmt.println("nil");
  457. }
  458. }
  459. {
  460. // There is a duality between `any` and `union`
  461. // An `any` has a pointer to the data and allows for any type (open)
  462. // A `union` has as binary blob to store the data and allows only certain types (closed)
  463. // The following code is with `any` but has the same syntax
  464. val: any;
  465. val = 137;
  466. if i, ok := val.(int); ok {
  467. fmt.println(i);
  468. }
  469. val = true;
  470. fmt.println(val);
  471. val = nil;
  472. switch v in val {
  473. case int: fmt.println("int", v);
  474. case bool: fmt.println("bool", v);
  475. case: fmt.println("nil");
  476. }
  477. }
  478. Vector3 :: distinct [3]f32;
  479. Quaternion :: distinct quaternion128;
  480. // More realistic examples
  481. {
  482. // NOTE(bill): For the above basic examples, you may not have any
  483. // particular use for it. However, my main use for them is not for these
  484. // simple cases. My main use is for hierarchical types. Many prefer
  485. // subtyping, embedding the base data into the derived types. Below is
  486. // an example of this for a basic game Entity.
  487. Entity :: struct {
  488. id: u64,
  489. name: string,
  490. position: Vector3,
  491. orientation: Quaternion,
  492. derived: any,
  493. };
  494. Frog :: struct {
  495. using entity: Entity,
  496. jump_height: f32,
  497. };
  498. Monster :: struct {
  499. using entity: Entity,
  500. is_robot: bool,
  501. is_zombie: bool,
  502. };
  503. // See `parametric_polymorphism` procedure for details
  504. new_entity :: proc($T: typeid) -> ^Entity {
  505. t := new(T);
  506. t.derived = t^;
  507. return t;
  508. }
  509. entity := new_entity(Monster);
  510. switch e in entity.derived {
  511. case Frog:
  512. fmt.println("Ribbit");
  513. case Monster:
  514. if e.is_robot { fmt.println("Robotic"); }
  515. if e.is_zombie { fmt.println("Grrrr!"); }
  516. fmt.println("I'm a monster");
  517. }
  518. }
  519. {
  520. // NOTE(bill): A union can be used to achieve something similar. Instead
  521. // of embedding the base data into the derived types, the derived data
  522. // in embedded into the base type. Below is the same example of the
  523. // basic game Entity but using an union.
  524. Entity :: struct {
  525. id: u64,
  526. name: string,
  527. position: Vector3,
  528. orientation: Quaternion,
  529. derived: union {Frog, Monster},
  530. };
  531. Frog :: struct {
  532. using entity: ^Entity,
  533. jump_height: f32,
  534. };
  535. Monster :: struct {
  536. using entity: ^Entity,
  537. is_robot: bool,
  538. is_zombie: bool,
  539. };
  540. // See `parametric_polymorphism` procedure for details
  541. new_entity :: proc($T: typeid) -> ^Entity {
  542. t := new(Entity);
  543. t.derived = T{entity = t};
  544. return t;
  545. }
  546. entity := new_entity(Monster);
  547. switch e in entity.derived {
  548. case Frog:
  549. fmt.println("Ribbit");
  550. case Monster:
  551. if e.is_robot { fmt.println("Robotic"); }
  552. if e.is_zombie { fmt.println("Grrrr!"); }
  553. }
  554. // NOTE(bill): As you can see, the usage code has not changed, only its
  555. // memory layout. Both approaches have their own advantages but they can
  556. // be used together to achieve different results. The subtyping approach
  557. // can allow for a greater control of the memory layout and memory
  558. // allocation, e.g. storing the derivatives together. However, this is
  559. // also its disadvantage. You must either preallocate arrays for each
  560. // derivative separation (which can be easily missed) or preallocate a
  561. // bunch of "raw" memory; determining the maximum size of the derived
  562. // types would require the aid of metaprogramming. Unions solve this
  563. // particular problem as the data is stored with the base data.
  564. // Therefore, it is possible to preallocate, e.g. [100]Entity.
  565. // It should be noted that the union approach can have the same memory
  566. // layout as the any and with the same type restrictions by using a
  567. // pointer type for the derivatives.
  568. /*
  569. Entity :: struct {
  570. ...
  571. derived: union{^Frog, ^Monster},
  572. }
  573. Frog :: struct {
  574. using entity: Entity,
  575. ...
  576. }
  577. Monster :: struct {
  578. using entity: Entity,
  579. ...
  580. }
  581. new_entity :: proc(T: type) -> ^Entity {
  582. t := new(T);
  583. t.derived = t;
  584. return t;
  585. }
  586. */
  587. }
  588. }
  589. using_statement :: proc() {
  590. fmt.println("\n# using statement");
  591. // using can used to bring entities declared in a scope/namespace
  592. // into the current scope. This can be applied to import names, struct
  593. // fields, procedure fields, and struct values.
  594. Vector3 :: struct{x, y, z: f32};
  595. {
  596. Entity :: struct {
  597. position: Vector3,
  598. orientation: quaternion128,
  599. };
  600. // It can used like this:
  601. foo0 :: proc(entity: ^Entity) {
  602. fmt.println(entity.position.x, entity.position.y, entity.position.z);
  603. }
  604. // The entity members can be brought into the procedure scope by using it:
  605. foo1 :: proc(entity: ^Entity) {
  606. using entity;
  607. fmt.println(position.x, position.y, position.z);
  608. }
  609. // The using can be applied to the parameter directly:
  610. foo2 :: proc(using entity: ^Entity) {
  611. fmt.println(position.x, position.y, position.z);
  612. }
  613. // It can also be applied to sub-fields:
  614. foo3 :: proc(entity: ^Entity) {
  615. using entity.position;
  616. fmt.println(x, y, z);
  617. }
  618. }
  619. {
  620. // We can also apply the using statement to the struct fields directly,
  621. // making all the fields of position appear as if they on Entity itself:
  622. Entity :: struct {
  623. using position: Vector3,
  624. orientation: quaternion128,
  625. };
  626. foo :: proc(entity: ^Entity) {
  627. fmt.println(entity.x, entity.y, entity.z);
  628. }
  629. // Subtype polymorphism
  630. // It is possible to get subtype polymorphism, similar to inheritance-like
  631. // functionality in C++, but without the requirement of vtables or unknown
  632. // struct layout:
  633. Colour :: struct {r, g, b, a: u8};
  634. Frog :: struct {
  635. ribbit_volume: f32,
  636. using entity: Entity,
  637. colour: Colour,
  638. };
  639. frog: Frog;
  640. // Both work
  641. foo(&frog.entity);
  642. foo(&frog);
  643. frog.x = 123;
  644. // Note: using can be applied to arbitrarily many things, which allows
  645. // the ability to have multiple subtype polymorphism (but also its issues).
  646. // Note: using’d fields can still be referred by name.
  647. }
  648. }
  649. implicit_context_system :: proc() {
  650. fmt.println("\n# implicit context system");
  651. // In each scope, there is an implicit value named context. This
  652. // context variable is local to each scope and is implicitly passed
  653. // by pointer to any procedure call in that scope (if the procedure
  654. // has the Odin calling convention).
  655. // The main purpose of the implicit context system is for the ability
  656. // to intercept third-party code and libraries and modify their
  657. // functionality. One such case is modifying how a library allocates
  658. // something or logs something. In C, this was usually achieved with
  659. // the library defining macros which could be overridden so that the
  660. // user could define what he wanted. However, not many libraries
  661. // supported this in many languages by default which meant intercepting
  662. // third-party code to see what it does and to change how it does it is
  663. // not possible.
  664. c := context; // copy the current scope's context
  665. context.user_index = 456;
  666. {
  667. context.allocator = my_custom_allocator();
  668. context.user_index = 123;
  669. what_a_fool_believes(); // the `context` for this scope is implicitly passed to `what_a_fool_believes`
  670. }
  671. // `context` value is local to the scope it is in
  672. assert(context.user_index == 456);
  673. what_a_fool_believes :: proc() {
  674. c := context; // this `context` is the same as the parent procedure that it was called from
  675. // From this example, context.user_index == 123
  676. // An context.allocator is assigned to the return value of `my_custom_allocator()`
  677. assert(context.user_index == 123);
  678. // The memory management procedure use the `context.allocator` by
  679. // default unless explicitly specified otherwise
  680. china_grove := new(int);
  681. free(china_grove);
  682. _ = c;
  683. }
  684. my_custom_allocator :: mem.nil_allocator;
  685. _ = c;
  686. // By default, the context value has default values for its parameters which is
  687. // decided in the package runtime. What the defaults are are compiler specific.
  688. // To see what the implicit context value contains, please see the following
  689. // definition in package runtime.
  690. }
  691. parametric_polymorphism :: proc() {
  692. fmt.println("\n# parametric polymorphism");
  693. print_value :: proc(value: $T) {
  694. fmt.printf("print_value: %T %v\n", value, value);
  695. }
  696. v1: int = 1;
  697. v2: f32 = 2.1;
  698. v3: f64 = 3.14;
  699. v4: string = "message";
  700. print_value(v1);
  701. print_value(v2);
  702. print_value(v3);
  703. print_value(v4);
  704. fmt.println();
  705. add :: proc(p, q: $T) -> T {
  706. x: T = p + q;
  707. return x;
  708. }
  709. a := add(3, 4);
  710. fmt.printf("a: %T = %v\n", a, a);
  711. b := add(3.2, 4.3);
  712. fmt.printf("b: %T = %v\n", b, b);
  713. // This is how `new` is implemented
  714. alloc_type :: proc($T: typeid) -> ^T {
  715. t := cast(^T)alloc(size_of(T), align_of(T));
  716. t^ = T{}; // Use default initialization value
  717. return t;
  718. }
  719. copy_slice :: proc(dst, src: []$T) -> int {
  720. n := min(len(dst), len(src));
  721. if n > 0 {
  722. mem.copy(&dst[0], &src[0], n*size_of(T));
  723. }
  724. return n;
  725. }
  726. double_params :: proc(a: $A, b: $B) -> A {
  727. return a + A(b);
  728. }
  729. fmt.println(double_params(12, 1.345));
  730. { // Polymorphic Types and Type Specialization
  731. Table_Slot :: struct($Key, $Value: typeid) {
  732. occupied: bool,
  733. hash: u32,
  734. key: Key,
  735. value: Value,
  736. };
  737. TABLE_SIZE_MIN :: 32;
  738. Table :: struct($Key, $Value: typeid) {
  739. count: int,
  740. allocator: mem.Allocator,
  741. slots: []Table_Slot(Key, Value),
  742. };
  743. // Only allow types that are specializations of a (polymorphic) slice
  744. make_slice :: proc($T: typeid/[]$E, len: int) -> T {
  745. return make(T, len);
  746. }
  747. // Only allow types that are specializations of `Table`
  748. allocate :: proc(table: ^$T/Table, capacity: int) {
  749. c := context;
  750. if table.allocator.procedure != nil {
  751. c.allocator = table.allocator;
  752. }
  753. context = c;
  754. table.slots = make_slice(type_of(table.slots), max(capacity, TABLE_SIZE_MIN));
  755. }
  756. expand :: proc(table: ^$T/Table) {
  757. c := context;
  758. if table.allocator.procedure != nil {
  759. c.allocator = table.allocator;
  760. }
  761. context = c;
  762. old_slots := table.slots;
  763. defer delete(old_slots);
  764. cap := max(2*len(table.slots), TABLE_SIZE_MIN);
  765. allocate(table, cap);
  766. for s in old_slots {
  767. if s.occupied {
  768. put(table, s.key, s.value);
  769. }
  770. }
  771. }
  772. // Polymorphic determination of a polymorphic struct
  773. // put :: proc(table: ^$T/Table, key: T.Key, value: T.Value) {
  774. put :: proc(table: ^Table($Key, $Value), key: Key, value: Value) {
  775. hash := get_hash(key); // Ad-hoc method which would fail in a different scope
  776. index := find_index(table, key, hash);
  777. if index < 0 {
  778. if f64(table.count) >= 0.75*f64(len(table.slots)) {
  779. expand(table);
  780. }
  781. assert(table.count <= len(table.slots));
  782. index = int(hash % u32(len(table.slots)));
  783. for table.slots[index].occupied {
  784. if index += 1; index >= len(table.slots) {
  785. index = 0;
  786. }
  787. }
  788. table.count += 1;
  789. }
  790. slot := &table.slots[index];
  791. slot.occupied = true;
  792. slot.hash = hash;
  793. slot.key = key;
  794. slot.value = value;
  795. }
  796. // find :: proc(table: ^$T/Table, key: T.Key) -> (T.Value, bool) {
  797. find :: proc(table: ^Table($Key, $Value), key: Key) -> (Value, bool) {
  798. hash := get_hash(key);
  799. index := find_index(table, key, hash);
  800. if index < 0 {
  801. return Value{}, false;
  802. }
  803. return table.slots[index].value, true;
  804. }
  805. find_index :: proc(table: ^Table($Key, $Value), key: Key, hash: u32) -> int {
  806. if len(table.slots) <= 0 {
  807. return -1;
  808. }
  809. index := int(hash % u32(len(table.slots)));
  810. for table.slots[index].occupied {
  811. if table.slots[index].hash == hash {
  812. if table.slots[index].key == key {
  813. return index;
  814. }
  815. }
  816. if index += 1; index >= len(table.slots) {
  817. index = 0;
  818. }
  819. }
  820. return -1;
  821. }
  822. get_hash :: proc(s: string) -> u32 { // fnv32a
  823. h: u32 = 0x811c9dc5;
  824. for i in 0..<len(s) {
  825. h = (h ~ u32(s[i])) * 0x01000193;
  826. }
  827. return h;
  828. }
  829. table: Table(string, int);
  830. for i in 0..=36 { put(&table, "Hellope", i); }
  831. for i in 0..=42 { put(&table, "World!", i); }
  832. found, _ := find(&table, "Hellope");
  833. fmt.printf("`found` is %v\n", found);
  834. found, _ = find(&table, "World!");
  835. fmt.printf("`found` is %v\n", found);
  836. // I would not personally design a hash table like this in production
  837. // but this is a nice basic example
  838. // A better approach would either use a `u64` or equivalent for the key
  839. // and let the user specify the hashing function or make the user store
  840. // the hashing procedure with the table
  841. }
  842. { // Parametric polymorphic union
  843. Error :: enum {
  844. Foo0,
  845. Foo1,
  846. Foo2,
  847. Foo3,
  848. };
  849. Para_Union :: union($T: typeid) {T, Error};
  850. r: Para_Union(int);
  851. fmt.println(typeid_of(type_of(r)));
  852. fmt.println(r);
  853. r = 123;
  854. fmt.println(r);
  855. r = Error.Foo0; // r = .Foo0; is allow too, see implicit selector expressions below
  856. fmt.println(r);
  857. }
  858. { // Polymorphic names
  859. foo :: proc($N: $I, $T: typeid) -> (res: [N]T) {
  860. // `N` is the constant value passed
  861. // `I` is the type of N
  862. // `T` is the type passed
  863. fmt.printf("Generating an array of type %v from the value %v of type %v\n",
  864. typeid_of(type_of(res)), N, typeid_of(I));
  865. for i in 0..<N {
  866. res[i] = T(i*i);
  867. }
  868. return;
  869. }
  870. T :: int;
  871. array := foo(4, T);
  872. for v, i in array {
  873. assert(v == T(i*i));
  874. }
  875. // Matrix multiplication
  876. mul :: proc(a: [$M][$N]$T, b: [N][$P]T) -> (c: [M][P]T) {
  877. for i in 0..<M {
  878. for j in 0..<P {
  879. for k in 0..<N {
  880. c[i][j] += a[i][k] * b[k][j];
  881. }
  882. }
  883. }
  884. return;
  885. }
  886. x := [2][3]f32{
  887. {1, 2, 3},
  888. {3, 2, 1},
  889. };
  890. y := [3][2]f32{
  891. {0, 8},
  892. {6, 2},
  893. {8, 4},
  894. };
  895. z := mul(x, y);
  896. assert(z == {{36, 24}, {20, 32}});
  897. }
  898. }
  899. prefix_table := [?]string{
  900. "White",
  901. "Red",
  902. "Green",
  903. "Blue",
  904. "Octarine",
  905. "Black",
  906. };
  907. threading_example :: proc() {
  908. if ODIN_OS == "darwin" {
  909. // TODO: Fix threads on darwin/macOS
  910. return;
  911. }
  912. fmt.println("\n# threading_example");
  913. { // Basic Threads
  914. fmt.println("\n## Basic Threads");
  915. worker_proc :: proc(t: ^thread.Thread) {
  916. for iteration in 1..=5 {
  917. fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
  918. fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
  919. time.sleep(1 * time.Millisecond);
  920. }
  921. }
  922. threads := make([dynamic]^thread.Thread, 0, len(prefix_table));
  923. defer delete(threads);
  924. for in prefix_table {
  925. if t := thread.create(worker_proc); t != nil {
  926. t.init_context = context;
  927. t.user_index = len(threads);
  928. append(&threads, t);
  929. thread.start(t);
  930. }
  931. }
  932. for len(threads) > 0 {
  933. for i := 0; i < len(threads); /**/ {
  934. if t := threads[i]; thread.is_done(t) {
  935. fmt.printf("Thread %d is done\n", t.user_index);
  936. thread.destroy(t);
  937. ordered_remove(&threads, i);
  938. } else {
  939. i += 1;
  940. }
  941. }
  942. }
  943. }
  944. { // Thread Pool
  945. fmt.println("\n## Thread Pool");
  946. task_proc :: proc(t: ^thread.Task) {
  947. index := t.user_index % len(prefix_table);
  948. for iteration in 1..=5 {
  949. fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration);
  950. fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration);
  951. time.sleep(1 * time.Millisecond);
  952. }
  953. }
  954. pool: thread.Pool;
  955. thread.pool_init(pool=&pool, thread_count=3);
  956. defer thread.pool_destroy(&pool);
  957. for i in 0..<30 {
  958. thread.pool_add_task(pool=&pool, procedure=task_proc, data=nil, user_index=i);
  959. }
  960. thread.pool_start(&pool);
  961. thread.pool_wait_and_process(&pool);
  962. }
  963. }
  964. array_programming :: proc() {
  965. fmt.println("\n# array programming");
  966. {
  967. a := [3]f32{1, 2, 3};
  968. b := [3]f32{5, 6, 7};
  969. c := a * b;
  970. d := a + b;
  971. e := 1 + (c - d) / 2;
  972. fmt.printf("%.1f\n", e); // [0.5, 3.0, 6.5]
  973. }
  974. {
  975. a := [3]f32{1, 2, 3};
  976. b := swizzle(a, 2, 1, 0);
  977. assert(b == [3]f32{3, 2, 1});
  978. c := swizzle(a, 0, 0);
  979. assert(c == [2]f32{1, 1});
  980. assert(c == 1);
  981. }
  982. {
  983. Vector3 :: distinct [3]f32;
  984. a := Vector3{1, 2, 3};
  985. b := Vector3{5, 6, 7};
  986. c := (a * b)/2 + 1;
  987. d := c.x + c.y + c.z;
  988. fmt.printf("%.1f\n", d); // 22.0
  989. cross :: proc(a, b: Vector3) -> Vector3 {
  990. i := swizzle(a, 1, 2, 0) * swizzle(b, 2, 0, 1);
  991. j := swizzle(a, 2, 0, 1) * swizzle(b, 1, 2, 0);
  992. return i - j;
  993. }
  994. cross_shorter :: proc(a, b: Vector3) -> Vector3 {
  995. i := a.yzx * b.zxy;
  996. j := a.zxy * b.yzx;
  997. return i - j;
  998. }
  999. blah :: proc(a: Vector3) -> f32 {
  1000. return a.x + a.y + a.z;
  1001. }
  1002. x := cross(a, b);
  1003. fmt.println(x);
  1004. fmt.println(blah(x));
  1005. }
  1006. }
  1007. map_type :: proc() {
  1008. fmt.println("\n# map type");
  1009. m := make(map[string]int);
  1010. defer delete(m);
  1011. m["Bob"] = 2;
  1012. m["Ted"] = 5;
  1013. fmt.println(m["Bob"]);
  1014. delete_key(&m, "Ted");
  1015. // If an element of a key does not exist, the zero value of the
  1016. // element will be returned. To check to see if an element exists
  1017. // can be done in two ways:
  1018. elem, ok := m["Bob"];
  1019. exists := "Bob" in m;
  1020. _, _ = elem, ok;
  1021. _ = exists;
  1022. }
  1023. implicit_selector_expression :: proc() {
  1024. fmt.println("\n# implicit selector expression");
  1025. Foo :: enum {A, B, C};
  1026. f: Foo;
  1027. f = Foo.A;
  1028. f = .A;
  1029. BAR :: bit_set[Foo]{.B, .C};
  1030. switch f {
  1031. case .A:
  1032. fmt.println("HITHER");
  1033. case .B:
  1034. fmt.println("NEVER");
  1035. case .C:
  1036. fmt.println("FOREVER");
  1037. }
  1038. my_map := make(map[Foo]int);
  1039. defer delete(my_map);
  1040. my_map[.A] = 123;
  1041. my_map[Foo.B] = 345;
  1042. fmt.println(my_map[.A] + my_map[Foo.B] + my_map[.C]);
  1043. }
  1044. partial_switch :: proc() {
  1045. fmt.println("\n# partial_switch");
  1046. { // enum
  1047. Foo :: enum {
  1048. A,
  1049. B,
  1050. C,
  1051. D,
  1052. };
  1053. f := Foo.A;
  1054. switch f {
  1055. case .A: fmt.println("A");
  1056. case .B: fmt.println("B");
  1057. case .C: fmt.println("C");
  1058. case .D: fmt.println("D");
  1059. case: fmt.println("?");
  1060. }
  1061. #partial switch f {
  1062. case .A: fmt.println("A");
  1063. case .D: fmt.println("D");
  1064. }
  1065. }
  1066. { // union
  1067. Foo :: union {int, bool};
  1068. f: Foo = 123;
  1069. switch in f {
  1070. case int: fmt.println("int");
  1071. case bool: fmt.println("bool");
  1072. case:
  1073. }
  1074. #partial switch in f {
  1075. case bool: fmt.println("bool");
  1076. }
  1077. }
  1078. }
  1079. cstring_example :: proc() {
  1080. fmt.println("\n# cstring_example");
  1081. W :: "Hellope";
  1082. X :: cstring(W);
  1083. Y :: string(X);
  1084. w := W;
  1085. _ = w;
  1086. x: cstring = X;
  1087. y: string = Y;
  1088. z := string(x);
  1089. fmt.println(x, y, z);
  1090. fmt.println(len(x), len(y), len(z));
  1091. fmt.println(len(W), len(X), len(Y));
  1092. // IMPORTANT NOTE for cstring variables
  1093. // len(cstring) is O(N)
  1094. // cast(string)cstring is O(N)
  1095. }
  1096. bit_set_type :: proc() {
  1097. fmt.println("\n# bit_set type");
  1098. {
  1099. Day :: enum {
  1100. Sunday,
  1101. Monday,
  1102. Tuesday,
  1103. Wednesday,
  1104. Thursday,
  1105. Friday,
  1106. Saturday,
  1107. };
  1108. Days :: distinct bit_set[Day];
  1109. WEEKEND :: Days{.Sunday, .Saturday};
  1110. d: Days;
  1111. d = {.Sunday, .Monday};
  1112. e := d + WEEKEND;
  1113. e += {.Monday};
  1114. fmt.println(d, e);
  1115. ok := .Saturday in e; // `in` is only allowed for `map` and `bit_set` types
  1116. fmt.println(ok);
  1117. if .Saturday in e {
  1118. fmt.println("Saturday in", e);
  1119. }
  1120. X :: .Saturday in WEEKEND; // Constant evaluation
  1121. fmt.println(X);
  1122. fmt.println("Cardinality:", card(e));
  1123. }
  1124. {
  1125. x: bit_set['A'..='Z'];
  1126. #assert(size_of(x) == size_of(u32));
  1127. y: bit_set[0..=8; u16];
  1128. fmt.println(typeid_of(type_of(x))); // bit_set[A..=Z]
  1129. fmt.println(typeid_of(type_of(y))); // bit_set[0..=8; u16]
  1130. x += {'F'};
  1131. assert('F' in x);
  1132. x -= {'F'};
  1133. assert('F' not_in x);
  1134. y += {1, 4, 2};
  1135. assert(2 in y);
  1136. }
  1137. {
  1138. Letters :: bit_set['A'..='Z'];
  1139. a := Letters{'A', 'B'};
  1140. b := Letters{'A', 'B', 'C', 'D', 'F'};
  1141. c := Letters{'A', 'B'};
  1142. assert(a <= b); // 'a' is a subset of 'b'
  1143. assert(b >= a); // 'b' is a superset of 'a'
  1144. assert(a < b); // 'a' is a strict subset of 'b'
  1145. assert(b > a); // 'b' is a strict superset of 'a'
  1146. assert(!(a < c)); // 'a' is a not strict subset of 'c'
  1147. assert(!(c > a)); // 'c' is a not strict superset of 'a'
  1148. }
  1149. }
  1150. deferred_procedure_associations :: proc() {
  1151. fmt.println("\n# deferred procedure associations");
  1152. @(deferred_out=closure)
  1153. open :: proc(s: string) -> bool {
  1154. fmt.println(s);
  1155. return true;
  1156. }
  1157. closure :: proc(ok: bool) {
  1158. fmt.println("Goodbye?", ok);
  1159. }
  1160. if open("Welcome") {
  1161. fmt.println("Something in the middle, mate.");
  1162. }
  1163. }
  1164. reflection :: proc() {
  1165. fmt.println("\n# reflection");
  1166. Foo :: struct {
  1167. x: int `tag1`,
  1168. y: string `json:"y_field"`,
  1169. z: bool, // no tag
  1170. };
  1171. id := typeid_of(Foo);
  1172. names := reflect.struct_field_names(id);
  1173. types := reflect.struct_field_types(id);
  1174. tags := reflect.struct_field_tags(id);
  1175. assert(len(names) == len(types) && len(names) == len(tags));
  1176. fmt.println("Foo :: struct {");
  1177. for tag, i in tags {
  1178. name, type := names[i], types[i];
  1179. if tag != "" {
  1180. fmt.printf("\t%s: %T `%s`,\n", name, type, tag);
  1181. } else {
  1182. fmt.printf("\t%s: %T,\n", name, type);
  1183. }
  1184. }
  1185. fmt.println("}");
  1186. for tag, i in tags {
  1187. if val, ok := reflect.struct_tag_lookup(tag, "json"); ok {
  1188. fmt.printf("json: %s -> %s\n", names[i], val);
  1189. }
  1190. }
  1191. }
  1192. quaternions :: proc() {
  1193. // Not just an April Fool's Joke any more, but a fully working thing!
  1194. fmt.println("\n# quaternions");
  1195. { // Quaternion operations
  1196. q := 1 + 2i + 3j + 4k;
  1197. r := quaternion(5, 6, 7, 8);
  1198. t := q * r;
  1199. fmt.printf("(%v) * (%v) = %v\n", q, r, t);
  1200. v := q / r;
  1201. fmt.printf("(%v) / (%v) = %v\n", q, r, v);
  1202. u := q + r;
  1203. fmt.printf("(%v) + (%v) = %v\n", q, r, u);
  1204. s := q - r;
  1205. fmt.printf("(%v) - (%v) = %v\n", q, r, s);
  1206. }
  1207. { // The quaternion types
  1208. q128: quaternion128; // 4xf32
  1209. q256: quaternion256; // 4xf64
  1210. q128 = quaternion(1, 0, 0, 0);
  1211. q256 = 1; // quaternion(1, 0, 0, 0);
  1212. }
  1213. { // Built-in procedures
  1214. q := 1 + 2i + 3j + 4k;
  1215. fmt.println("q =", q);
  1216. fmt.println("real(q) =", real(q));
  1217. fmt.println("imag(q) =", imag(q));
  1218. fmt.println("jmag(q) =", jmag(q));
  1219. fmt.println("kmag(q) =", kmag(q));
  1220. fmt.println("conj(q) =", conj(q));
  1221. fmt.println("abs(q) =", abs(q));
  1222. }
  1223. { // Conversion of a complex type to a quaternion type
  1224. c := 1 + 2i;
  1225. q := quaternion256(c);
  1226. fmt.println(c);
  1227. fmt.println(q);
  1228. }
  1229. { // Memory layout of Quaternions
  1230. q := 1 + 2i + 3j + 4k;
  1231. a := transmute([4]f64)q;
  1232. fmt.println("Quaternion memory layout: xyzw/(ijkr)");
  1233. fmt.println(q); // 1.000+2.000i+3.000j+4.000k
  1234. fmt.println(a); // [2.000, 3.000, 4.000, 1.000]
  1235. }
  1236. }
  1237. unroll_for_statement :: proc() {
  1238. fmt.println("\n#'#unroll for' statements");
  1239. // '#unroll for' works the same as if the 'inline' prefix did not
  1240. // exist but these ranged loops are explicitly unrolled which can
  1241. // be very very useful for certain optimizations
  1242. fmt.println("Ranges");
  1243. #unroll for x, i in 1..<4 {
  1244. fmt.println(x, i);
  1245. }
  1246. fmt.println("Strings");
  1247. #unroll for r, i in "Hello, 世界" {
  1248. fmt.println(r, i);
  1249. }
  1250. fmt.println("Arrays");
  1251. #unroll for elem, idx in ([4]int{1, 4, 9, 16}) {
  1252. fmt.println(elem, idx);
  1253. }
  1254. Foo_Enum :: enum {
  1255. A = 1,
  1256. B,
  1257. C = 6,
  1258. D,
  1259. };
  1260. fmt.println("Enum types");
  1261. #unroll for elem, idx in Foo_Enum {
  1262. fmt.println(elem, idx);
  1263. }
  1264. }
  1265. where_clauses :: proc() {
  1266. fmt.println("\n#procedure 'where' clauses");
  1267. { // Sanity checks
  1268. simple_sanity_check :: proc(x: [2]int)
  1269. where len(x) > 1,
  1270. type_of(x) == [2]int {
  1271. fmt.println(x);
  1272. }
  1273. }
  1274. { // Parametric polymorphism checks
  1275. cross_2d :: proc(a, b: $T/[2]$E) -> E
  1276. where intrinsics.type_is_numeric(E) {
  1277. return a.x*b.y - a.y*b.x;
  1278. }
  1279. cross_3d :: proc(a, b: $T/[3]$E) -> T
  1280. where intrinsics.type_is_numeric(E) {
  1281. x := a.y*b.z - a.z*b.y;
  1282. y := a.z*b.x - a.x*b.z;
  1283. z := a.x*b.y - a.y*b.z;
  1284. return T{x, y, z};
  1285. }
  1286. a := [2]int{1, 2};
  1287. b := [2]int{5, -3};
  1288. fmt.println(cross_2d(a, b));
  1289. x := [3]f32{1, 4, 9};
  1290. y := [3]f32{-5, 0, 3};
  1291. fmt.println(cross_3d(x, y));
  1292. // Failure case
  1293. // i := [2]bool{true, false};
  1294. // j := [2]bool{false, true};
  1295. // fmt.println(cross_2d(i, j));
  1296. }
  1297. { // Procedure groups usage
  1298. foo :: proc(x: [$N]int) -> bool
  1299. where N > 2 {
  1300. fmt.println(#procedure, "was called with the parameter", x);
  1301. return true;
  1302. }
  1303. bar :: proc(x: [$N]int) -> bool
  1304. where 0 < N,
  1305. N <= 2 {
  1306. fmt.println(#procedure, "was called with the parameter", x);
  1307. return false;
  1308. }
  1309. baz :: proc{foo, bar};
  1310. x := [3]int{1, 2, 3};
  1311. y := [2]int{4, 9};
  1312. ok_x := baz(x);
  1313. ok_y := baz(y);
  1314. assert(ok_x == true);
  1315. assert(ok_y == false);
  1316. }
  1317. { // Record types
  1318. Foo :: struct($T: typeid, $N: int)
  1319. where intrinsics.type_is_integer(T),
  1320. N > 2 {
  1321. x: [N]T,
  1322. y: [N-2]T,
  1323. };
  1324. T :: i32;
  1325. N :: 5;
  1326. f: Foo(T, N);
  1327. #assert(size_of(f) == (N+N-2)*size_of(T));
  1328. }
  1329. }
  1330. when ODIN_OS == "windows" {
  1331. foreign import kernel32 "system:kernel32.lib"
  1332. }
  1333. foreign_system :: proc() {
  1334. fmt.println("\n#foreign system");
  1335. when ODIN_OS == "windows" {
  1336. // It is sometimes necessarily to interface with foreign code,
  1337. // such as a C library. In Odin, this is achieved through the
  1338. // foreign system. You can “import” a library into the code
  1339. // using the same semantics as a normal import declaration.
  1340. // This foreign import declaration will create a
  1341. // “foreign import name” which can then be used to associate
  1342. // entities within a foreign block.
  1343. foreign kernel32 {
  1344. ExitProcess :: proc "stdcall" (exit_code: u32) ---
  1345. }
  1346. // Foreign procedure declarations have the cdecl/c calling
  1347. // convention by default unless specified otherwise. Due to
  1348. // foreign procedures do not have a body declared within this
  1349. // code, you need append the --- symbol to the end to distinguish
  1350. // it as a procedure literal without a body and not a procedure type.
  1351. // The attributes system can be used to change specific properties
  1352. // of entities declared within a block:
  1353. @(default_calling_convention = "std")
  1354. foreign kernel32 {
  1355. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---
  1356. }
  1357. // Example using the link_prefix attribute
  1358. @(default_calling_convention = "std")
  1359. @(link_prefix = "Get")
  1360. foreign kernel32 {
  1361. LastError :: proc() -> i32 ---
  1362. }
  1363. }
  1364. }
  1365. ranged_fields_for_array_compound_literals :: proc() {
  1366. fmt.println("\n#ranged fields for array compound literals");
  1367. { // Normal Array Literal
  1368. foo := [?]int{1, 4, 9, 16};
  1369. fmt.println(foo);
  1370. }
  1371. { // Indexed
  1372. foo := [?]int{
  1373. 3 = 16,
  1374. 1 = 4,
  1375. 2 = 9,
  1376. 0 = 1,
  1377. };
  1378. fmt.println(foo);
  1379. }
  1380. { // Ranges
  1381. i := 2;
  1382. foo := [?]int {
  1383. 0 = 123,
  1384. 5..=9 = 54,
  1385. 10..<16 = i*3 + (i-1)*2,
  1386. };
  1387. #assert(len(foo) == 16);
  1388. fmt.println(foo); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1389. }
  1390. { // Slice and Dynamic Array support
  1391. i := 2;
  1392. foo_slice := []int {
  1393. 0 = 123,
  1394. 5..=9 = 54,
  1395. 10..<16 = i*3 + (i-1)*2,
  1396. };
  1397. assert(len(foo_slice) == 16);
  1398. fmt.println(foo_slice); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1399. foo_dynamic_array := [dynamic]int {
  1400. 0 = 123,
  1401. 5..=9 = 54,
  1402. 10..<16 = i*3 + (i-1)*2,
  1403. };
  1404. assert(len(foo_dynamic_array) == 16);
  1405. fmt.println(foo_dynamic_array); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1406. }
  1407. }
  1408. deprecated_attribute :: proc() {
  1409. @(deprecated="Use foo_v2 instead")
  1410. foo_v1 :: proc(x: int) {
  1411. fmt.println("foo_v1");
  1412. }
  1413. foo_v2 :: proc(x: int) {
  1414. fmt.println("foo_v2");
  1415. }
  1416. // NOTE: Uncomment to see the warning messages
  1417. // foo_v1(1);
  1418. }
  1419. range_statements_with_multiple_return_values :: proc() {
  1420. // IMPORTANT NOTE(bill, 2019-11-02): This feature is subject to be changed/removed
  1421. fmt.println("\n#range statements with multiple return values");
  1422. My_Iterator :: struct {
  1423. index: int,
  1424. data: []i32,
  1425. };
  1426. make_my_iterator :: proc(data: []i32) -> My_Iterator {
  1427. return My_Iterator{data = data};
  1428. }
  1429. my_iterator :: proc(it: ^My_Iterator) -> (val: i32, idx: int, cond: bool) {
  1430. if cond = it.index < len(it.data); cond {
  1431. val = it.data[it.index];
  1432. idx = it.index;
  1433. it.index += 1;
  1434. }
  1435. return;
  1436. }
  1437. data := make([]i32, 6);
  1438. for _, i in data {
  1439. data[i] = i32(i*i);
  1440. }
  1441. {
  1442. it := make_my_iterator(data);
  1443. for val in my_iterator(&it) {
  1444. fmt.println(val);
  1445. }
  1446. }
  1447. {
  1448. it := make_my_iterator(data);
  1449. for val, idx in my_iterator(&it) {
  1450. fmt.println(val, idx);
  1451. }
  1452. }
  1453. {
  1454. it := make_my_iterator(data);
  1455. for {
  1456. val, _, cond := my_iterator(&it);
  1457. if !cond {
  1458. break;
  1459. }
  1460. fmt.println(val);
  1461. }
  1462. }
  1463. }
  1464. soa_struct_layout :: proc() {
  1465. fmt.println("\n#SOA Struct Layout");
  1466. {
  1467. Vector3 :: struct {x, y, z: f32};
  1468. N :: 2;
  1469. v_aos: [N]Vector3;
  1470. v_aos[0].x = 1;
  1471. v_aos[0].y = 4;
  1472. v_aos[0].z = 9;
  1473. fmt.println(len(v_aos));
  1474. fmt.println(v_aos[0]);
  1475. fmt.println(v_aos[0].x);
  1476. fmt.println(&v_aos[0].x);
  1477. v_aos[1] = {0, 3, 4};
  1478. v_aos[1].x = 2;
  1479. fmt.println(v_aos[1]);
  1480. fmt.println(v_aos);
  1481. v_soa: #soa[N]Vector3;
  1482. v_soa[0].x = 1;
  1483. v_soa[0].y = 4;
  1484. v_soa[0].z = 9;
  1485. // Same syntax as AOS and treat as if it was an array
  1486. fmt.println(len(v_soa));
  1487. fmt.println(v_soa[0]);
  1488. fmt.println(v_soa[0].x);
  1489. fmt.println(&v_soa[0].x);
  1490. v_soa[1] = {0, 3, 4};
  1491. v_soa[1].x = 2;
  1492. fmt.println(v_soa[1]);
  1493. // Can use SOA syntax if necessary
  1494. v_soa.x[0] = 1;
  1495. v_soa.y[0] = 4;
  1496. v_soa.z[0] = 9;
  1497. fmt.println(v_soa.x[0]);
  1498. // Same pointer addresses with both syntaxes
  1499. assert(&v_soa[0].x == &v_soa.x[0]);
  1500. // Same fmt printing
  1501. fmt.println(v_aos);
  1502. fmt.println(v_soa);
  1503. }
  1504. {
  1505. // Works with arrays of length <= 4 which have the implicit fields xyzw/rgba
  1506. Vector3 :: distinct [3]f32;
  1507. N :: 2;
  1508. v_aos: [N]Vector3;
  1509. v_aos[0].x = 1;
  1510. v_aos[0].y = 4;
  1511. v_aos[0].z = 9;
  1512. v_soa: #soa[N]Vector3;
  1513. v_soa[0].x = 1;
  1514. v_soa[0].y = 4;
  1515. v_soa[0].z = 9;
  1516. }
  1517. {
  1518. // SOA Slices
  1519. // Vector3 :: struct {x, y, z: f32};
  1520. Vector3 :: struct {x: i8, y: i16, z: f32};
  1521. N :: 3;
  1522. v: #soa[N]Vector3;
  1523. v[0].x = 1;
  1524. v[0].y = 4;
  1525. v[0].z = 9;
  1526. s: #soa[]Vector3;
  1527. s = v[:];
  1528. assert(len(s) == N);
  1529. fmt.println(s);
  1530. fmt.println(s[0].x);
  1531. a := s[1:2];
  1532. assert(len(a) == 1);
  1533. fmt.println(a);
  1534. d: #soa[dynamic]Vector3;
  1535. append_soa(&d, Vector3{1, 2, 3}, Vector3{4, 5, 9}, Vector3{-4, -4, 3});
  1536. fmt.println(d);
  1537. fmt.println(len(d));
  1538. fmt.println(cap(d));
  1539. fmt.println(d[:]);
  1540. }
  1541. { // soa_zip and soa_unzip
  1542. fmt.println("\nsoa_zip and soa_unzip");
  1543. x := []i32{1, 3, 9};
  1544. y := []f32{2, 4, 16};
  1545. z := []b32{true, false, true};
  1546. // produce an #soa slice the normal slices passed
  1547. s := soa_zip(a=x, b=y, c=z);
  1548. // iterate over the #soa slice
  1549. for v, i in s {
  1550. fmt.println(v, i); // exactly the same as s[i]
  1551. // NOTE: 'v' is NOT a temporary value but has a specialized addressing mode
  1552. // which means that when accessing v.a etc, it does the correct transformation
  1553. // internally:
  1554. // s[i].a === s.a[i]
  1555. fmt.println(v.a, v.b, v.c);
  1556. }
  1557. // Recover the slices from the #soa slice
  1558. a, b, c := soa_unzip(s);
  1559. fmt.println(a, b, c);
  1560. }
  1561. }
  1562. constant_literal_expressions :: proc() {
  1563. fmt.println("\n#constant literal expressions");
  1564. Bar :: struct {x, y: f32};
  1565. Foo :: struct {a, b: int, using c: Bar};
  1566. FOO_CONST :: Foo{b = 2, a = 1, c = {3, 4}};
  1567. fmt.println(FOO_CONST.a);
  1568. fmt.println(FOO_CONST.b);
  1569. fmt.println(FOO_CONST.c);
  1570. fmt.println(FOO_CONST.c.x);
  1571. fmt.println(FOO_CONST.c.y);
  1572. fmt.println(FOO_CONST.x); // using works as expected
  1573. fmt.println(FOO_CONST.y);
  1574. fmt.println("-------");
  1575. ARRAY_CONST :: [3]int{1 = 4, 2 = 9, 0 = 1};
  1576. fmt.println(ARRAY_CONST[0]);
  1577. fmt.println(ARRAY_CONST[1]);
  1578. fmt.println(ARRAY_CONST[2]);
  1579. fmt.println("-------");
  1580. FOO_ARRAY_DEFAULTS :: [3]Foo{{}, {}, {}};
  1581. fmt.println(FOO_ARRAY_DEFAULTS[2].x);
  1582. fmt.println("-------");
  1583. Baz :: enum{A=5, B, C, D};
  1584. ENUM_ARRAY_CONST :: [Baz]int{.A ..= .C = 1, .D = 16};
  1585. fmt.println(ENUM_ARRAY_CONST[.A]);
  1586. fmt.println(ENUM_ARRAY_CONST[.B]);
  1587. fmt.println(ENUM_ARRAY_CONST[.C]);
  1588. fmt.println(ENUM_ARRAY_CONST[.D]);
  1589. fmt.println("-------");
  1590. Partial_Baz :: enum{A=5, B, C, D=16};
  1591. #assert(len(Partial_Baz) < len(#partial [Partial_Baz]int));
  1592. PARTIAL_ENUM_ARRAY_CONST :: #partial [Partial_Baz]int{.A ..= .C = 1, .D = 16};
  1593. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.A]);
  1594. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.B]);
  1595. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.C]);
  1596. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.D]);
  1597. fmt.println("-------");
  1598. STRING_CONST :: "Hellope!";
  1599. fmt.println(STRING_CONST[0]);
  1600. fmt.println(STRING_CONST[2]);
  1601. fmt.println(STRING_CONST[3]);
  1602. fmt.println(STRING_CONST[0:5]);
  1603. fmt.println(STRING_CONST[3:][:4]);
  1604. }
  1605. union_maybe :: proc() {
  1606. fmt.println("\n#union #maybe");
  1607. // NOTE: This is already built-in, and this is just a reimplementation to explain the behaviour
  1608. Maybe :: union($T: typeid) #maybe {T};
  1609. i: Maybe(u8);
  1610. p: Maybe(^u8); // No tag is stored for pointers, nil is the sentinel value
  1611. #assert(size_of(i) == size_of(u8) + size_of(u8));
  1612. #assert(size_of(p) == size_of(^u8));
  1613. i = 123;
  1614. x := i.?;
  1615. y, y_ok := p.?;
  1616. p = &x;
  1617. z, z_ok := p.?;
  1618. fmt.println(i, p);
  1619. fmt.println(x, &x);
  1620. fmt.println(y, y_ok);
  1621. fmt.println(z, z_ok);
  1622. }
  1623. dummy_procedure :: proc() {
  1624. fmt.println("dummy_procedure");
  1625. }
  1626. explicit_context_definition :: proc "c" () {
  1627. // Try commenting the following statement out below
  1628. context = runtime.default_context();
  1629. fmt.println("\n#explicit context definition");
  1630. dummy_procedure();
  1631. }
  1632. relative_data_types :: proc() {
  1633. fmt.println("\n#relative data types");
  1634. x: int = 123;
  1635. ptr: #relative(i16) ^int;
  1636. ptr = &x;
  1637. fmt.println(ptr^);
  1638. arr := [3]int{1, 2, 3};
  1639. s := arr[:];
  1640. rel_slice: #relative(i16) []int;
  1641. rel_slice = s;
  1642. fmt.println(rel_slice);
  1643. fmt.println(rel_slice[:]);
  1644. fmt.println(rel_slice[1]);
  1645. }
  1646. or_else_operator :: proc() {
  1647. fmt.println("\n#'or_else'");
  1648. // IMPORTANT NOTE: 'or_else' is an experimental feature and subject to change/removal
  1649. {
  1650. m: map[string]int;
  1651. i: int;
  1652. ok: bool;
  1653. if i, ok = m["hellope"]; !ok {
  1654. i = 123;
  1655. }
  1656. // The above can be mapped to 'or_else'
  1657. i = m["hellope"] or_else 123;
  1658. assert(i == 123);
  1659. }
  1660. {
  1661. // 'or_else' can be used with type assertions too, as they
  1662. // have optional ok semantics
  1663. v: union{int, f64};
  1664. i: int;
  1665. i = v.(int) or_else 123;
  1666. i = v.? or_else 123; // Type inference magic
  1667. assert(i == 123);
  1668. m: Maybe(int);
  1669. i = m.? or_else 456;
  1670. assert(i == 456);
  1671. }
  1672. }
  1673. or_return_operator :: proc() {
  1674. fmt.println("\n#'or_return'");
  1675. // IMPORTANT NOTE: 'or_return' is an experimental feature and subject to change/removal
  1676. //
  1677. // The concept of 'or_return' will work by popping off the end value in a multiple
  1678. // valued expression and checking whether it was not 'nil' or 'false', and if so,
  1679. // set the end return value to value if possible. If the procedure only has one
  1680. // return value, it will do a simple return. If the procedure had multiple return
  1681. // values, 'or_return' will require that all parameters be named so that the end
  1682. // value could be assigned to by name and then an empty return could be called.
  1683. Error :: enum {
  1684. None,
  1685. Something_Bad,
  1686. Something_Worse,
  1687. The_Worst,
  1688. Your_Mum,
  1689. };
  1690. caller_1 :: proc() -> Error {
  1691. return .None;
  1692. }
  1693. caller_2 :: proc() -> (int, Error) {
  1694. return 123, .None;
  1695. }
  1696. caller_3 :: proc() -> (int, int, Error) {
  1697. return 123, 345, .None;
  1698. }
  1699. foo_1 :: proc() -> Error {
  1700. // This can be a common idiom in many code bases
  1701. n0, err := caller_2();
  1702. if err != nil {
  1703. return err;
  1704. }
  1705. // The above idiom can be transformed into the following
  1706. n1 := caller_2() or_return;
  1707. // And if the expression is 1-valued, it can be used like this
  1708. caller_1() or_return;
  1709. // which is functionally equivalent to
  1710. if err1 := caller_1(); err1 != nil {
  1711. return err1;
  1712. }
  1713. // Multiple return values still work with 'or_return' as it only
  1714. // pops off the end value in the multi-valued expression
  1715. n0, n1 = caller_3() or_return;
  1716. return .None;
  1717. }
  1718. foo_2 :: proc() -> (n: int, err: Error) {
  1719. // It is more common that your procedure turns multiple values
  1720. // If 'or_return' is used within a procedure multiple parameters (2+),
  1721. // then all the parameters must be named so that the remaining parameters
  1722. // so that a bare 'return' statement can be used
  1723. // This can be a common idiom in many code bases
  1724. x: int;
  1725. x, err = caller_2();
  1726. if err != nil {
  1727. return;
  1728. }
  1729. // The above idiom can be transformed into the following
  1730. y := caller_2() or_return;
  1731. _ = y;
  1732. // And if the expression is 1-valued, it can be used like this
  1733. caller_1() or_return;
  1734. // which is functionally equivalent to
  1735. if err1 := caller_1(); err1 != nil {
  1736. err = err1;
  1737. return;
  1738. }
  1739. // If using a non-bare 'return' statement is required, setting the return values
  1740. // using the normal idiom is a better choice and clearer to read.
  1741. if z, zerr := caller_2(); zerr != nil {
  1742. return -345 * z, zerr;
  1743. }
  1744. // If the other return values need to be set depending on what the end value is,
  1745. // the 'defer if' idiom is can be used
  1746. defer if err != nil {
  1747. n = -1;
  1748. }
  1749. n = 123;
  1750. return;
  1751. }
  1752. foo_1();
  1753. foo_2();
  1754. }
  1755. main :: proc() {
  1756. when true {
  1757. the_basics();
  1758. control_flow();
  1759. named_proc_return_parameters();
  1760. explicit_procedure_overloading();
  1761. struct_type();
  1762. union_type();
  1763. using_statement();
  1764. implicit_context_system();
  1765. parametric_polymorphism();
  1766. array_programming();
  1767. map_type();
  1768. implicit_selector_expression();
  1769. partial_switch();
  1770. cstring_example();
  1771. bit_set_type();
  1772. deferred_procedure_associations();
  1773. reflection();
  1774. quaternions();
  1775. unroll_for_statement();
  1776. where_clauses();
  1777. foreign_system();
  1778. ranged_fields_for_array_compound_literals();
  1779. deprecated_attribute();
  1780. range_statements_with_multiple_return_values();
  1781. threading_example();
  1782. soa_struct_layout();
  1783. constant_literal_expressions();
  1784. union_maybe();
  1785. explicit_context_definition();
  1786. relative_data_types();
  1787. or_else_operator();
  1788. or_return_operator();
  1789. }
  1790. }