Topic
  • 4 replies
  • Latest Post - ‏2013-11-11T09:22:19Z by M_vdLaan
M_vdLaan
M_vdLaan
27 Posts

Pinned topic Returning generic data types

‏2013-11-05T14:36:33Z |

Hi all,

I'm trying to get the following test2 function to work like the Array get function; i.e. a function that can return any type that is cast to the correct type when calling the function. What do I need to change. As you can see, test1 works, but I can't get it to work as a function.

Array ar = create(1,1)
string s = "String"
string r
int i = ((addr_ s) int)
put(ar, i, 0, 0)

_m test2(int x, y)
{
int d = (int  get(ar, x, y))
return ((addr_ d) _m)
}

_m test1 = (_m get(ar, 0, 0))

r = (string test1)
print r "\n"

r = (string test2(0,0))
print r "\n"

delete ar

The line calling the test2 function results in an "unassigned variable reference" error.

 

Any help is appreciated.

Regards,
M_vdLaan

Updated on 2013-11-05T14:38:17Z at 2013-11-05T14:38:17Z by M_vdLaan
  • GothSloth
    GothSloth
    20 Posts

    Re: Returning generic data types

    ‏2013-11-05T16:43:43Z  

    M_vdLaan,

    I have tried this in the past with no luck. I played with it again and still no luck. Please see the following threads:

    https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014481996

    https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014476822#77777777-0000-0000-0000-000014477080

     

    GothSloth

  • adevicq
    adevicq
    154 Posts

    Re: Returning generic data types

    ‏2013-11-06T08:36:20Z  
    • GothSloth
    • ‏2013-11-05T16:43:43Z

    Hi,

    Have you tried with a DxlObject?

     

    {code}

    DxlObject d = new
    d->"data" = "lorem ipsum"
    string s
    s = (string d->"data")
    print s
    delete d

    {code}

    This can be a solution. With the disadvantage to have to free the memory...

    Alain

     

     

  • Mathias Mamsch
    Mathias Mamsch
    1970 Posts

    Re: Returning generic data types

    ‏2013-11-06T13:39:41Z  

    You need to use reference parameters to have a function return multiple types. The DXL Interpreter does not handle return values well and does not work at all for returning underscore types. So do something like this:

    Array ar = create(1,1)
    put(ar, "String", 0, 0)

    void test2(int x, int y, _m &result)
    {
      int &ref = addr_ result
      ref = (int  get(ar, x, y))
    }

    string test2String(int x, int y) { string r = null; test2(x,y,r); return r }
    int    test2Int   (int x, int y) { int    r = null; test2(x,y,r); return r }

    string r = null;
    test2(0,0, r)

    print "With a variable only a minor syntax difference:" r "\n"
    print "If you need embedding you need to declare helpers: " (test2String(0,0)) "\n"

    delete ar

    Regards, Mathias

  • M_vdLaan
    M_vdLaan
    27 Posts

    Re: Returning generic data types

    ‏2013-11-11T09:22:19Z  

    You need to use reference parameters to have a function return multiple types. The DXL Interpreter does not handle return values well and does not work at all for returning underscore types. So do something like this:

    Array ar = create(1,1)
    put(ar, "String", 0, 0)

    void test2(int x, int y, _m &result)
    {
      int &ref = addr_ result
      ref = (int  get(ar, x, y))
    }

    string test2String(int x, int y) { string r = null; test2(x,y,r); return r }
    int    test2Int   (int x, int y) { int    r = null; test2(x,y,r); return r }

    string r = null;
    test2(0,0, r)

    print "With a variable only a minor syntax difference:" r "\n"
    print "If you need embedding you need to declare helpers: " (test2String(0,0)) "\n"

    delete ar

    Regards, Mathias

    Okay,

     

    Thanks all. So to summarize, it's not (reliably) possible to create a function similar to the Array get() function.

    I am aware of the alternatives (the _m parameter as used by the Skip find() function and the DxlObject field return method).

    Since I was playing around with an Array wrapper, I wanted to use a wrapper around the Array get() function.

     

    Guess I'll work around this.

     

    Thanks,

    M_vdLaan