Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

Atrribute Introspect shape #6197

Open p6rt opened 7 years ago

p6rt commented 7 years ago

Migrated from rt.perl.org#131174 (status was 'open')

Searchable as RT131174$

p6rt commented 7 years ago

From @dwarring

AFAIK there's currently no way of introspecting the shape of a Class attribute that's a native shaped array.

I'd like this feature for the Native​::Packing module. For example to interpret​:

class C {   has byte @​.gif-header[6];   has uint16 $.width;   has uint16 $height; }

At the moment, I just can't determine the size of shaped native arrays, such as @​.gif-header above​:

say .type, .container   for C.^attributes;

(Positional[byte])(array[byte]) (uint16)(uint16) (uint16)(uint16)

p6rt commented 7 years ago

From @zoffixznet

On Tue, 18 Apr 2017 20​:34​:15 -0700, david.warring wrote​:

AFAIK there's currently no way of introspecting the shape of a Class attribute that's a native shaped array.

I'd like this feature for the Native​::Packing module. For example to interpret​:

class C { has byte @​.gif-header[6]; has uint16 $.width; has uint16 $height; }

At the moment, I just can't determine the size of shaped native arrays, such as @​.gif-header above​:

say .type, .container for C.^attributes;

(Positional[byte])(array[byte]) (uint16)(uint16) (uint16)(uint16)

Seems .container does have method .shape, but it returns a Whatever​:

class C {   has byte @​.gif-header[6];   has uint16 $.width;   has uint16 $height; } say .container.shape for C.^attributes[0]; # (*) dd C.new.gif-header.shape; # (6,)

my byte @​z[6]; dd @​z.shape; # (6,)

p6rt commented 7 years ago

The RT System itself - Status changed from 'new' to 'open'

p6rt commented 7 years ago

From @dwarring

I've added a fudged test to S12-introspection/attributes.t that the container shape is as declared; not a Whatever.

On Wed, 19 Apr 2017 03​:14​:35 -0700, cpan@​zoffix.com wrote​:

On Tue, 18 Apr 2017 20​:34​:15 -0700, david.warring wrote​:

AFAIK there's currently no way of introspecting the shape of a Class attribute that's a native shaped array.

I'd like this feature for the Native​::Packing module. For example to interpret​:

class C { has byte @​.gif-header[6]; has uint16 $.width; has uint16 $height; }

At the moment, I just can't determine the size of shaped native arrays, such as @​.gif-header above​:

say .type, .container for C.^attributes;

(Positional[byte])(array[byte]) (uint16)(uint16) (uint16)(uint16)

Seems .container does have method .shape, but it returns a Whatever​:

class C { has byte @​.gif-header[6]; has uint16 $.width; has uint16 $height; } say .container.shape for C.^attributes[0]; # (*) dd C.new.gif-header.shape; # (6,)

my byte @​z[6]; dd @​z.shape; # (6,)

dwarring commented 2 years ago

Rechecking as of 2022.03. The example in this is now a syntax error (with both has and HAS variants):

$ raku -v
Welcome to Rakudo™ v2022.03-127-gbb9d7497b.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.03-13-ga3476e286.
$ raku -e'class C { has byte @​.gif-header[6];}'
===SORRY!=== Error while compiling -e
Cannot declare an anonymous attribute
at -e:1
------> class C { has byte @⏏​.gif-header[6];}
    expecting any of:
        constraint
$ raku -e'class C { HAS byte @​.gif-header[6];}'
===SORRY!=== Error while compiling -e
Cannot declare an anonymous attribute
at -e:1
------> class C { HAS byte @⏏​.gif-header[6];}
    expecting any of:
        constraint
dwarring commented 2 years ago

New formulation, based on this this SO question:

use NativeCall;
class C is repr('CStruct') {
    HAS int8 @.gif-header[6] is CArray;
    has int32 $.width;
    has int32 $.height;
}

say nativesizeof(C);
dd C.new.gif-header.elems; # 6

nativesizeof(C) now evaluates correctly, but C.new.gif-header.elems is zero.

So, I'm still not sure of a way of introspecting the gif-header array length in isolation.