Jump to content

Implement optional ByRef parameters for functions


Recommended Posts

Hello, a while back I created ticket #3540 in the AutoIt Bug Tracker:

Quote

Hello, I know that this had been already bought up in ticket #2360 but I am going to re-request the feature with proper reasoning and support.

I would like to see optional ByRef parameters work in AutoIt. They would function something like this: If a function has been called which contains an optional ByRef parameter, the ByRef parameter should act as a "normal" non-ByRef parameter unless it has been specified in the function call.

There are several advantages to this approach:

  1. It is fully backward compatible
  2. Several functions which use ByRef parameters can be made optional
  3. This solves the problem of creating "dummy" variables which are just created for the sake of passing them to ByRef parameters.

I think this is relatively easy to implement and won't have any side effects unlike most feature requests.

If we have nothing to lose but except gain, why don't we do it? ;)

Here is some proof-of-concept code:

FuncWithOptionalByRef()
FuncWithOptionalByRef("Foo")

Func FuncWithOptionalByRef(ByRef $vFoo = "Bar")
    ConsoleWrite($vFoo & @CRLF)
EndFunc

Expected Output after the implementation of this feature:

Bar
Foo

In the latest version, the PoC code won't work because of an error: "Badly formatted function"

I just wanted to bring it back up again for discussion here, any feedback is appreciated, what do you think about it?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

At the very least that would possibly violate the rules about scope of function arguments. A reference currently implies that the variable exists at the time of invokation, having its own predefined scope and attributes (Const, Static). Making the argument optional might be used, but then what should be the scope and attributes of the optional argument created on the fly?

All in all that seems (to me) just good to save a single line of code (having to declare a transient variable) in simple use cases, but might lead to sneaky issues in more involved situations.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

46 minutes ago, jchd said:

At the very least that would possibly violate the rules about scope of function arguments. A reference currently implies that the variable exists at the time of invokation, having its own predefined scope and attributes (Const, Static). Making the argument optional might be used, but then what should be the scope and attributes of the optional argument created on the fly?

All in all that seems (to me) just good to save a single line of code (having to declare a transient variable) in simple use cases, but might lead to sneaky issues in more involved situations.

I don't see any violation of anything. And getting out of bed might lead to sneaky issues too, just as staying in.

All in all... nothing. This is sensible request already implemented, but not allowed, for unknown reason.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

1 hour ago, jchd said:

At the very least that would possibly violate the rules about scope of function arguments.

How?? :blink:

2 hours ago, jchd said:

A reference currently implies that the variable exists at the time of invokation

2 hours ago, jchd said:

Making the argument optional might be used, but then what should be the scope and attributes of the optional argument created on the fly?

My answer :D:

3 hours ago, TheDcoder said:

the ByRef parameter should act as a "normal" non-ByRef parameter unless it has been specified in the function call.

 

2 hours ago, jchd said:

All in all that seems (to me) just good to save a single line of code (having to declare a transient variable)

Something like a dummy variable for the ByRef parameter? Not only is it a waste of memory and a line, but it is also a bad practice ;)

2 hours ago, jchd said:

might lead to sneaky issues in more involved situations.

Issue like what? :huh:

3 hours ago, TheDcoder said:

It is fully backward compatible

3 hours ago, TheDcoder said:

...this is relatively easy to implement and won't have any side effects...

 

Thanks @trancexx for your response. I hope I have cleared all of your concerns @jchd :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Ah? When will the optional object be destroyed? Is it at function exit, giving a scope error, or will that silently create a global variable?

 

$vMyVar = GetThing()
ConsoleWrite($vMyVar.More() & @LF)

Func GetThing(ByRef $vFoo = ObjCreate(<blah>))
    ; optional action on $vFoo
    Return $vFoo.Something()
EndFunc

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

6 hours ago, TheDcoder said:

.... any feedback is appreciated, what do you think about it?

my 2 cents, ( might as well be 1 cent ). ByRef will change the value of the referenced variable. That is understood. What is not understood to me, is a real life use of such an implementation, given that, one can handle the referenced variable as one'd like within the function and, to a dyslexic like me, your request, would make life code difficult to troubleshoot ( as I create more bugs than operational code most of the time ). So I, given the option to choose either way, would prefer it as is. What if I need an empty string ?, would it be changed to the default value ? Will creation of a variable be by default NUL without a value assignment ?. Truly, I'd live it as is.

Link to comment
Share on other sites

what do you thing .... it can be pretty useful I think

Is not pretty useful.

Just now, TheDcoder said:

If you don't like this feature, then don't use it ;)

It's not the point. Your trend of thought is not sound to me but this is more than the 2 cents I offered. 
Know that you asked " any feedback is appreciated, what do you think about it? ", and I did just that.

Link to comment
Share on other sites

It's generic. Some submethod from, say, an Excel sheet or the like.

The first question is: "would the assignment before this line work or not?"

I'm not at all against the feature but it looks a bit unclear as is to my aged eyes.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

2 minutes ago, argumentum said:

Know that you asked " any feedback is appreciated, what do you think about it? ", and I did just that.

I appreciate your feedback, but my reply is a solution for your concern :).

2 minutes ago, jchd said:

It's generic.

I wanted to know the datatype of the evaluated "information"

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

2 hours ago, jchd said:

Ah? When will the optional object be destroyed? Is it at function exit, giving a scope error, or will that silently create a global variable?

 

$vMyVar = GetThing()
ConsoleWrite($vMyVar.More() & @LF)

Func GetThing(ByRef $vFoo = ObjCreate(<blah>))
    ; optional action on $vFoo
    Return $vFoo.Something()
EndFunc

 

 

Uhm... Objects use their own counting scheme, unrelated to AutoIt. If creating .Something() object increases reference count for object behind $vFoothen that object is alive until object behind $vMyVar is destroyed. Of course that reference count for object behind $vFoo is decreased when GetThing() returns. If that results in 0 reference count the object will destroy itself. AutoIt losses reference to that object anyhow, because it's declared as local.

How is your dilemma related to optional Byref isn't all that clear to me.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

2 hours ago, argumentum said:

Is not my concern, is my opinion to your concern.

:)

Looks like @trancexx knows how to address @jchd's example, thanks for the help again :thumbsup:

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

3 hours ago, trancexx said:

How is your dilemma related to optional Byref isn't all that clear to me.

That's because I didn't realize at once that even if AutoIt loses reference to the main object going out of local scope, it still exists thanks to it's internal reference count.

Sorry for the noise.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

As a feature, I don't believe the potential benefit should be ignored. With @jchd's example, I envisage that the object would be destroyed and the reference fail. This might not be the entirely correct, but it's how it looks to me, and this is also pretty much what @trancexx said.

Edited by czardas
Link to comment
Share on other sites

2 minutes ago, czardas said:

With @jchd's example, I envisage that the object would be destroyed

But @jchd mentioned that it would still exist "thanks to it's internal reference count."

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

I'm not sure what problems that would cause, if any.

Another way to think about this: The ByRef part can also be optional. Therefore, if no argument is given and no global variable exists, then a local variable is created.

Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...