digitalmars.D.bugs - Aliasing a property
- Florian Sonnenberger (19/20) Nov 04 2005 I'd like to do the following:
- BCS (9/29) Nov 04 2005 I think length is a value. This is not to say that the compiler couldn't...
- Florian Sonnenberger (23/73) Nov 05 2005 I've ever thought array.length was a function that calls some internal
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
Nov 04 2005
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
Nov 04 2005
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'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, FlorianI'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
Nov 05 2005