Thread overview  


August 18, 2008 generic array functions and vector operations  

 
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 
August 18, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Bent Rasmussen  "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

August 18, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Steven Schveighoffer  "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 syntaxwise 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 structbased 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 > > 
August 18, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Bent Rasmussen  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)
...

August 18, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Walter Bright  "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 nonheap performance benefits (and value semantics)?
Steve

August 19, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Walter Bright  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) > ... 
August 19, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Walter Bright  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 straightforward, 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) > ... 
August 19, 2008 Re: generic array functions and vector operations  

 
Posted in reply to Bent Rasmussen  "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 nonzero) 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. 
Copyright © 19992016 by Digital Mars ®, All Rights Reserved