PL/I for AIX

 View Only
Expand all | Collapse all

The difference between handle and type?

  • 1.  The difference between handle and type?

    Posted Sat May 18, 2013 06:35 AM

    Originally posted by: mdiin


    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.



  • 2.  Re: The difference between handle and type?

    Posted Fri May 24, 2013 07:44 PM

    Originally posted by: pelderon


    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.



  • 3.  Re: The difference between handle and type?

    Posted Mon May 27, 2013 08:42 AM

    Originally posted by: mdiin


    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.