www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Aliasing a property

reply Florian Sonnenberger <nairolf online.de> writes:
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
parent reply BCS <BCS_member pathlink.com> writes:
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
parent Florian Sonnenberger <nairolf online.de> writes:
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
Nov 05 2005