fn get_number_input(question string) {<br> println(question)<br> input = read_line()<br> // try to parse to an int, then a float.<br> in = match input {<br> input.parse_int() input<br> input.parse_float() input<br> }<br> // 'in' has type int/float/[] because of the match statement<br> switch! in {<br> int/float in<br> // replace [] with an appropriate error before returning<br> [] Err: "input was not a number."<br> }<br> // return type is int/float/Err(string)<br>}<br><br>answer = get_number_input("What is your favorite number?")<br><br>answer.debug() // type: int/float/Err(string)<br>// switch can be used to branch based on a variables type.<br>// switch! indicates that every possible type must be handled.<br>switch! answer {<br> int {<br> println("Entered an integer")<br> answer.debug() // type: int<br> }<br> float {<br> println("Entered a decimal number")<br> answer.debug() // type: float<br> }<br> Err(string) println("Input was not a number!")<br>}<br><br>// wait one second<br>sleep(1)<br><br><br>// function that returns an anonymous function (function object).<br>// anonymous functions can be used as iterators in for-loops.<br>fn square_numbers() {<br> i = 0<br> () {<br> i = i + 1<br> i * i<br> }<br>}<br><br>for num square_numbers() {<br> println(num.to_string())<br> // once num is greater than 60, the loop stops.<br> num.gt(50)<br>}<br></code></pre>
It keeps track of which types a variable could have
and constructs a type with that information.
<br>
For example, <code>int/float</code> can represent a number - int or float.
Optional types can be <code>[]/[t]</code> - either nothing or one value (tuple with length 0 or 1).
Mers doesn't have null, it just has the empty tuple <code>[]</code>.
</div>
<h3>No exceptions, no crashes</h3>
<div>
Errors in mers are passed as values.
Because of the type system, you are forced to handle them explicitly.
Mers will not crash in unexpected places, because the only way to crash
it is by using one of the assume*() functions (similar to unwrap()s).
</div>
</section>
</section>
<hr>
<h3>HTML preprocessor to help build this document written in mers:</h3>
<sectionclass="container">
<preclass="container2_left"><code>
<!DOCTYPE html><br># This document will be processed by build.mers.<br># Lines starting with hashtags are comments and will be ignored.<br># Lines starting with dollar-signs insert special text.<br># To escape this, put a space before the hashtag or dollar sign.<br><head><br><meta charset=“UTF-8”><br><link rel="stylesheet" href="external.css"><br><title>Mark :: mers</title><br></head><br><body><br><h1>Mers</h1><br><section class="container"><br><section class="container_left2 code-border"><br><pre><code class="mers-code-snippet"><br>$welcome_script<br></code></pre><br></section><br><section class="container_right"><br><image<br> alt="some picture related to mers (todo)"<br> src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////<br> width="100%" height="100%"<br>><br><h3>Mers types</h3><br><div><br> Mers uses a multiple-types system.<br> It keeps track of which types a variable could have<br> and constructs a type with that information.<br><br><br> For example, <code>int/float</code> can represent a number - int or<br> Optional types can be <code>[]/[t]</code> - either nothing or one v<br> Mers doesn't have null, it just has the empty tuple <code>[]</code><br></div><br><h3>No exceptions, no crashes</h3><br><div><br> Errors in mers are passed as values.<br> Because of the type system, you are forced to handle them explicitl<br> Mers will not crash in unexpected places, because the only way to c<br> it is by using one of the assume*() functions (similar to unwrap()s<br></div><br></section><br></section><br><hr><br><h3>HTML preprocessor to help build this document written in mers:</h3><br><section class="container"><br><pre class="container2_left"><code><br>$index.html<br></code></pre><br><pre class="container2_right"><code class="mers-code-snippet"><br>$build_script<br></code></pre><br></section><br></body><br><br></code></pre>
#!/usr/bin/env mers<br><br>// helper functions<br><br>fn read_string(path string) {<br> bytes_to_string(fs_read(path).assume_no_enum()).assume_no_enum()<br>}<br>fn code_to_html(code string code_width_limit_chars int) {<br> out = ""<br> for line code.regex(".*").assume_no_enum() {<br> if code_width_limit_chars.gtoe(0).and(line.len().gt(code_width_limit_chars)) {<br> line = line.substring(0 code_width_limit_chars)<br> }<br> line = line<br> .replace("&" "&amp;")<br> .replace("<" "&lt;")<br> .replace(">" "&gt;")<br> out = out.add(line.add("<br>"))<br> }<br> out<br>}<br><br>// data<br><br>index = read_string("index.html")<br><br>index_html = index.code_to_html(75)<br>build_script = read_string("build.mers").code_to_html(-1)<br>welcome_script = read_string("welcome.mers").code_to_html(-1)<br><br>// process index.html<br><br>out = ""<br>for line index.regex("\\S*.*").assume_no_enum() {<br> if line.starts_with("#") {<br> // comment, ignore<br> } else if line.starts_with("$") {<br> if line == "$welcome_script" {<br> out = out + welcome_script<br> } else if line == "$build_script" {<br> out = out + build_script<br> } else if line == "$index.html" {<br> out = out + index_html<br> }<br> } else {<br> // remove spaces<br> loop {<br> if line.starts_with(" ") {<br> line = line.substring(1)<br> } else {<br> true // break<br> }<br> }<br> out = out + line + "\n"<br> }<br>}<br>fs_write("../index.html" string_to_bytes(out)).assume_no_enum()<br><br></code></pre>