Jump to content

Default keyword


Recommended Posts

Hi everybody :)
There is something I don't understand concerning the Default keyword.

Help file, topic "Language Reference - Datatypes" :

Default : Only used for assigning a default value to optional arguments of a function.

Help file, topic "Default" :

When used in parameter passing, the behavior is specified in the corresponding AutoIt function documentation.
For UDF's, it is the scripter's responsibility to check if the parameter has been set to Default and to perform the desired behavior in this situation.
If used, the passed parameter will be set to the Default keyword and not to an optional parameter value, if defined.

My question is : wouldn't it be much simpler to let AutoIt match the Default keyword to an optional parameter, instead of forcing the scripter to add numerous useless lines (one per optional parameter) for the same result ?

For example, when I scripted SortAll.au3 UDF, it had to be created like this...

Func SortAll(ByRef $aArray, $iSortCol = 0, $iType = 2, $iSense = 0, $iStable = 0)

    If $iSortCol = Default Then $iSortCol = 0 ; first column = 0
    If $iType = Default Then $iType = 2 ; 0 = String sort, 1 = Numeric sort, 2 = Natural sort
    If $iSense = Default Then $iSense = 0 ; 0 = ascending, 1 = descending
    If $iStable = Default Then $iStable = 0 ; 0 = unstable sort, 1 = stable sort

    If Not IsArray($aArray) Then
        ...

...when it would have been much simpler to create it like that :

Func SortAll(ByRef $aArray, $iSortCol = 0, $iType = 2, $iSense = 0, $iStable = 0)

    If Not IsArray($aArray) Then
        ...

For the record, Valik, in this old link, was already concerned with the Default keyword, this is what he wrote :

"Default should never compare true to a boolean, neither true nor false. Default should only compare true to -1, "" and Default itself."

Then he immediately opened trac ticket #885, was not happy with the result ("This is not fixed in the way that I want."), had a discussion with wise @jpm then admitted "Default should only compare true to Default".

This being established, after that, I don't know who forced UDF scripters to match by themselves the Default keyword to each optional parameter. And you better not forget 1 matching line in any UDF !

Link to comment
Share on other sites

2 hours ago, pixelsearch said:

Func SortAll(ByRef $aArray, $iSortCol = 0, $iType = 2, $iSense = 0, $iStable = 0)     If $iSortCol = Default Then $iSortCol = 0 ; first column = 0     If $iType = Default Then $iType = 2 ; 0 = String sort, 1 = Numeric sort, 2 = Natural sort     If $iSense = Default Then $iSense = 0 ; 0 = ascending, 1 = descending     If $iStable = Default Then $iStable = 0 ; 0 = unstable sort, 1 = stable sort     If Not IsArray($aArray) Then         ...

what a pickle !, 

Func SortAll(ByRef $aArray, $iSortCol = Default, $iType = Default, $iSense = Default, $iStable = Default)

    If $iSortCol = Default Then $iSortCol = 0 ; first column = 0
    If $iType = Default Then $iType = 2 ; 0 = String sort, 1 = Numeric sort, 2 = Natural sort
    If $iSense = Default Then $iSense = 0 ; 0 = ascending, 1 = descending
    If $iStable = Default Then $iStable = 0 ; 0 = unstable sort, 1 = stable sort

    If Not IsArray($aArray) Then
        ...

the above should be the way according to ... me ?. That way it'd standardize the func calling instead of knowing if a default value is to be  "" or -1 or "GoodLuck" !.
So yes, as it stands the default value should be Default keyword unless,

ThisOne()
ThisOne(Default)
Func ThisOne($1 = 2, $2 = "Two")
    ConsoleWrite(@TAB & $1 & @TAB & $2 & @CRLF)
EndFunc

internally AutoIt3 takes Default as whatever the default was declared as, saving us from the countless* rows of comparisons. Such change would not be script-braking** but it'd be upto the overlord to thinker with and implement if the MVPs find no flaw in the concept.

* I recon they are not countless. And they serve as "comments in the code". But if we add all lines in all func in a script it may make the script unnoticeably faster. 
** Should not be script-braking, but go figure.

Edited by argumentum
mitigated exaggerations

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

1 hour ago, argumentum said:

...instead of knowing if a default value is to be  "" or -1 or "GoodLuck" !

You don't need to know that, and I never said the Default keyword should disappear. I often use it when calling a function, for example :

_ArrayDisplay($aArray, "UNsorted", Default, Default, Default, _
    "Strings|Integers|Floats|Dates|Times|R/C")

But I wouldn't mind at all if the function was presented like the following (showing all Defaults parameters within the Func line) :

Func _ArrayDisplay(Const ByRef $aArray, $sTitle = "ArrayDisplay", $sArrayRange = "", $iFlags = 0, $vUser_Separator = "", $sHeader = "", ...

And now let AutoIt automatically match the Default keyword found in the calling line with the optional value found in the Func line, avoiding all the "If $iSortCol = Default Then..." we must script now (aren't they slowing the script speed ?)

What's wrong for the user to read all Default values in the Func line itself ?
Personally it suits me very fine.

Now you'll ask : "but if you do this, how would we know the differents values for each parameter ?"

Their description could be found :
1) In the help file (if your UDF is in the help file)

2) Before the function itself (like Melba23 does often, indicating the Default value for each optional param)

