Topic
  • 2 replies
  • Latest Post - ‏2013-05-27T12:41:54Z by mdiin
mdiin
mdiin
4 Posts

Pinned topic The difference between handle and type?

‏2013-05-18T10:34:42Z |

Hi.

I am trying to understand how PL/I handles defined structures, i.e. how to use a stronger typing discipline in my PL/I programs. When I define a named type using define structure I have two ways to use that type in my code:

  • Using a handle, e.g. dcl foo handle Bar
  • Using type, e.g. dcl foo type Bar

It seems I cannot use type when I want to create a recursive type such as List:

define structure 1 List;

define structure 1 List,

  2 head pointer,

  2 tail handle List;

However, in most other situations I cannot really tell when to use one over the other. I'm guessing there must be a difference between when memory is allocated, because handles are typed pointers, but I'm not really sure.

I have read the relevant sections of the language reference a number of times, but I have not been able to find a straight answer, better than that handles are typed pointers. I hope someone here can help clarify two things for me:

  1. When to use one over the other
  2. The difference between the two in terms of what the compiler does

I asked this question on twitter to @IBM_compilers and was in turn asked to ask my question here.

  • pelderon
    pelderon
    11 Posts

    Re: The difference between handle and type?

    ‏2013-05-24T23:43:31Z  

    Define type is just a way to give a name to a set of variables, for example

        define alias int fixed bin(31);

    this could also be done by using the macro facility, for example

        %dcl int char;

         %int = 'fixed bin(31)';

     

    Handles are a form of strongly-typed pointers which must point to a defined structure. Handles consume the same amount as pointers, but unlike pointers, handle can dereference only the structure they point to, and can be compared to and assigned to only handles pointing to the same structure type.

  • mdiin
    mdiin
    4 Posts

    Re: The difference between handle and type?

    ‏2013-05-27T12:41:54Z  
    • pelderon
    • ‏2013-05-24T23:43:31Z

    Define type is just a way to give a name to a set of variables, for example

        define alias int fixed bin(31);

    this could also be done by using the macro facility, for example

        %dcl int char;

         %int = 'fixed bin(31)';

     

    Handles are a form of strongly-typed pointers which must point to a defined structure. Handles consume the same amount as pointers, but unlike pointers, handle can dereference only the structure they point to, and can be compared to and assigned to only handles pointing to the same structure type.

    I was not talking about define type, but rather using type when referring to a defined structure, like so:

    define structure 1 List;

    define structure 1 List,

      2 head pointer,

      2 tail handle List;

    declare aList type List;

    Talking to one of my colleagues, I think I have figured out what the semantics of the above declare statement is: aList is allocated on the stack and given the amount of memory equal to size(:List:).

    When using pointers, and thus also handles, you have to be aware of the difference between the stack and the heap. When creating a new handle using the e.g. new(:List:), heap memory corresponding to the size of List is allocated, and the handle points to this. But when using the handle built-in function to convert aList to a handle(List), the handle points to the location of aList on the stack. The pitfall here is that you cannot return the created handle from a function and still expect to access aList's data - it will be destroyed upon exit from the block in which it was allocated.

    So, my conclusion is that handles are to be used when creating recursive data types, and in most other situations one should use declare foo type List.