Thread overview | |||||
---|---|---|---|---|---|
|
November 04, 2005 Aliasing a property | ||||
---|---|---|---|---|
| ||||
I'd like to do the following: struct foo { int[] bar; alias bar.length size; // Line 19 } So that I can get the length of someFoo.bar with someFoo.size . This should be ok, since www.digitalmars.com/d/declaration.html says: > A symbol can be declared as an alias of another symbol. (...) A property is actually a function, and functions are symbols. But the above code gives me these errors: foobar.d(19): no property 'length' for type 'int[]' foobar.d(19): bar.length is used as a type The first one is somewhat strange. This doesn't only happen if declared in a struct, it was just an example. Bug? Not yet implemented? Will never get implemented? Thanks, Florian |
November 05, 2005 Re: Aliasing a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Florian Sonnenberger | I think length is a value. This is not to say that the compiler couldn't make a function to get the value. try this: struct foo { int[] bar; typeof(bar.length) size() {return bar.length;} } In article <dkgoan$2vhe$1@digitaldaemon.com>, Florian Sonnenberger says... > >I'd like to do the following: > >struct foo >{ > int[] bar; > alias bar.length size; // Line 19 >} > >So that I can get the length of someFoo.bar with someFoo.size . > >This should be ok, since www.digitalmars.com/d/declaration.html says: >> A symbol can be declared as an alias of another symbol. (...) > >A property is actually a function, and functions are symbols. But the above code gives me these errors: > >foobar.d(19): no property 'length' for type 'int[]' >foobar.d(19): bar.length is used as a type > >The first one is somewhat strange. > >This doesn't only happen if declared in a struct, it was just an example. > >Bug? >Not yet implemented? >Will never get implemented? > > >Thanks, >Florian |
November 05, 2005 Re: Aliasing a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS schrieb:
> I think length is a value. This is not to say that the compiler couldn't make a
> function to get the value.
>
>
> try this:
>
> struct foo
> {
> int[] bar;
> typeof(bar.length) size() {return bar.length;}
> }
>
>
>
>
> In article <dkgoan$2vhe$1@digitaldaemon.com>, Florian Sonnenberger says...
>
>>I'd like to do the following:
>>
>>struct foo
>>{
>> int[] bar;
>> alias bar.length size; // Line 19
>>}
>>
>>So that I can get the length of someFoo.bar with someFoo.size .
>>
>>This should be ok, since www.digitalmars.com/d/declaration.html says:
>>
>>> A symbol can be declared as an alias of another symbol. (...)
>>
>>A property is actually a function, and functions are symbols.
>>But the above code gives me these errors:
>>
>>foobar.d(19): no property 'length' for type 'int[]'
>>foobar.d(19): bar.length is used as a type
>>
>>The first one is somewhat strange.
>>
>>This doesn't only happen if declared in a struct, it was just an example.
>>
>>Bug?
>>Not yet implemented?
>>Will never get implemented?
>>
>>
>>Thanks,
>>Florian
>
>
I've ever thought array.length was a function that calls some internal functions of the GC to allocate more memory or mark it as garbage.
But you are right, it's just a uint variable.
Though, alias does also work with variables:
struct foo
{
uint bar;
}
int main( char[][] args )
{
foo someFoo;
alias someFoo.bar qwert;
return 0;
}
No problem with that.
It only seems not to work for those /magic/ properties (or variables) like .length, .sizeof, .min, .max, .alignof, .dup, .init, ...
I first had a wrapper function like yours but I thought the alias methode was better because it doesn't need one extra function call.
BTW, will such small functions get inlined by the compiler?
Thanks,
Florian
|
Copyright © 1999-2021 by the D Language Foundation