Skip to content

Commit

Permalink
Merge branch 'gh-pages' of https://github.com/manastech/crystal into …
Browse files Browse the repository at this point in the history
…gh-pages
  • Loading branch information
askn committed Oct 29, 2015
2 parents c4995f4 + 3c8365b commit 3d71045
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 39 deletions.
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/c_bindings/alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ lib C
end
```

Refer to the [type grammar](type_grammar.html) for the notation used in alias types.
Refer to the [type grammar](../type_grammar.html) for the notation used in alias types.
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/c_bindings/fun.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The valid types to use in C bindings are:
* `Void`: the absence of a return value.
* `NoReturn`: similar to `Void`, but the compiler understands that no code can be executed after that invocation.

Refer to the [type gammar](type_grammar.html) for the notation used in fun types.
Refer to the [type gammar](../type_grammar.html) for the notation used in fun types.

The standard library defines the [LibC](https://github.com/manastech/crystal/blob/master/src/libc.cr) lib with aliases for common C types, like `int`, `short`, `size_t`. Use them in bindings like this:

Expand Down
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/c_bindings/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ change_it tz
tz.minutes_west #=> 0
```

Refer to the [type grammar](type_grammar.html) for the notation used in struct field types.
Refer to the [type grammar](../type_grammar.html) for the notation used in struct field types.
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/c_bindings/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ change_it value
value.some_int #=> 0
```

Refer to the [type grammar](type_grammar.html) for the notation used in union field types.
Refer to the [type grammar](../type_grammar.html) for the notation used in union field types.
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/c_bindings/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ lib C
end
```

Refer to the [type grammar](type_grammar.html) for the notation used in external variables types.
Refer to the [type grammar](../type_grammar.html) for the notation used in external variables types.
12 changes: 0 additions & 12 deletions _gitbook/syntax_and_semantics/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,6 @@ In the above code we didn't have to specify the type arguments of `MyBox`, the c

In this way generic types are less tedious to work with.

## Other uses for generic types

Although generic types are usually associated with containers, they can also be used to improve execution performance at the cost of a larger executable size. The main trick is to use a generic type to avoid runtime method dispatch. For example there's the standard library's `BufferedIO(T)`:

```crystal
file = File.open("myfile.txt")
io = BufferedIO.new(file) #:: BufferedIO(File)
io.gets
```

That `io` variable is a specified `BufferedIO(File)` instance, so invoking `gets` on it will end up invoking `File#gets`. If `BufferedIO` wasn't generic, that `gets` call would make a dispatch over all the `IO` types that were used to create buffered IOs. It being generic avoids this dispatch and gives better opportunities for the optimizer to inline stuff. However, each instantiation of `BufferedIO` will repeat almost the same code, but this is usually not as important as execution performance. Furthermore, many method calls will be inlined.

## Generic structs and modules

Structs and modules can be generic too. When a module is generic you include it like this:
Expand Down
2 changes: 1 addition & 1 deletion _gitbook/syntax_and_semantics/type_grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ When:
* specifying [type arguments](generics.html)
* [declaring variables](declare_var.html)
* declaring [aliases](alias.html)
* declaring [typedefs](type.html)
* declaring [typedefs](c_bindings/type.html)
* the argument of an [is_a?](is_a.html) pseudo-call
* the argument of an [as](as.html) expression
* the argument of a [sizeof](sizeof.html) expression
Expand Down
2 changes: 1 addition & 1 deletion docs/search_index.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/c_bindings/alias.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.15.9" data-basepath="../.." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.15.9" data-basepath="../.." data-revision="Tue Oct 27 2015 16:25:17 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2021,7 +2021,7 @@ <h1 id="alias">alias</h1>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">memcmp</span></span>(p1 <span class="hljs-symbol">:</span> <span class="hljs-constant">Void</span>*, p2 <span class="hljs-symbol">:</span> <span class="hljs-constant">Void</span>*, size <span class="hljs-symbol">:</span> <span class="hljs-constant">C::SizeT</span>) <span class="hljs-symbol">:</span> <span class="hljs-constant">Int32</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>Refer to the <a href="type_grammar.html">type grammar</a> for the notation used in alias types.</p>
<p>Refer to the <a href="../type_grammar.html">type grammar</a> for the notation used in alias types.</p>


</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/c_bindings/fun.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.15.2" data-basepath="../.." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.15.2" data-basepath="../.." data-revision="Wed Oct 28 2015 12:47:44 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2048,7 +2048,7 @@ <h1 id="fun">fun</h1>
<li><code>Void</code>: the absence of a return value.</li>
<li><code>NoReturn</code>: similar to <code>Void</code>, but the compiler understands that no code can be executed after that invocation.</li>
</ul>
<p>Refer to the <a href="type_grammar.html">type gammar</a> for the notation used in fun types.</p>
<p>Refer to the <a href="../type_grammar.html">type gammar</a> for the notation used in fun types.</p>
<p>The standard library defines the <a href="https://github.com/manastech/crystal/blob/master/src/libc.cr" target="_blank">LibC</a> lib with aliases for common C types, like <code>int</code>, <code>short</code>, <code>size_t</code>. Use them in bindings like this:</p>
<pre><code class="lang-crystal"><span class="hljs-class"><span class="hljs-keyword">lib</span> <span class="hljs-title">MyLib</span></span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">my_fun</span></span>(some_size <span class="hljs-symbol">:</span> <span class="hljs-constant">LibC::SizeT</span>)
Expand Down
4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/c_bindings/struct.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.15.3" data-basepath="../.." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.15.3" data-basepath="../.." data-revision="Wed Oct 28 2015 12:47:44 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2056,7 +2056,7 @@ <h1 id="struct">struct</h1>
change_it tz
tz.minutes_west <span class="hljs-comment">#=&gt; 0</span>
</code></pre>
<p>Refer to the <a href="type_grammar.html">type grammar</a> for the notation used in struct field types.</p>
<p>Refer to the <a href="../type_grammar.html">type grammar</a> for the notation used in struct field types.</p>


