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 8135119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:18:27+00:00 2026-06-06T10:18:27+00:00

Whenever I try to use ranges in D, I fail miserably. What is the

  • 0

Whenever I try to use ranges in D, I fail miserably.

What is the proper way to use ranges in D? (See inline comments for my confusion.)

void print(R)(/* ref? auto ref? neither? */ R r)
{
    foreach (x; r)
    {
        writeln(x);
    }

    // Million $$$ question:
    //
    // Will I get back the same things as last time?
    // Do I have to check for this every time?

    foreach (x; r)
    {
        writeln(x);
    }
}

void test2(alias F, R)(/* ref/auto ref? */ R items)
{
    // Will it consume items?
    // _Should_ it consume items?
    // Will the caller be affected? How do I know?
    // Am I supposed to?
    F(items);
}
  • 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-06T10:18:30+00:00Added an answer on June 6, 2026 at 10:18 am

    Sorry, I can’t fit this into a comment :D. Consider if Range were defined this way:

    interface Range {
        void doForeach(void delegate() myDel);
    }
    

    And your function looked like this:

    void myFunc(Range r) {
        doForeach(() {
            //blah
        });
    }
    

    You wouldn’t expect anything strange to happen when you reassigned r, nor would you expect
    to be able to modify the caller’s Range. I think the problem is that you are expecting your template function to be able to account for all of the variation in range types, while still taking advantage of the specialization. That doesn’t work. You can apply a contract to the template to take advantage of the specialization, or use only the general functionality.
    Does this help at all?

    Edit (what we’ve been talking about in comments):

    void funcThatDoesntRuinYourRanges(R)(R r)
    if (isForwardRange(r)) {
        //do some stuff
    }
    

    Edit 2 std.range It looks like isForwardRange simply checks whether save is defined, and save is just a primitive that makes a sort of un-linked copy of the range. The docs specify that save is not defined for e.g. files and sockets.

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

Sidebar

Related Questions

Whenever I try to use apt-get/dpkg, it tries a to install a broken source.
Whenever I try to use the python interpreter on windows, C:\path>pythonw.exe cubes.py <blank line>
I keep getting a 'List does not exist' error whenever I try and use
I use TortoiseSVN normally and it works fine. Whenever I try to use SVN
I installed Msys Git and TortoiseGIT x64. Whenever I try to use the Get
Whenever I import java.awt.* and try to use a List I get an error.
Whenever I use WCF, I always try to make immutable classes that end up
I'm getting the SQLException Invalid Column Type whenever I try to use a parameter
Whenever I try to use the Rails script/generate or script/install command I get this
Whenever I try to use __sync_fetch_and_add with -m32 on a 64 bit machine, I

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.