Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
September 06, 2015 How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
I am working on a struct vector. The data is stored in a member static array and I want to be able to forward all array properties except length to vector. Reason is I have free functions f that take vector(s) as arguments, such that f(vector) and vector.f via UFCS is possible. Using alias array this in the case of length function/array property is problematic, as length(vector) obviously uses the free function but vector.length the array property. What would be the simplest way to disable the array.length property for the vector struct? I would prefer not to implement length as a vector member function and call it inside the free function as this is inconsistent with the other free funcs. |
September 06, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ParticlePeter | On Sunday, 6 September 2015 at 07:34:36 UTC, ParticlePeter wrote: > I am working on a struct vector. The data is stored in a member static array and I want to be able to forward all array properties except length to vector. > Reason is I have free functions f that take vector(s) as arguments, such that f(vector) and vector.f via UFCS is possible. Using alias array this in the case of length function/array property is problematic, as length(vector) obviously uses the free function but vector.length the array property. > > What would be the simplest way to disable the array.length property for the vector struct? > > I would prefer not to implement length as a vector member function and call it inside the free function as this is inconsistent with the other free funcs. Hi, If you are looking for somethin like delegator from ruby: http://ruby-doc.org/stdlib-2.0.0/libdoc/forwardable/rdoc/Forwardable.html they are in D template proxy; http://dlang.org/phobos/std_typecons.html#.Proxy Or by using mixin delegates: http://forum.dlang.org/post/jitn9v$20u4$1@digitalmars.com |
September 06, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | On Sunday, 6 September 2015 at 08:48:32 UTC, bioinfornatics wrote: > On Sunday, 6 September 2015 at 07:34:36 UTC, ParticlePeter wrote: >> I am working on a struct vector. The data is stored in a member static array and I want to be able to forward all array properties except length to vector. >> Reason is I have free functions f that take vector(s) as arguments, such that f(vector) and vector.f via UFCS is possible. Using alias array this in the case of length function/array property is problematic, as length(vector) obviously uses the free function but vector.length the array property. >> >> What would be the simplest way to disable the array.length property for the vector struct? >> >> I would prefer not to implement length as a vector member function and call it inside the free function as this is inconsistent with the other free funcs. > > Hi, > > If you are looking for somethin like delegator from ruby: http://ruby-doc.org/stdlib-2.0.0/libdoc/forwardable/rdoc/Forwardable.html No, not this one. It is O.k. for vector to be implicitly converted to an array, and in such a case array property length should be used. > Or by using mixin delegates: http://forum.dlang.org/post/jitn9v$20u4$1@digitalmars.com I think this approach (in particular Jacobs suggestion) would be useful if I would like to forward array member properties for lot of different struct/classes but for now it is sufficient for the vector struct. I took a look at opIndex, opSlice, opIndexAssign and opIndexOpAssign but find the examples very confusing. Also its not clear for me which of these operators I have to implement to have full array functionality on the vector struct. In the end all that I want is "just" to disable access to array.length through vector and alias this array. |
September 06, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ParticlePeter | Untested: struct Vector(T) { T[42] data; auto opDispatch(string func, Args...)(Args args) if(is(typeof(mixin("data."~func)(Args.init))) && func != "length") { return mixin("data."~func)(Args.init); } } |
September 06, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 6 September 2015 at 10:24:25 UTC, Marc Schütz wrote:
> Untested:
>
> struct Vector(T) {
> T[42] data;
>
> auto opDispatch(string func, Args...)(Args args)
> if(is(typeof(mixin("data."~func)(Args.init))) && func != "length")
> {
> return mixin("data."~func)(Args.init);
> }
> }
Unfortunately not working, afaics the built in static array does not have the property opIndex et al. so they cannot be forwarded, right?
|
September 07, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ParticlePeter | On 9/6/15 3:34 AM, ParticlePeter wrote:
> I am working on a struct vector. The data is stored in a member static
> array and I want to be able to forward all array properties except
> length to vector.
> Reason is I have free functions f that take vector(s) as arguments, such
> that f(vector) and vector.f via UFCS is possible. Using alias array this
> in the case of length function/array property is problematic, as
> length(vector) obviously uses the free function but vector.length the
> array property.
>
> What would be the simplest way to disable the array.length property for
> the vector struct?
>
> I would prefer not to implement length as a vector member function and
> call it inside the free function as this is inconsistent with the other
> free funcs.
struct vector(T)
{
T[] _buf;
alias _buf this;
auto length() { return .length(this);}
}
-Steve
|
September 08, 2015 Re: How to partially forward properties of struct array member to struct (disable length property) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ParticlePeter | On Sunday, 6 September 2015 at 10:12:58 UTC, ParticlePeter wrote:
> In the end all that I want is "just" to disable access to array.length through vector and alias this array.
struct Vec(T, size_t n = 3)
{
T[n] data;
alias data this;
@disable @property size_t length() const;
}
void main()
{
Vec!int v;
v[0] = 1; // ok
assert(v[0] == 1); // ok
int n = v.length; // error
}
- Kenji
|
Copyright © 1999-2021 by the D Language Foundation