Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

EricLendvai
Copy link

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.

   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.
@EricLendvai
Copy link
Author

This will help handle the message https://groups.google.com/g/harbour-users/c/EuRbfhmI3TQ
See the commit note for an example.

@tuffnatty
Copy link
Member

@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)

@EricLendvai
Copy link
Author

@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 ...

@alcz
Copy link
Contributor

alcz commented Apr 19, 2021

hb_ExecFromArray() does this sort of things, especially when codeblock is the first parameter

PROC Main
   LOCAL nTmp
   hb_ExecFromArray( {| p1, p2, p3, ... | nTmp := p1, QOut( p2, p3, ... ) }, GenArray() )
   ? nTmp

FUNC GenArray
   RETURN { 10, 9, 8, 7, 6 }

https://os.allcom.pl/hb/3.2/#!eJwLCPJ3VvBNzMzjUlBQ8PF3dvRRyAvJLQDxMpLiXStSk92K8nMdi4oSKzUUqmsUCgx1FAqMgNhYR0FPT0-hBqxcwcoWLBPoX1qigSKvqVCro-CemgcxQVNBE2SyPcQOLrdQP2e4JEgiyDUkNMhPoVrB0EBHwVJHwUJHwVxHwUyhlgsALM0r5A

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 )

https://os.allcom.pl/hb/3.2/#!eJxVjcsKgkAUhvfzFP_SgYNkQTeIENE2piXqNqYQClIHKTDUd28uELQ4h8N_-c4pSwMcxaNhAOI08GM0eS0JQm2t3a-XsK9uUdfWfteJj4NhhPQIcq5mQXBdF6MpYbszzjl9v5w_n1ueDgxGmICJcKgay-Tg-tfeYOyl_pbimbdhLx1b5oxFRRL8WjqXhXmRJQrqzQgbwpqwIiwxsS9DLDlh

@alcz
Copy link
Contributor

alcz commented Aug 19, 2024

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.

@fperillo
Copy link
Contributor

fperillo commented Aug 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants