## digitalmars.D - generic array functions and vector operations

• Bent Rasmussen (14/14) Aug 18 2008 I wanted to test out array operations and create some nice generic funct...
• Steven Schveighoffer (9/18) Aug 18 2008 missing feature. It is a (commonly annoying) limitation of the D spec.
• Bent Rasmussen (15/28) Aug 18 2008 That's what I figured as well. It's an ugly solution syntax-wise though,
• Walter Bright (4/11) Aug 18 2008 Just write it as:
• Steven Schveighoffer (4/15) Aug 18 2008 What if you just wanted to use static arrays for the non-heap performanc...
• Bent Rasmussen (9/20) Aug 18 2008 As Steven writes, I'd prefer optimal efficiency (who wouldn't). Of cours...
• Bent Rasmussen (9/20) Aug 18 2008 I see that the language specification likens tuples to [static] arrays a...
"Bent Rasmussen" <IncredibleShrinkingSphere Gmail.com> writes:
```I wanted to test out array operations and create some nice generic functions
for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

It looks pretty elegant with array operations, but I can't return a static
array. Is this a bug or missing feature and if not how might one get similar
elegant syntax without sacrificing efficiency?

I'm not sure the "uint n" part is even valid, but it made sense as it is
allowed in templates in general.

How would you define and deal with vectors in D?

Bent

```
Aug 18 2008
"Steven Schveighoffer" <schveiguy yahoo.com> writes:
```"Bent Rasmussen" wrote
I wanted to test out array operations and create some nice generic
functions for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

It looks pretty elegant with array operations, but I can't return a static
array. Is this a bug or missing feature and if not how might one get
similar elegant syntax without sacrificing efficiency?

missing feature.  It is a (commonly annoying) limitation of the D spec.

To get around it, you can return a struct with the array inside.

struct StaticArray(T, uint n)
{
T[n] value;
}

StaticArray!(A, n) lerp(A, uint n)...

-Steve
```
Aug 18 2008
"Bent Rasmussen" <IncredibleShrinkingSphere Gmail.com> writes:
```"Steven Schveighoffer" <schveiguy yahoo.com> skrev i meddelelsen
missing feature.  It is a (commonly annoying) limitation of the D spec.

To get around it, you can return a struct with the array inside.

struct StaticArray(T, uint n)
{
T[n] value;
}

StaticArray!(A, n) lerp(A, uint n)...

That's what I figured as well. It's an ugly solution syntax-wise though,
but as the beautiful way is impossible, I suppose it is beautiful for
all practical purposes :-)

It just takes away some of the elegance of array/vector operations in
D as far as I'm concerned.

But I do have a modified struct-based vector implementation inspired
by work done by others here on the forum. That'll have to do until
then.

By the way: Is there a way to vote for features in D? :-)

And where might this get fixed? In D2 and D1 or only in D2?
I want to use D2 but my understanding is that DSSS or is that
Derelict, or both, only supports D1.

Best regards

Bent

-Steve

```
Aug 18 2008
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
```"Bent Rasmussen" <IncredibleShrinkingSphere Gmail.com> wrote in message
news:g8ccrj\$1c26\$1 digitalmars.com...

By the way: Is there a way to vote for features in D? :-)

And where might this get fixed? In D2 and D1 or only in D2?
I want to use D2 but my understanding is that DSSS or is that
Derelict, or both, only supports D1.

Only in D2.  D1 does not get new features.

And yes, you can vote for features in D, by simply voicing your opinion.
There is an infinitessimally small (but non-zero) chance that your
suggestion will make it in.  But returning static arrays is something that
even Walter has mentioned before, so I wouldn't be surprised if it made it
in.
```
Aug 18 2008
Walter Bright <newshound1 digitalmars.com> writes:
```Bent Rasmussen wrote:
I wanted to test out array operations and create some nice generic
functions for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

Just write it as:

A[] lerp(A)(A t, A[] a, A[] b)
...
```
Aug 18 2008
"Steven Schveighoffer" <schveiguy yahoo.com> writes:
```"Walter Bright" wrote
Bent Rasmussen wrote:
I wanted to test out array operations and create some nice generic
functions for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

Just write it as:

A[] lerp(A)(A t, A[] a, A[] b)
...

What if you just wanted to use static arrays for the non-heap performance
benefits (and value semantics)?

-Steve
```
Aug 18 2008
"Bent Rasmussen" <IncredibleShrinkingSphere Gmail.com> writes:
```As Steven writes, I'd prefer optimal efficiency (who wouldn't). Of course in
a transitional phase dynamic arrays offer the compelling advantage of a
smooth migration path to static arrays. On the other hand I have no idea
when or if static array return types will be allowed and so I may as well
stick with the struct based approach, which have the desired efficiency,
while "waiting for Cousteau". :-)

Bent

"Walter Bright" <newshound1 digitalmars.com> skrev i meddelelsen
news:g8cj44\$1qmf\$2 digitalmars.com...
Bent Rasmussen wrote:
I wanted to test out array operations and create some nice generic
functions for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

Just write it as:

A[] lerp(A)(A t, A[] a, A[] b)
...

```
Aug 18 2008
"Bent Rasmussen" <IncredibleShrinkingSphere Gmail.com> writes:
```I see that the language specification likens tuples to [static] arrays and
states that it may become possible to return tuples from functions, as well
as using operators on them. As such, might one imagine a future with vector
operations on tuples as well? Not that I see any reason to use them over
static arrays per se - array literals are more straight-forward, at least as
it stands now.

Bent

"Walter Bright" <newshound1 digitalmars.com> skrev i meddelelsen
news:g8cj44\$1qmf\$2 digitalmars.com...
Bent Rasmussen wrote:
I wanted to test out array operations and create some nice generic
functions for dealing with vectors. One function is defined as

A[n] lerp(A, uint n)(A t, A[n] a, A[n] b)
{
return a[] + t * (b[] - a[]);
}

Just write it as:

A[] lerp(A)(A t, A[] a, A[] b)
...

```
Aug 18 2008