Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning user-defined types with user-defined functions
#1
I somehow cant seem to return a user-defined type with my functions...
Maybe i've been coding too long but :?
My real goal is to return a reference to my element.

Anyway if i try the following i get "unsuported return type":
Code:
Copy      Help
function'tJobArrayElement int'iJobNr
int iLoop

for iLoop 0 JobSystem.JobArray.len
,if(iJobNr = JobSystem.JobArray[iLoop].nr)
,,break
;;;end-for
ret JobSystem.JobArray[iLoop]

And when i use either "*" or "&" or combinations of these 2 with the ', i get a syntax error when i try to compile it (CTRL+SHIFT+R)
This is what i would like to work:
Code:
Copy      Help
function&tJobArrayElement int'iJobNr
int iLoop

for iLoop 0 JobSystem.JobArray.len
,if(iJobNr = JobSystem.JobArray[iLoop].nr)
,,break
;;;end-for
ret &JobSystem.JobArray[iLoop]

And here is a cut-out version of the user-defined types used:
Code:
Copy      Help
type tJobArrayElement
,int'nr
,str'name

type tJobSystem
,ARRAY(tJobArrayElement)JobArray

tJobSystem+ JobSystem

So now the question is:
Am i doing something wrong because too much codeing in a session?
Or did i find another bug? (Because documentation says its posible)
Well thats all for now, 3M
#2
A function can return only types of size 1, 2, 4, 8, 16. To return an user-defined type, use reference argument.

This function returns copy of JobSystem.JobArray[iLoop]:

Code:
Copy      Help
function int'iJobNr tJobArrayElement&retVal
int iLoop

for iLoop 0 JobSystem.JobArray.len
,if(iJobNr = JobSystem.JobArray[iLoop].nr)
,,break
;;;end-for
retVal=JobSystem.JobArray[iLoop]

Calling:
Code:
Copy      Help
tJobArrayElement je
Func(1 je)
;;;now, manipulating je does not affect JobSystem.JobArray[iLoop]

----

This function returns address of JobSystem.JobArray[iLoop]:

Code:
Copy      Help
function'tJobArrayElement* int'iJobNr
int iLoop

for iLoop 0 JobSystem.JobArray.len
,if(iJobNr = JobSystem.JobArray[iLoop].nr)
,,break
;;;end-for
ret &JobSystem.JobArray[iLoop]

Calling:
Code:
Copy      Help
tJobArrayElement& r=Func(1)
;now, when you manipulate r, you actually manipulate JobSystem.JobArray[iLoop]

Of course, you could not return pointer to local variable.
#3
Aha....so the type of usertype variables in function return should be placed after the typename? hmmm
Thats a inconsistency i didnt know :lol:

So if i want to return a reference like original idea i would use this correct?
Code:
Copy      Help
function'tJobArrayElement& int'iJobNr
int iLoop

for iLoop 0 JobSystem.JobArray.len
,if(iJobNr = JobSystem.JobArray[iLoop].nr)
,,break
;;;end-for
ret JobSystem.JobArray[iLoop]

And able to use like:
Code:
Copy      Help
FindJob(1).name

Sorry for not understanding, i should have posted this in programming instead of general (bug) forum...
Well thats all for now, 3M
#4
no, qm does not support returning references, as well as Func().member syntax. Use the second example. In QM to initialize a reference variable, can be used either variable or pointer at the right side:

TYPE& r=var
same as
TYPE& r=&var

so, a function can return a pointer, and returning reference is not needed (function'tJobArrayElement& actually would be the same as function'tJobArrayElement*).
#5
hmm ok...ty
Too bad it dont sopport returning references and that function().name syntax.
Will that become available at some point?
Because it has potential good and fast usage posibilities :wink:
Well thats all for now, 3M
#6
Now it is not in my plans.
#7
ok np
Gintaras Wrote:Your avatar is not displayed.
I was wondering about that also...
I dont see anyones avatars...

(PS: did something went wrong just now because my last post dissapeared together with yours)
Well thats all for now, 3M
#8
I deleted my post and accidentally yours, sorry.

Only 4 or 5 members of this forum have avatars. Other avatars are displayed, your is not accessible.
#9
Ahh ok np...we all human hehehe
not accesible problem of my avatar is corrected now :wink:
Well thats all for now, 3M
#10
yes, I see


Forum Jump:


Users browsing this thread: 1 Guest(s)