Read-only forum archive

Are packages like C structs?

Are packages like C structs?

Chris Davis · Sun Apr 12, 2009 11:10 pm

It seems like I should be able to treat a package data type like a C struct, but when I return a package from a procedure, I can't access any of its nested identifiers. I've also tried using importfrom(package,identifier), but that doesn't work either. Here's an example.

Code:
> proc gimmePackage()
. {
.    package apack;
.    int apack::anum = 1;
.
.    return(apack);
. }
>
> package testpack = gimmePackage();
> listvar(testpack);
// testpack             [0]  package (N)
// ::anum               [1]  int 1
>
> testpack::anum;
   ? `anum` is undefined
   ? error occurred in STDIN line 95: `testpack::anum;`
>
> importfrom(testpack,anum);
   ? `anum` not found in `testpack`
   ? error occurred in STDIN line 96: `importfrom(testpack,anum);`
>
> importfrom(testpack,::anum);
   ? error occurred in STDIN line 97: `importfrom(testpack,::anum);`
   ? last reserved name was `importfrom`
   skipping text from `anum` error at token `)`
>
> importfrom(testpack,testpack::anum);
   ? `anum` not found in `testpack`
   ? error occurred in STDIN line 98: `importfrom(testpack,testpack::anum);`


It also seems that poly data types can't be stored in (user-defined) packages. Is that correct?

Any help is greatly appreciated!

Re: packages are not like C structs

hannes · Thu May 21, 2009 2:17 pm

packages are not like C structs, more like a C++ name space.
It should (and is ) used to avoid name collisions between different
libraries (and the Top level).
And, yes, they contain only types which are ring independent.
Ring depended types (like poly) are contain in their base rings,
#which are quite similiar to packages.