-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.html
More file actions
365 lines (342 loc) · 25.4 KB
/
print.html
File metadata and controls
365 lines (342 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js ayu">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title></title>
<meta name="robots" content="noindex" />
<!-- Custom HTML head -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff" />
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
</head>
<body>
<!-- Provide site root to javascript -->
<script type="text/javascript">
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "ayu" : "ayu";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script type="text/javascript">
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script type="text/javascript">
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('ayu')
html.classList.add(theme);
html.classList.add('js');
</script>
<!-- Hide / unhide sidebar before it is displayed -->
<script type="text/javascript">
var html = document.querySelector('html');
var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
}
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded "><a href="ch01-start.html"><strong aria-hidden="true">1.</strong> Getting Started</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch01-02-install.html"><strong aria-hidden="true">1.1.</strong> Installation</a></li><li class="chapter-item expanded "><a href="ch01-03-hw.html"><strong aria-hidden="true">1.2.</strong> Hello World</a></li></ol></li><li class="chapter-item expanded "><a href="ch02-00-guess.html"><strong aria-hidden="true">2.</strong> A simple Guessing Game</a></li><li class="chapter-item expanded "><a href="ch03-00-concepts.html"><strong aria-hidden="true">3.</strong> Basic Concepts</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch03-01-variables.html"><strong aria-hidden="true">3.1.</strong> Variables</a></li><li class="chapter-item expanded "><a href="ch03-02-functions.html"><strong aria-hidden="true">3.2.</strong> Functions</a></li><li class="chapter-item expanded "><a href="ch03-04-comments.html"><strong aria-hidden="true">3.3.</strong> Comments</a></li><li class="chapter-item expanded "><a href="ch03-04-classes-enums.html"><strong aria-hidden="true">3.4.</strong> Enums and Classes</a></li></ol></li><li class="chapter-item expanded "><a href="ch04-00-lifetimes.html"><strong aria-hidden="true">4.</strong> Understanding Lifetimes</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch04-01-what-is-lifetime.html"><strong aria-hidden="true">4.1.</strong> What is a lifetime?</a></li><li class="chapter-item expanded "><a href="ch04-02-lifetimes-in-typesharp.html"><strong aria-hidden="true">4.2.</strong> How to use lifetimes in Typesharp</a></li></ol></li><li class="chapter-item expanded "><a href="ch05-00-type-system.html"><strong aria-hidden="true">5.</strong> Typesharps Type system</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch05-01-generics.html"><strong aria-hidden="true">5.1.</strong> Generics</a></li><li class="chapter-item expanded "><a href="ch05-02-declarations.html"><strong aria-hidden="true">5.2.</strong> Type Declarations</a></li><li class="chapter-item expanded "><a href="ch05-03-unions.html"><strong aria-hidden="true">5.3.</strong> Unions</a></li><li class="chapter-item expanded "><a href="ch05-04-lasso.html"><strong aria-hidden="true">5.4.</strong> Lasso Types</a></li><li class="chapter-item expanded "><a href="ch05-05-expression.html"><strong aria-hidden="true">5.5.</strong> Evaluating Types</a></li><li class="chapter-item expanded "><a href="ch05-06-constants.html"><strong aria-hidden="true">5.6.</strong> Constant Types</a></li><li class="chapter-item expanded "><a href="ch05-06-instancing.html"><strong aria-hidden="true">5.7.</strong> Instance Types</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky bordered">
<div class="left-buttons">
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</button>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu (default)</button></li>
<li role="none" disabled="true"><button role="menuitem" class="theme" id="gitdark" disabled>Github Dark</button></li>
<li role="none" disabled="true"><button role="menuitem" class="theme" id="gitlight" disabled>Github Light</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title"></h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script type="text/javascript">
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>
<ul>
<li><a href="ch01-02-install.html">Installation</a></li>
<li><a href="ch01-03-hw.html">Hello World</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="installation"><a class="header" href="#installation">Installation</a></h1>
<p>In order to use TypeSharp, you must install it first. This guide will use the official <a href="">Typesharp CLI</a> for managing typesharp versions, and associated utilities.</p>
<h2 id="installing-t"><a class="header" href="#installing-t">Installing <em>T#</em></a></h2>
<p><a href="">TypeSharp CLI</a> is TypeSharp's command line interface / package manager that can be invoked using <code>t#</code> or <code>ts</code> in command line.</p>
<blockquote>
<p><strong>Note:</strong> If you are migrating from typescript, and have an alias for typescript set as <code>ts</code> you will need to remove it.</p>
</blockquote>
<p>To install TypeSharp's CLI you will need to open a shell session and enter the following command:</p>
<pre><code class="language-shell">$ curl http://localhost:9328/bin/u/latest -sSf | sh
</code></pre>
<p>or if on windows:</p>
<pre><code class="language-powershell">iwr -useb http://localhost:9328/bin/10/latest | iex; install
</code></pre>
<p>with this command executed, you have successfully installed typesharp (awesome!).</p>
<h2 id="updating-or-uninstalling"><a class="header" href="#updating-or-uninstalling">Updating or Uninstalling</a></h2>
<p>If you decide you don't like Typesharp, you can run:</p>
<pre><code class="language-shell">$ ts uninstall -y
</code></pre>
<p>If you would like to update to a newer version of TypeSharp, you can run:</p>
<pre><code class="language-shell">$ ts upgrade
</code></pre>
<p>If you would like to change to a different/specific version of TypeSharp you can use:</p>
<pre><code class="language-shell">$ ts upgrade --version "1.0.0"
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="hello-world"><a class="header" href="#hello-world">Hello World</a></h1>
<p>Now that you have TypeSharp downloaded, and installed, let's make the infamous "hello world" program in TypeSharp!</p>
<h2 id="setup"><a class="header" href="#setup">Setup</a></h2>
<p>Typesharp by default has a <a href="">CLI</a> utility to manage packages and projects. You can start or initialize a project by opening a terminal and running the following:</p>
<pre><code class="language-shell">$ ts init "hello_world"
</code></pre>
<p>After executing the <code>init</code> command you should now see a the following in your project directory:</p>
<p><img src="https://i.imgur.com/2aorK4a.png" alt="https://imgur.com/a/vyb2sOd" /></p>
<p>In your <code>mod.ts</code> file you can insert the following:</p>
<pre><code class="language-typesharp">println("Hello World.")
</code></pre>
<blockquote>
<p><strong>Notice:</strong> It is important to note, that Typesharp does have a <code>main-fn</code> flag for compiling, which will require you to change the code from above to the following if that flag is present:</p>
<pre><code class="language-typesharp">fn main() {
println("Hello World.");
}
</code></pre>
</blockquote>
<div style="break-before: page; page-break-before: always;"></div><h1 id="a-simple-guessing-game"><a class="header" href="#a-simple-guessing-game">A simple Guessing game</a></h1>
<p>Now that we've made a very simple program that prints "Hello, world" to console, let's make a simple guessing game! The game will take a number given by the player and check if it matches a randomly generated number by the computer. If the number is too high, the computer will say "too high" and respectively, "too low" if the number the player inputs is too low.</p>
<h2 id="getting-setup"><a class="header" href="#getting-setup">Getting Setup</a></h2>
<p>We can use what we learned from the <a href="">hello world</a> example for generating a TypeSharp project by opening a terminal and executing:</p>
<pre><code class="language-shell">$ ts init "number_game"
</code></pre>
<p>Now that we have our project setup, navigate to the <code>mod.ts</code> file in the <code>src</code> directory of the project and open it in an editor.</p>
<p>If you'd like, you can make sure you did everything correctly by executing the following in the currently open terminal:</p>
<pre><code class="language-shell">$ ts run
</code></pre>
<p>Which should output:</p>
<pre><code class="language-shell">"Hello, world!"
</code></pre>
<h2 id="generating-a-number"><a class="header" href="#generating-a-number">Generating a number</a></h2>
<p>By default, TypeSharp does <strong>not</strong> implement any utilities for generating random numbers, you'll need to include this from the standard library! You can achieve this in one of the following ways:</p>
<ul>
<li>Using the "use" keyword, for example: <code>use std::math</code></li>
<li>Using the "import" keyword, with js-like syntax: <code>import { math } from 'std'</code></li>
</ul>
<p>With that said, we're going to stick to the first way, and the most standardized way of importing in TypeSharp. In your <code>src/mod.ts</code> file, add:</p>
<pre><code class="language-typesharp">use std::math;
use std::io::stdin;
</code></pre>
<h4 id="making-a-working-prototype"><a class="header" href="#making-a-working-prototype">Making a working prototype</a></h4>
<p>Now that we have our import, we need to add the code to save a random number for the user to guess. By default, TypeSharp is <strong>strict</strong>. Meaning, variables are only mutable to it's predefined type. With that said, let's get some things working!</p>
<p>Before we do anything, we need to tell the compiler that we would like to use lazy numbers, because they are not enabled by default, let's do that with the following:</p>
<pre><code class="language-typesharp">#compiler numerics-lazy
</code></pre>
<p>After that, let's make a type, this type will specify our <code>GameInt</code>! It will only be a number from 0 - 100.</p>
<pre><code class="language-typesharp">type GameInt = (x: int) => x in 0..100;
</code></pre>
<p>Now let's create a game class for our game! This will run our game until the game is over!
Here we'll declare the class, as well as our randomly generated number, which we'll save as <code>generated</code>.</p>
<pre><code class="language-typesharp">class Game {
static generated: GameInt = random(0..100);
pub fn loop(): void {
// ..
}
}
</code></pre>
<p>Next, before we can start checking inputs from the user, we need to make a function to loop our game! The game loop will contain all the logic for out game! While you can name it whatever you like, we're naming it <code>loop</code> for simplicity. In our loop function (inside the <code>Game</code> class) we need to add a way to check for a user input! Let's do that using <code>readln</code> from <code>stdin</code>. Because <code>readln</code> literally reads a line from stdin aka (until the user presses enter or enters a new line) we can wait for the user to guess something and then try to parse it using the <code>parse</code> implementation from stdin. However if it fails, we need to output something to the user to let them know that their input was invalid, and then proceed with the game loop.</p>
<pre><code class="language-typesharp">// Assign the result from readln to a var named "guess"
let guess = readln().parse<GameInt>() || {
// If parsing fails, print line.
println("Please input a number from 0 - 100!");
// Return (continue) the loop.
return this.loop();
}
</code></pre>
<p>Now we need to implement the checking for each guess, we're going to use some simple logic operations for this. If <code>guess</code> is higher than <code>generated</code> we will print "Guess is too high.", and if the guess is too low, we will print "Guess is too low".</p>
<pre><code class="language-typesharp">if guess == self::generated {
println("You won! You guessed the number!");
return;
} else if (guess > self::generated) {
println("You guessed to high...");
} else {
println("You guessed to low...");
}
return false;
</code></pre>
<p>With this completed, we can simply invoke the <code>loop</code> function by instancing our class and calling loop! After we do this our simple guessing program should now be complete! The final product should look similar to the following.</p>
<pre><code class="language-typesharp">use std::math::random;
use std::io::stdin;
type GameInt = (x: int) => x in 0..100;
class Game {
static generated: GameInt = random(0..100);
pub fn loop(): void {
let guess = readln().parse<GameInt>() || {
// If parsing fails, print line.
println("Please input a number from 0 - 100!");
// Return (continue) the loop.
return this.loop();
}
if guess == self::generated {
println("You won! You guessed the number!");
return;
} else if (guess > self::generated) {
println("You guessed to high...");
} else {
println("You guessed to low...");
}
this.loop();
}
}
new Game().loop();
</code></pre>
<h2 id="running-our-program"><a class="header" href="#running-our-program">Running our program</a></h2>
<p>There are multiple ways to run a TypeSharp program, you could either do the first, and most recommended solution for production, aka the one you learned in the <a href="">Hello World</a> example; or you can make use of <code>ts run</code>, which will run a "script" or "package" given the command was ran properly, syntax can be seen in <strong>Block.1</strong> or can use the command <code>ts compile</code> from the previous guide.</p>
<h5 id="block1"><a class="header" href="#block1">Block.1</a></h5>
<p>You will need to first open a terminal in your project's <a href="">parent directory</a> and execute the following command for this to work:</p>
<pre><code class="language-shell">ts run .
</code></pre>
<p>We used <code>ts compile</code> and execute the binary in the bin directory in your project, however your final solution should be similar to:</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="basic-concepts"><a class="header" href="#basic-concepts">Basic Concepts</a></h1>
<ul>
<li><a href="ch03-01-variables.html">Variables</a></li>
<li><a href="ch03-02-functions.html">Functions</a></li>
<li><a href="ch03-04-comments.html">Comments</a></li>
<li><a href="ch03-04-classes-enums.html">Enums and Classes</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="variables"><a class="header" href="#variables">Variables</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="functions"><a class="header" href="#functions">Functions</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="comments"><a class="header" href="#comments">Comments</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="enums-and-classes"><a class="header" href="#enums-and-classes">Enums and Classes</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="understanding-lifetimes"><a class="header" href="#understanding-lifetimes">Understanding Lifetimes</a></h1>
<ul>
<li><a href="ch04-01-what-is-lifetime.html">What is a lifetime?</a></li>
<li><a href="ch04-02-lifetimes-in-typesharp.html">How to use lifetimes in Typesharp</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="what-is-a-lifetime"><a class="header" href="#what-is-a-lifetime">What is a lifetime?</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-use-lifetimes-in-typesharp"><a class="header" href="#how-to-use-lifetimes-in-typesharp">How to use lifetimes in Typesharp</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="typesharps-type-system"><a class="header" href="#typesharps-type-system">Typesharps Type system</a></h1>
<ul>
<li><a href="ch05-01-generics.html">Generics</a></li>
<li><a href="ch05-02-declarations.html">Type Declarations</a></li>
<li><a href="ch05-03-unions.html">Unions</a></li>
<li><a href="ch05-04-lasso.html">Lasso Types</a></li>
<li><a href="ch05-05-expression.html">Evaluating Types</a></li>
<li><a href="ch05-06-constants.html">Constant Types</a></li>
<li><a href="ch05-06-instancing.html">Instance Types</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="generics"><a class="header" href="#generics">Generics</a></h1>
<p>Type generics in Typesharp function very similarly to <a href="https://www.typescriptlang.org/docs/handbook/2/generics.html">Typescripts</a>. Generics are the identity of an identifer,</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="type-declarations"><a class="header" href="#type-declarations">Type Declarations</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="unions"><a class="header" href="#unions">Unions</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="lasso-types"><a class="header" href="#lasso-types">Lasso Types</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="evaluating-types"><a class="header" href="#evaluating-types">Evaluating Types</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="constant-types"><a class="header" href="#constant-types">Constant Types</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="instance-types"><a class="header" href="#instance-types">Instance Types</a></h1>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script type="text/javascript">
var socket = new WebSocket("ws://localhost:3000/__livereload");
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script type="text/javascript">
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
<!-- Additional Langs -->
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
<script src="book.js" type="text/javascript" charset="utf-8"></script>
<!-- Custom JS scripts -->
<script type="text/javascript" src="typesharp.js"></script>
<script type="text/javascript">
window.addEventListener('load', function() {
window.setTimeout(window.print, 100);
});
</script>
</body>
</html>