TheDcoder

Implement optional ByRef parameters for functions

27 posts in this topic

#1 ·  Posted

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?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites



#2 ·  Posted

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)

Share this post


Link to post
Share on other sites

#3 ·  Posted

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.

1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#4 ·  Posted

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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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)

Share this post


Link to post
Share on other sites

#6 ·  Posted

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.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@jchd Well, I wouldn't know since I don't use Objects...

Edited by TheDcoder

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#8 ·  Posted

@argumentum Have a look at ticket #2360. If you don't like this feature, then don't use it ;)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#9 ·  Posted

10 minutes ago, jchd said:

Return $vFoo.Something()

What does $vFoo.Something() evaluate to?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#10 ·  Posted

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.

Share this post


Link to post
Share on other sites

#11 ·  Posted

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)

Share this post


Link to post
Share on other sites

#12 ·  Posted

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"


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#14 ·  Posted

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.

1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#15 ·  Posted

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:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#16 ·  Posted

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)

Share this post


Link to post
Share on other sites

#17 ·  Posted

12 hours ago, jchd said:

Sorry for the noise.

No problem :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#19 ·  Posted

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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

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

Share this post


Link to post
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

  • Similar Content

    • Morthawt
      By Morthawt
      I have a program I made long ago for something but I never saved the koda file so it would make it a complete pain for me to visually tweak anything because as far as I am aware, I would have to design an entirely new GUI.
      Would it be possible for you guys at AutoIt to add functionality to Koda so that you can take an existing GUI section, with while loop and cases and all and then koda would literally build it ready for tweaking and where existing things still exist and have been tweaked, it would tweak such details and keep all the existing case information and added formatting things that were manually tweaked etc?
      This would be really amazing and I feel that other people would benefit from it. I can't be the only person who makes a GUI, doesn't save it and then has no problems making minor tweaks in-code but struggles like hell to go rearranging things perfectly. That is where using a GUI (koda) to make and arrange a GUI you are making, makes everything easy and pleasure to deal with.
      tldr;
      Example:
      A friend gives me his code and wants help. The GUI is just a mess, but trying to do all the alignment and positioning all by digits in code would be a pain. I cannot just hallucinate where everything is based on pixel locations and sizes. So I would take the entire GUI chunk of code, all the form/controls/formatting functions/while loop/switch/case in it's entirety and paste it into an area of Koda and press a button. Koda would then build the GUI on the screen, placing all the controls and formatting them with the instructions that may have been added and keeps the case information as-is. Then when you make your alterations in Koda, you go to export it and it makes an entirely new export that re-makes (from scratch as if you had made this GUI from scratch) all the code for controls, size, placement etc and then also adds all the existing case code back as part of the code you then send (or manually copy/paste as I do) to Scite editor.
      Is there any chance this could be implemented? I think this could help a lot of people with their own scripts and others'.
    • TheDcoder
      By TheDcoder
      Hello friends , I was working on my project today and got a wild idea with Switch..EndSwitch statements, it looks like this:
      #include <MsgBoxConstants.au3> $iWildInteger = 10 Switch $iWildInteger Case 1 MsgBox($MB_OK, "AutoIt Forums", "The value of $iWildInteger is " & 1 & '.') Case 2, Else MsgBox($MB_OK, "AutoIt Forums", "The value of $iWildInteger is " & 2 & ' or else.') EndSwitch A case with Else and another value... I tried it but It didn't work , I think its great to have a feature like this so that I don't have to have a separate case for it
       
      Please note that this is *NOT* a feature request , This is a poll for opinion on this feature (before making a feature request in the BT ), Vote wisely, TD
    • bustasnipe
      By bustasnipe
      Hello I have been going to this forum regularly for a couple weeks, getting all sorts of good info from you people.
      So first off thanks, you are all gentlemen/women and scholars.
      I just wanted to point out that I think it would be neat if you could double click either side of the quotation marks in the Scite editor to select the string of text within. Instead of having to manually select all the text within, which is not hard, but u may sometimes miss a extra character or something. Just like how you can select variables and single words by double clicking. Thanks to all those who have worked on and continue to work on Autoit, I love it.