</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/c_bindings/union.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.15.4" data-basepath="../.." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.15.4" data-basepath="../.." data-revision="Wed Oct 28 2015 12:47:44 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2034,7 +2034,7 @@ <h1 id="union">union</h1>
change_it value
value.some_int <span class="hljs-comment">#=&gt; 0</span>
</code></pre>
<p>Refer to the <a href="type_grammar.html">type grammar</a> for the notation used in union field types.</p>
<p>Refer to the <a href="../type_grammar.html">type grammar</a> for the notation used in union field types.</p>


</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/c_bindings/variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.15.6" data-basepath="../.." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.15.6" data-basepath="../.." data-revision="Wed Oct 28 2015 12:47:44 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2012,7 +2012,7 @@ <h1 id="variables">Variables</h1>
<span class="hljs-variable">$errno</span> <span class="hljs-symbol">:</span> <span class="hljs-constant">Int32</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>Refer to the <a href="type_grammar.html">type grammar</a> for the notation used in external variables types.</p>
<p>Refer to the <a href="../type_grammar.html">type grammar</a> for the notation used in external variables types.</p>


</section>
Expand Down
9 changes: 1 addition & 8 deletions docs/syntax_and_semantics/generics.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.7.5" data-basepath=".." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.7.5" data-basepath=".." data-revision="Wed Oct 28 2015 19:27:22 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2120,13 +2120,6 @@ <h2 id="type-variables-inference">Type variables inference</h2>
<li>Because <code>MyBox</code> is actually <code>MyBox(T)</code>, and <code>T</code> is both a free variable and a type argument, <code>T</code> becomes the type of the passed value</li>
</ul>
<p>In this way generic types are less tedious to work with.</p>
<h2 id="other-uses-for-generic-types">Other uses for generic types</h2>
<p>Although generic types are usually associated with containers, they can also be used to improve execution performance at the cost of a larger executable size. The main trick is to use a generic type to avoid runtime method dispatch. For example there&apos;s the standard library&apos;s <code>BufferedIO(T)</code>:</p>
<pre><code class="lang-crystal">file = <span class="hljs-constant">File</span>.open(<span class="hljs-string">&quot;myfile.txt&quot;</span>)
io = <span class="hljs-constant">BufferedIO</span>.new(file) <span class="hljs-comment">#:: BufferedIO(File)</span>
io.gets
</code></pre>
<p>That <code>io</code> variable is a specified <code>BufferedIO(File)</code> instance, so invoking <code>gets</code> on it will end up invoking <code>File#gets</code>. If <code>BufferedIO</code> wasn&apos;t generic, that <code>gets</code> call would make a dispatch over all the <code>IO</code> types that were used to create buffered IOs. It being generic avoids this dispatch and gives better opportunities for the optimizer to inline stuff. However, each instantiation of <code>BufferedIO</code> will repeat almost the same code, but this is usually not as important as execution performance. Furthermore, many method calls will be inlined.</p>
<h2 id="generic-structs-and-modules">Generic structs and modules</h2>
<p>Structs and modules can be generic too. When a module is generic you include it like this:</p>
<pre><code class="lang-crystal"><span class="hljs-class"><span class="hljs-keyword">module</span> <span class="hljs-title">Moo</span>(<span class="hljs-title">T</span>)</span>
Expand Down
4 changes: 2 additions & 2 deletions docs/syntax_and_semantics/type_grammar.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<body>


<div class="book" data-level="4.16" data-basepath=".." data-revision="Fri Oct 02 2015 16:34:25 GMT+0000 (UTC)">
<div class="book" data-level="4.16" data-basepath=".." data-revision="Wed Oct 28 2015 12:47:44 GMT+0000 (UTC)">


<div class="book-summary">
Expand Down Expand Up @@ -2002,7 +2002,7 @@ <h1 id="type-grammar">Type grammar</h1>
<li>specifying <a href="generics.html">type arguments</a></li>
<li><a href="declare_var.html">declaring variables</a></li>
<li>declaring <a href="alias.html">aliases</a></li>
<li>declaring <a href="type.html">typedefs</a></li>
<li>declaring <a href="c_bindings/type.html">typedefs</a></li>
<li>the argument of an <a href="is_a.html">is_a?</a> pseudo-call</li>
<li>the argument of an <a href="as.html">as</a> expression</li>
<li>the argument of a <a href="sizeof.html">sizeof</a> expression</li>
Expand Down

0 comments on commit 3d71045

Please sign in to comment.