-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+New hb_AUnpack() #232
base: master
Are you sure you want to change the base?
+New hb_AUnpack() #232
Conversation
hb_AUnpack will assign all the element of the first parameter if is an array, to following parameters passed by reference. This provides the concept of "unpacking" an array as used in the Python language. This function is very useful when the first parameter is a function returning an array. For example you could return {<lResult>,<cErrorMessage>} if !hb_AUnpack(FunctionReturningAnArray(),,@cLastError) ?"Error Occurred calling FunctionReturningAnArray:",cLastError endif Will return the first element of the array as well as placing it in 2nd parameter, if specified by reference.
This will help handle the message https://groups.google.com/g/harbour-users/c/EuRbfhmI3TQ |
@EricLendvai This is cool. Do you have an idea for syntax to allow partial unpacking, cf. in Python: a, b, *rest = (1, 2, 3, 4, 5) |
@tuffnatty I could maybe implement the partial unpacking as an enhancement. I would only have to test that the last variable is an array and that the first array has unused element. By the way, this unpacking could also be used to mimic the Python "for loop" where more than one variable receives values ... |
PROC Main
LOCAL nTmp
hb_ExecFromArray( {| p1, p2, p3, ... | nTmp := p1, QOut( p2, p3, ... ) }, GenArray() )
? nTmp
FUNC GenArray
RETURN { 10, 9, 8, 7, 6 } example with partial unpacking too: PROC Main
LOCAL nTmp, aTmp
hb_ExecFromArray( {| p1, p2, p3, ... | nTmp := p1, QOut( p2, p3, ... ), aTmp := { ... } }, GenArray() )
// or
aTmp := hb_ExecFromArray( {| p1, p2, p3, ... | nTmp := p1, QOut( p2, p3, ... ), { ... } }, GenArray() )
? nTmp
? hb_ValToExp( aTmp ) |
As shown before, hb_AUnpack() can be achieved using hb_ExecFromArray(). |
Interesting additions.
In the part, when needing a function to return multiple values, I used to
return an array and "unpack" it.
Recently I had to update a function to return 2 values, whose second value
was of interest in just a couple of calls, and decided to use a @ second
parameter.
Using the preprocessor you may create a new language, new syntax....really
powerfull and, at the same time, really dangerous.
Il Lun 19 Ago 2024, 17:03 alcz ***@***.***> ha scritto:
… As shown before, hb_AUnpack() can be achieved using hb_ExecFromArray().
***@***.***
<alcz@c6a897c>
link shows how to make a UNPACK command out idea shown before
hb_AUnpack() could be implemented, but here I'm not sure if the syntax
performs the job of readabilty. Not closing, hopefully i bump the
discussion how to work out function arguments,to get partial unpacking out
of such function.
—
Reply to this email directly, view it on GitHub
<#232 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA6LSY34F3N44CPI6PTVYRLZSICL3AVCNFSM6AAAAABMYARMTCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJWG44TQOJZGQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
hb_AUnpack will assign all the element of the first parameter if is an array, to following parameters passed by reference.
This provides the concept of "unpacking" an array as used in the Python language.
This function is very useful when the first parameter is a function returning an array.
For example you could return {,}
if !hb_AUnpack(FunctionReturningAnArray(),,@cLastError)
?"Error Occurred calling FunctionReturningAnArray:",cLastError
endif
Will return the first element of the array as well as placing it in 2nd parameter, if specified by reference.