Skip to content

Conversation

denis-sh
Copy link
Contributor

asFlatStaticArray adds an ability to view any abject as a single-dimensional static array which is very useful for template code as it eliminates error-prone need to create branches and recursion for static-arrays support.

asFlatStaticArray isn't a to* function as it doesn't create a copy. It creates a view of object. And it is important that it doesn't require to provide any types explicitly.

Example:

int i;
static assert(is(typeof(asFlatStaticArray(i)) == int[1]));
asFlatStaticArray(i) = 5;
assert(i == 5);

int[1][2][3] mdimSArr;
static assert(is(typeof(asFlatStaticArray(mdimSArr)) == int[6]));
asFlatStaticArray(mdimSArr) = [1, 2, 3, 4, 5, 6];
assert(mdimSArr == [[[1], [2]], [[3], [4]], [[5], [6]]]);

Requires pull #924.

@dnadlinger
Copy link
Contributor

I am not too sure about the general usefulness of this – could you name 2-3 distinct use cases for the function?

@denis-sh
Copy link
Contributor Author

I am not too sure about the general usefulness of this – could you name 2-3 distinct use cases for the function?

What? It is useful for every function auto func(T, ...)(T t, ...) accepting a template parameter and doing something with its field. As examples: supplemental _initializeFrom for emplace (see #949), setInitialState, callCopyConstructors, and callDestructors (see #928).

@andralex
Copy link
Member

I'm also not sure about the utility of this. Why so close attention to the minutiae of multidimensional static arrays?

@denis-sh
Copy link
Contributor Author

First of all, to avoid branches and tail recursion is templates.

@denis-sh
Copy link
Contributor Author

But asFlatStaticArray also has an unlimited power to foreach everything:

foreach(ref el, t.asFlatStaticArray())
{
    // `el` isn't a static array.
    // Can `is(typeof(el) == struct)` e.g. to check for structs.
}

@andralex
Copy link
Member

But asFlatStaticArray also has an unlimited power to foreach everything:

Problem is, this is an unremarkable power to be had. I have seldom been in the situation of (a) dealing with multidimensional static arrays with so many dimensions looping became an issue; and (b) writing code that needs to do the same exact thing with each element regardless of its row/column/etc.

@denis-sh
Copy link
Contributor Author

As examples from my previous comment aren't enough to change you mind I'll make it package.

@denis-sh
Copy link
Contributor Author

@9rnsr, @alexrp, @jmdavis, or @AndrejMitrovic, could any of you confirm useless of this pull and close it? Thanks.

@9rnsr 9rnsr closed this Jun 10, 2013
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.

4 participants