; #FUNCTION# ================================================================================
; Name...........: SortAll
; Description ...: ...
; Syntax.........: SortAll(ByRef $aArray, [....])
; Parameters ....: $aArray    ...
;                  $iSortCol  Default 0  (first column = 0)
;                  $iType     Default 2  (0 = String sort, 1 = Numeric sort, 2 = Natural sort)
;                  $iSense    Default 0  (0 = ascending, 1 = descending)
;                  $iStable   Default 0  (0 = unstable sort, 1 = stable sort)

3) Within the function itself (at its very beginning) in commented lines, if you prefer to avoid 2)

I never asked this question for changing anything. I simply wanted to know if it's a possibility that was considered at a certain moment and why it has been done differently, forcing scripters to write these superfluous lines inside each UDF, taking the risk of missing one optional parameter and good luck for the debug when the result is slightly different of what is expected.

Edit: lol, just saw your edit "mitigated exaggerations", ty for that :)

Edited by pixelsearch
Link to comment
Share on other sites

1 hour ago, argumentum said:

Should not be script-braking

Don't mind script-braking, as long as it's not script-breaking!:D

Link to comment
Share on other sites

From what I understand, that's not how you should check for Default.

Func SortAll(ByRef $aArray, $iSortCol = Default, $iType = Default, $iSense = Default, $iStable = Default)

    If IsKeyword($iSortCol) Then $iSortCol = 0 ; first column = 0
    If IsKeyword($iType) Then $iType = 2 ; 0 = String sort, 1 = Numeric sort, 2 = Natural sort
    If IsKeyword($iSense) Then $iSense = 0 ; 0 = ascending, 1 = descending
    If IsKeyword($iStable) Then $iStable = 0 ; 0 = unstable sort, 1 = stable sort

    If Not IsArray($aArray) Then
        ...

The downfall of that approach is that you can't see the difference between Null and Default, but I generally avoid using Null in the first place. If you're using both, you can use a function like this to set your default values

DefaultValue($iSortCol, 0)
DefaultValue($iType, 2)
DefaultValue($iSense, 0)
DefaultValue($iStable, 0)

Func DefaultValue(ByRef $vParam, $vValue)
    $vParam = (IsKeyword($vParam) And $vParam = Default) ? $vValue : $vParam
EndFunc

The second approach has the added bonus of being more readable as well, imho.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@argumentum That's what the second half of my post talks about, see my DefaultValue function :P

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

  • 2 months later...

Hi everybody :)
Is this an issue with the Default keyword, or not ?

I gave an example yesterday in this post, concerning StringReplace()
First I used the Default keyword for the 4th argument as shown below, but as results were surprising, then I sticked to a 4th argument = 0 . Here is the script with the Default keyword :

#include <StringConstants.au3>

Local $sString = StringReplace("lower lower lower Large Large Large", "l", "", Default, $STR_CASESENSE)
Local $iReplacements = @extended
MsgBox(0, "", $iReplacements & " replacements were made and the new string is:" & @CRLF & @CRLF & $sString)

Output :

lower lower ower Large Large Large

So only the 3rd "l" is replaced by "", which shows that Default is acting like -1 [help file : Use a negative occurrence to replace from the right side] and not like 0 [0 = all searchstrings will be replaced (default)]

If the 5th argument $STR_CASESENSE isn't passed, then the Output shows :

lower lower lower Large Large arge

which confirms that Default is acting like -1

My idea was that we can constantly use the Default keyword in built-in functions, when an optional parameter is present. But this is what's written in the help file, Default topic :

When used in parameter passing, the behavior is specified in the corresponding AutoIt function documentation.

Does it mean that if nothing is specified concerning Default in a built-in function example, then we're not allowed to use the keyword at all ? Or did the preceding script detect a bug ?

Thanks.

Link to comment
Share on other sites

  • Moderators

pixelsearch,

The wording you found in the Help file is quite specific:

Quote

When used in parameter passing, the behavior is specified in the corresponding AutoIt function documentation.

So if no particular role for Default is specified then the results of its use will be arbitrary and undefined. You need to be careful because  a "default" setting (as mentioned in the StringReplace case) is NOT necessarily the same thing as using the Default keyword as a parameter. The first defines what will happen if no parameter is passed - the second is setting a specific value to the parameter, which in this case is not mentioned in the parameter definition and so gives unexpected results.

Is that clear?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The lack of use of Default in AutoIt's internal functions bothers me, but changing them would break too many scripts so I understand not updating them. I've considered wrapping them all, but that feels excessive :D 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

5 hours ago, seadoggie01 said:

...but changing them would break too many scripts...

No, it would not brake anything. The use of Default for the default value can be added without impacting anything. But can be a lot of work. If is done good, else, meh. ;)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...