Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not supposed to know or care what <code>std.algorithm.map</code> returns beyond the fact that it's a range of the same genre as the one passed in (forward, bidirectional, random, etc.). It's that way with most range-based functions. They almost always return either a new range which wraps the one passed in or a the exact same type of range as was passed in (e.g. <code>map</code> does the former; <code>find</code> does the latter). Use <code>auto</code>:</p> <pre><code>ulong[] x = [1, 2, 3]; auto y = map!"a"(x); </code></pre> <p>The range returned by <code>map</code> is lazy. It doesn't do anything until you iterate over it (it then calls the given function on each successive <code>front</code> of the underlying range). It's more efficient that way (as well as enabling infinite ranges). The exact return type depends on the type of range that you passed in and is local to <code>map</code> so that you can't create one directly. You need to either use <code>auto</code> to infer the type or <code>typeof</code> to get the type:</p> <pre><code>typeof(map!"a"(x)) y = map!"a"(x); </code></pre> <p>However, you generally only use <code>typeof</code> when you need a variable which you cannot initialize directly. <code>auto</code> is almost always the way to go.</p> <p>If you need to create an array from the result of <code>map</code> (or from any other range), then use <a href="http://dlang.org/phobos/std_array.html#array" rel="nofollow"><code>std.array.array</code></a>:</p> <pre><code>ulong[] y = array(map!"a"(x)); </code></pre> <p>If you don't know much about ranges, then you should probably read <a href="http://ddili.org/ders/d.en/ranges.html" rel="nofollow">this</a>. Unfortunately, there isn't currently an article on dlang.org explaining ranges, but that link is for a chapter in a book which one of the members of the D community wrote in Turkish and has been translating to English, and it covers ranges fairly well.</p> <p><strong>EDIT</strong>:</p> <p>Walter Bright recently wrote an article specifically about types which are local to a function but returned by the function which may also help enlighten you. They even get a cool name: <a href="http://www.drdobbs.com/blogs/cpp/232901591" rel="nofollow"><code>Voldemort Types in D</code></a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload