<spanclass="n">writeln</span><spanclass="p">(</span><spanclass="s">"The answer is "</span><spanclass="p">,</span><spanclass="n">root</span><spanclass="p">[</span><spanclass="s">"Answer"</span><spanclass="p">].</span><spanclass="n">get</span><spanclass="p">!</span><spanclass="kt">int</span><spanclass="p">);</span>
<spanclass="p">}</span>
</pre></div>
</div>
<divclass="section"id="explanation-of-the-code">
<h3>Explanation of the code<aclass="headerlink"href="#explanation-of-the-code"title="Permalink to this headline">¶</a></h3>
<p>First, we import the <em>yaml</em> module. This is the only module you need to import
to use D:YAML - it automatically imports all needed modules.</p>
<p>Next we load the file using the <em>yaml.load()</em> function - this loads the file as
<strong>one</strong> YAML document and throws <em>YAMLException</em>, D:YAML exception type, if the
file could not be parsed or does not contain exactly one document. Note that we
don’t do any error checking here in order to keep the example as simple as
possible.</p>
<p><em>yaml.Node</em> represents a node in a YAML document. It can be a sequence (array),
mapping (associative array) or a scalar (value). Here the root node is a
mapping, and we use the index operator to get subnodes with keys “Hello World”
and “Answer”. We iterate over the first, as it is a sequence, and use the
<em>yaml.Node.get()</em> method on the second to get its value as an integer.</p>
<p>You can iterate over a mapping or sequence as if it was an associative or normal
array. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
<p>You can iterate over subnodes using yaml.Node as the iterated type, or specify
the type subnodes are expected to have. D:YAML will automatically convert
iterated subnodes to that type if possible. Here we specify the <em>string</em> type,
so we iterate over the “Hello World” sequence as an array of strings. If it is
not possible to convert to iterated type, a <em>YAMLException</em> is thrown. For
instance, if we specified <em>int</em> here, we would get an error, as “Hello”
cannot be converted to an integer.</p>
<p>The <em>yaml.Node.get()</em> method is used to get value of a scalar node as specified
type. D:YAML will try to return the scalar as specified type, converting if
needed, throwing <em>YAMLException</em> if not possible.</p>
</div>
<divclass="section"id="compiling">
<h3>Compiling<aclass="headerlink"href="#compiling"title="Permalink to this headline">¶</a></h3>
<p>To compile your project, you must give DMD the directories containing import
modules and the library. You also need to tell it to link with D:YAML. The import
directory should be the D:YAML package directory. You can specify it using the
<ttclass="docutils literal"><spanclass="pre">-I</span></tt> option of DMD. The library directory should point to where you put the
compiled D:YAML library. On Unix/Linux you can specify it using the <ttclass="docutils literal"><spanclass="pre">-L-L</span></tt>
option, and link with D:YAML using the <ttclass="docutils literal"><spanclass="pre">-L-l</span></tt> option. On Windows, the import
directory is used as the library directory. To link with the library on Windows,
just add the path to it relative to the current directory.</p>
<p>For example, if you extracted D:YAML to <ttclass="docutils literal"><spanclass="pre">/home/xxx/dyaml</span></tt> and compiled it in
that directory, your project is in <ttclass="docutils literal"><spanclass="pre">/home/xxx/dyaml-project</span></tt>, and you are
currently in that directory, you can compile the project with the following
<p>This will produce an executable called <ttclass="docutils literal"><spanclass="pre">main</span></tt> or <ttclass="docutils literal"><spanclass="pre">main.exe</span></tt> in your
directory. When you run it, it should produce the following output:</p>
<divclass="highlight-python"><pre>Hello
World
The answer is 42</pre>
</div>
</div>
<divclass="section"id="conclusion">
<h3>Conclusion<aclass="headerlink"href="#conclusion"title="Permalink to this headline">¶</a></h3>
<p>You should now have a basic idea about how to use D:YAML. To learn more, look at
the <aclass="reference external"href="../api/index.html">API documentation</a> and other tutorials. You can find code for this
example in the <ttclass="docutils literal"><spanclass="pre">example/getting_started</span></tt> directory in the package.</p>