PL/I

PL/I

PL/I

 View Only
  • 1.  EPLI V3R4 question: "generic" as element of struct

    Posted Tue November 25, 2014 05:18 AM

    [This is a crosspost from comp.lang.pl1, it came into my mind after posting there that ibm dw might be a better place to ask.]

    Hi, 

    in my understanding "generic" allows to declare entries that can handle different types of parameter, e.g. 

    /* the entries */ 
    dcl foo_c entry(char(*)) returns(boolean); 
    dcl foo_df entry(dec fixed(5) returns(boolean); 
    /* the generic */ 
    dcl foo generic(foo_c when(char), foo_df when(dec fixed(5)) ); 
    /* usage */ 
    if foo(3)  /* calls foo_df */ 
     ! foo("3")  /* calls foo_c */ 
    then ... 

    i was wondering if / how it is possible to use "generic" in a struct similar to a entry (i failed until now). i was thinking about 

    dcl 1 foo, 
          2 bar generic( <similar to above> ); 
    /* and using */ 
    if foo.bar(3) .... 

    any idea how to solve? or explanation why it's not possible? or is it just a compiler restriction without special reason? 

    br johann 

    woecki


  • 2.  Re: EPLI V3R4 question: "generic" as element of struct

    Posted Tue November 25, 2014 02:56 PM

    Hi Johann,

    GENERIC is an attribute of an Entry declaration.  It is specific to creating functions that can act on various types of parameters.

    In a record structure, PL/I needs to know how much storage to allocate when you instantiate the record variable.  A truly generalized type definition would need to dynamically allocate storage based on the parameter given.  But you can probably achieve something similar using a UNION. Perhaps something like:

    dcl 1 foo,

         2 bar union,

               3 bar_as_char char(8),

               3 bar_as_dec fixed dec(15,2),

               3 bar_as_bits bit(64)

    ;

    foo.bar_as_dec = -100.23;

    put skip list(foo.bar_as_bits(63));

    Hope that helps!

    PeteKidwell


  • 3.  Re: EPLI V3R4 question: "generic" as element of struct

    Posted Wed November 26, 2014 02:34 AM

    Hi Pete,

    Thanks for the quick reply! Seems i was unclear in my question: i don't want to create a generic variable, i want to replace an "entry var" with generic.
    Background: i'd like to create a "module" that has data + entries (am i hearing anybody mumbling "oop" :-)?). 
    This is perfectly possible with PL/1:

    /* just a sample */
    /* declare */
    dcl 1 employee,
          2 id dec fixed(5),
          2 name char(20),
          2 birthday dec fixed(9),
          2 find_employee_by_name entry(char(*)) returns(dec fixed(5)),
          2 find_employee_by_birthday entry(dec fixed(9)) returns(dec fixed(5));
    /* assign the entries */
    employee.find_employee_by_name = the_func_that_implements_the_search_by_name;
    employee.find_employee_by_birthday = the_func_that_implements_the_search_by_birthday;

    dcl x_emp dec fixed(5);
    /* use/call the entries */
    x_emp = employee.find_employee_by_name("john doe");
    x_emp = employee.find_employee_by_birthday(19800101);

    /* what i want to achieve */

    x_emp = employee.find_employee("john doe");

    The "generic works perfect "outside" a struct:

    /* a generic */dcl find_employee generic(the_func_that_implements_the_search_by_name when(char),                          the_func_that_implements_the_search_by_birthday when(dec fixed(9)   );/* using the generic */x_emp = find_employee("john doe"); /* calls "the_func_that_implements_the_search_by_name" */

    But i was not able to achieve the same as part of the struct. 
    so there is a workaround, but i'm curious if there's a way to do it "right".

    br johann

    woecki


  • 4.  Re: EPLI V3R4 question: "generic" as element of struct

    Posted Wed November 26, 2014 02:54 PM

    Re-reading and -thinking my own posting i came to the following idea:

    - An "entry var" is a function pointer that can have different values during execution of a program and it's current value is determined at runtime.

    - A "generic" might get resolved at compiletime to the "when" entry that matches the given parameter list.

    If those two assumptions are right, i understand that "generic" and "entry var" are not related and "generic" won't work in a struct.

    Would be interesting if someone with a deeper insight can confirm my assumptions,

    br johann

    woecki


  • 5.  Re: EPLI V3R4 question: "generic" as element of struct

    Posted Tue December 02, 2014 02:59 PM

    In the long-ago days before OOP we had Abstract Data Types.  An ADT defined a data structure and the functions that worked on it, not as members per se, but as functions that accepted the data structure as a parameter.

    All of which is a long way of saying, your "workaround" is exactly the sort of solution I'd suggest. :)

    You could formalize it a bit more by making the employee a typed structure, i.e.

    define structure 1 Employee,
          2 id dec fixed(5),
          2 name char(20),
          2 birthday dec fixed(9)

    ;

    dcl (X, Y) type Employee;

    X = Find_Employee('John Doe');

    Y = Find_Employee(75352);

    But it's essentially the same thing you have above.

    PeteKidwell