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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:53:22+00:00 2026-06-05T20:53:22+00:00

I have some rather odd behavior in my D program that I’ve narrowed down

  • 0

I have some rather odd behavior in my D program that I’ve narrowed down to this:

import std.algorithm;
import std.stdio;
import std.traits;

enum E { a, b, c };

struct S { E e; };

void main()
{
    immutable(S)[] source = [ S(E.a), S(E.a), S(E.b) ];
    foreach (e; EnumMembers!E)
    {
        size_t c = count!(x => x.e == e)(source);
        writeln(e, " -> ", c);
    }
}

I would expect the output of this program to be something along the lines of:

a -> 2
b -> 1
c -> 0

But the actual result is:

a -> 2
b -> 2
c -> 2

Curiously, changing the for loop to foreach (e; [ E.a, E.b, E.c ]) produces my expected output. Using foreach (e; [ EnumMembers!E ]) also produces my expected result, so clearly my use of the range from EnumMemebers is the problem here…I just don’t know why.

I am clearly doing something wrong, but I have no idea what and would appreciate some insight.

My compiler is DMD64 D Compiler v2.059 on Linux.


EDIT: This has the exact same behavior with GDC 4.6.3, so it can’t be a compiler bug.


EDIT: By moving the count call to a separate function:

size_t counte(Range)(E e, Range src)
{
    return count!(x => x.e == e)(src);
}

and changing c‘s initialization to size_t c = counte(e, source);, the program works as I would expect.

  • 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-05T20:53:25+00:00Added an answer on June 5, 2026 at 8:53 pm

    Short and incomplete answer that may help with direction to dig for:

    EnumMembers!T is a type tuple ( http://dlang.org/tuple.html -> Type Tuple ), not an expression tuple or array. When you use [ EnumMembers!T ] syntax, tuple is used as an initializer list at compile time and regular array is created, providing expected behavior.

    Now, if you use type tuple in a foreach statement as-is, things gotta get interesting. There is a special case foreach for tuples: http://dlang.org/statement.html -> Foreach over Tuples. And in case of expression type tuples ( sorry for weird wording, but unfortunately, that is how it is named in D ) it does not really create a variable – it just replaces all usages of e with expression taken from type tuple.

    And here we go – e in lambda is just replaced with expression from tuple and so this lambda is NOT a delegated. I have checked it’s typeof, it is “bool function(S x) pure nothrow”. Guess it is created the very first time it is used, remembering e expression in lambda code and then just using it as-is.

    I need someone else to comment about is it a bug, misfeature or works as intended.

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

Sidebar

Related Questions

I have a rather large program that have some odd behaviour once in a
Ok, I have some rather odd behavior in one of my Projects and I'm
Is there some way when sending this message to specify that I rather have
I have a rather odd scenario here, hoping someone might have some insight. I
So I have a rather odd issue that I wanted to see if anyone
My colleague and I have encountered some rather odd behavour. Our environments are Ubuntu
I have some rather long string containing just about anything that I want to
I have encountered a rather odd error that I do not understand. I created
I have a rather long switch-case statement. Some of the cases are really short
I have a form, user enters some data then submits. Rather than a popup

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.