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

  • Home
  • SEARCH
  • 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 9160857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:50:12+00:00 2026-06-17T13:50:12+00:00

I have modified std.array.array so that it will always work at compile time, and

  • 0

I have modified std.array.array so that it will always work at compile time, and it looks like this:

ForeachType!Range[] array(Range)(Range r)
if (isIterable!Range && !isNarrowString!Range)
{
  auto a = appender!(ForeachType!Range[])();
  foreach (e; r)
  {
    a.put(e);
  }
  return a.data;
}

And a usage:

struct Type
{
  int[] xs;
  this(int[] r) { this.xs = r.array; }
}

enum Type t1 = Type([]);
static if (t1.xs.length) { }  // Error: expression null.length is not constant

Base on my understanding, when r is an empty range, array() returns a null. In this regard, is there supposed to be a difference between null and an empty array?

Replacing return a.data; in array() with return a.data.length ? a.data : []; does fix the problem, so I suppose there is a difference?

The thing is that this only happens with constructors. The following doesn’t produce the error:

enum int[] t1 = iota(0,0).array;
static if (t1.length) { }

So I suppose there is no difference between null and []? I’m confused.

  • 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-17T13:50:13+00:00Added an answer on June 17, 2026 at 1:50 pm

    It’s true that[] compares equal to null. [] == null and [] is null are both true. The same goes for an unitialized array or one initialized with null.

    However, an empty array is not always null. For example:

    int[] a;
    assert(a is null);
    a = new int[2];
    a = a[0..0];
    assert(a !is null);
    

    See The D Programming Language, p. 95.

    Edit:
    In light of the fact that compile-time .length seems to be malfunctioning, I suggest using std.array.empty for the check, which should be more reliable than a null comparison. On DMD 2.061, static if (!t1.xs.empty) { } works where static if (t1.xs.length) { } failed.

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

Sidebar

Related Questions

I have modified a working Windows service that had always been starting beforehand. After
I have an std::map , and I would like to define an iterator that
We all know that things like this are valid in c++: const T &x
I have a std::map that I use to map values (field ID's) to a
I have a program that load data from a file using std::ifstream and store
I have a signal in VHDL declared like this : signal Temp_Key : std_logic_vector(79
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have modified the code in this question ,according to the answers there, in
I have a very simple queue implementation that wraps a fixed array. It contains
I have been trying to find an answer and get this to work for

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.