Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:35:30+00:00 2026-06-01T02:35:30+00:00

Consider following code: enum size = 16; double[size] arr1 = […]; double[size] arr2 =

  • 0

Consider following code:

enum size = 16;
double[size] arr1 = [...];
double[size] arr2 = [...];
process = (double x) { return (x + 1); };

arr2[] = map!(process)(arr1[]); // here

I have trouble converting results of map back to my plain array. Problem applies not only to map, but also to take, repeat and all those fine tools from std.algorithm and std.range that operate on ranges.

On this assignment, I get Error: cannot implicitly convert expression (map(arr1[])) of type Result to double[]. How can I evaluate range to array without using

uint i = 0;
foreach (x; map!(process)(arr1[])) {
    arr2[i] = x;
    i++;
}

?

Additionally, can someone please explain, why I must call map!(process)(arr1[]) instead of map!(process)(arr1) with static arrays? Shouldn’t static arrays be compatible with dynamic for means of iteration, or I don’t get something?

Also, it seems that straightforward enumeration syntax foreach (index, item; sequence) does not work for ranges – are there workarounds? I guess the reason is the same as why ranges cannot be assigned to array slices.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:35:31+00:00Added an answer on June 1, 2026 at 2:35 am

    Functions such as map and filter return ranges, not arrays, so simply assigning to an array isn’t going to work any more than assigning a string to wstring is going to work. They’re different types. And for many range-based functions (including map and filter), the ranges they return are actually lazy in order to avoid unnecessary computation, which makes them that much less compatible with an array. The solution is to use std.array.array, which takes a range and creates a dynamic array from it. So, you could do

    auto arr = array(map!process(origArray));
    

    However, I would advise not converting a range into an array before you actually need to, since it can result in unnecessary computations, and it means allocating a new array. If you actually need an array, then by all means, use std.array.array to convert the range, but operating on the range can often be more efficient if you don’t need an actual array. However, if you want to convert the result to a static array as opposed to a dynamic one, you’re probably better off just assigning each element in a loop (and maybe skipping map altogether), since using std.array.array will then have allocated a dynamic array that you won’t be using once you’ve assigned to the static array. It’s a waste of memory.

    Also, be aware that using static arrays with range-based functions can be risky in that they must slice the static array to get a dynamic array for the range-based functions to process, and if that dynamic array escapes the scope that the static array was declared in, then you’re leaking references to data which no longer exists. For example,

    auto func()
    {
        int[5] arr;
        return map!process(arr[]);
    }
    

    would be very bad. However, as long as you’re done using the slice and nothing refers to it anymore (including any ranges that might have been created) before you exit the scope with the static array in it, you should be fine. It is something to be careful of though.

    As for the question about having to slice static arrays, you really should ask that as a separate question, but two existing questions that relate to it are this one and this one. What it pretty much comes down to is that IFTI (Implicit Function Template Instantiation) instantiates using the exact type that it’s given, and a static array is neither a dynamic array nor a range, so any templated function which specifically requires a dynamic array or a range will fail to compile with a static array. The compiler will implicitly slice static arrays to convert them to dynamic arrays for functions which explicitly take dynamic arrays, but those sort of implicit conversions don’t happen with template instantiation, so you must explicitly slice static arrays to pass them to range-based functions.

    As for the question about using foreach with indices and ranges, again, you shouldn’t be asking multiple questions in the same question. Please post separate questions for each question that you have. What it comes down to though is that

    foreach(elem; range)
    {
        //stuff
    }
    

    gets lowered to something close to

    for(; !range.empty; range.popFront())
    {
        auto elem = range.front;
        //stuff
    }
    

    And that doesn’t involve indices at all. It could be change to create an index variable for you, but it doesn’t always make sense for ranges to have their index iterating by one like that on every iteration (much as it usually would be fine), and so that hasn’t been done. It’s simple enough to add your own counter though.

    {
        size_t i;
        foreach(elem; range)
        {
            //stuff
            ++i;
        }
    }
    

    opApply does support using indices with foreach, but it isn’t a range, and doesn’t work with range-based functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public enum SomeCode { NIF = 0 ,NIE = 1
Consider the following (simplified) code: enum eTestMode { TM_BASIC = 1, // 1 <<
Please consider the following code: class Student { } enum StudentType { } static
Consider following code: typedef SomeType type_t[2]; SomeType * arr1 = new type_t; //new or
Consider following code public class City { public string Name { get { return
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
Consider the following code enum HorizontalAlignment { Left, Middle, Right }; enum VerticleAlignment {
Please consider the following code. enum type {CONS, ATOM, FUNC, LAMBDA}; typedef struct{ enum
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.