Skip to content

Add __traits isDynamicArray - #23803

Open
nordlow wants to merge 1 commit into
dlang:masterfrom
nordlow:add-more-traits
Open

Add __traits isDynamicArray#23803
nordlow wants to merge 1 commit into
dlang:masterfrom
nordlow:add-more-traits

Conversation

@nordlow

@nordlow nordlow commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Partially resolves #23784.

I can split the PR up into two if requested.

Changelog will be updated if this gets approved.

Looking into failing tests.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

DMD perf check

Metric Base PR Δ
dmd binary size (stripped) 6.86 MB 6.88 MB +0.28%
All measurements
Metric Base PR Δ
compile hello.d (instr) 215.1 M 215.1 M -0.001%
compile hello.d -O -release (instr) 233.4 M 233.4 M +0.002%
compile Phobos (instr) 5,120.6 M 5,120.5 M -0.002%
compile Phobos codegen (instr) 1,472.4 M 1,472.4 M -0.004%
compile vibe.d (instr) 15,112.9 M 15,112.0 M -0.006%
dmd binary size (stripped) 6.86 MB 6.88 MB +0.28%
hello binary size (stripped) 0.72 MB 0.72 MB 0.00%
peak RSS (compile hello.d) 43.50 MB 43.57 MB +0.17%
peak RSS (compile Phobos) 620.5 MB 620.2 MB -0.05%
peak RSS (compile vibe.d) 1918 MB 1917 MB -0.03%
compile dmd itself (wall) 9.4 s 9.4 s -0.02%
compile hello.d (wall) 50.8 ms 50.4 ms -0.73%
compile Phobos (wall) 1,116 ms 1,115 ms -0.04%

601e313 vs merge-base 86b74e2 · about these metrics

@ntrel

ntrel commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

runnable/traits.d TEST_OUTPUT needs updating.

@dkorpel

dkorpel commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

What's the rationale for isArray and why is an Associative Array not one?

@dkorpel

dkorpel commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

From the issue

Please do not allow this trait to accept enums!

This PR's implementation accepts enums

@nordlow

nordlow commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

What's the rationale for isArray and why is an Associative Array not one?

Good point. I agree that isArray should be true for AAs.

Update: I removed isArray from this PR.

@nordlow

nordlow commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

From the issue

Please do not allow this trait to accept enums!

This PR's implementation accepts enums

Shouldn't we mimic the behavior __traits(isStaticArray, ...) in this case? Despite it accepting enums is a bad design.

And then later disallow all traits of this kind to not accept enums?

For the sake of consistency.

I'm ok with both alternatives.

@dkorpel

dkorpel commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Good point. I agree that isArray should be true for AAs.

That's not what I meant, I don't see why it needs to exist in the first place. What's the real world use case?

@nordlow

nordlow commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Good point. I agree that isArray should be true for AAs.

That's not what I meant, I don't see why it needs to exist in the first place. What's the real world use case?

I don't know.

@nordlow

nordlow commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

compiler/test/run.d runnable/traits.d now passes locally for me.

@nordlow nordlow changed the title Add __traits isDynamicArray and isArray Add __traits isDynamicArray Sep 9, 2026
@nordlow

nordlow commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

What's the rationale for isArray and why is an Associative Array not one?

Removed isArray changes from this PR.

Comment thread changelog/dmd.traits.isDynamicArray.dd Outdated
@@ -0,0 +1,3 @@
Add a new unary builtin trait `isDynamicArray` that evalutes to true
iff its single type argument is a D (fat-pointer) array (slice) of any
type. No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline at end of file missing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Geod24

Geod24 commented Sep 10, 2026

Copy link
Copy Markdown
Member

Can't we do this with an is expression ?

@nordlow

nordlow commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Can't we do this with an is expression ?

Yes, as seen in

template isDynamicArray(T)
{
    static if (is(T == U[], U))
        enum bool isDynamicArray = true;
    else static if (is(T U == enum))
        // BUG: isDynamicArray / isStaticArray considers enums
        // with appropriate base types as dynamic/static arrays
        // Retain old behaviour for now, see
        // https://github.com/dlang/phobos/pull/7574
        enum bool isDynamicArray = isDynamicArray!U;
    else
        enum bool isDynamicArray = false;
}

So this addition is for conformity with the existing builtin traits isStaticArray and isAssociativeArray.

The behavior of supporting enums for all the builtin traits is...Array might not be the preferred default. But we copy the behavior of the builtin isStaticArray for, again, conformity.

Note, that long-term I would like increase convenience of builtin traits by somehow making them referreable to as identifiers and passable as alias parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add is-expression or __traits for Dynamic Arrays

5 participants