Added Method Functionality

Propose new features and changes
User avatar
Delta Pythagorean
Posts: 546
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Added Method Functionality

18 May 2020, 23:27

I have two ideas:

1.)
In a class there's all kinds of things you can do with a method, but sometimes you want to have private and public functions for people to use.
For example, I have a method that displays a message, and another that adds an index for how many times the method's been called:

Code: Select all

Loop, 4
	Messages.Alert("I've been called at least: ")
Return

Class Messages {
	Alert(Text) {
		MsgBox, 0x0, Alert!, % Text . This.__Times__()
	}

	; We only want to allow this function to be called inside the class, not outside!
	__Times__() {
		Static I := 0
		I += 1
		Return, (I)
	}
}
Some languages have it where you can specify which is public and which is private before the method name.
Some others have it with a built in variable that's an array of all of the method's names.
It would be nice to have a way to only allow certain methods to be called inside the class instead of letting the user call any method they want and possibly cause problems.

2.)
Documentation of a method inside a class is very useful at times. Python does this by having three double quotes just under the function definition:

Code: Select all

def my_function():
	"""I just display True so..."""
	return True
Doing something for AHK shouldn't be too hard right...? I'd say have some sort of meta function that defines the documentation of the methods.

Just a thought :)

- [AHK].......: 1.1.32.00 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
nnnik
Posts: 4444
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Added Method Functionality

19 May 2020, 03:30

The second suggestion should probably be in its own topic.
Recommends AHK Studio
User avatar
Delta Pythagorean
Posts: 546
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: Added Method Functionality

20 May 2020, 06:46

Fine by me, I'll split them as soon as I get back from work.

- [AHK].......: 1.1.32.00 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
HotKeyIt
Posts: 2095
Joined: 29 Sep 2013, 18:35
Contact:

Re: Added Method Functionality

20 May 2020, 06:51

In AHK v2 you can do:

Code: Select all

msg:=Messages.new()
Loop 4
  msg.Alert("I've been called at least: ")
Return

Class Messages {
  Alert(Text) {
    MsgBox Text . times()
    
    times() {
        Static I := 0
        I += 1
        Return I
    }
  }
}
swagfag
Posts: 3617
Joined: 11 Jan 2017, 17:59

Re: Added Method Functionality

20 May 2020, 07:07

u can, but its not quite the same as a private method
HotKeyIt
Posts: 2095
Joined: 29 Sep 2013, 18:35
Contact:

Re: Added Method Functionality

20 May 2020, 07:39

Yes in that case it is a private function inside method.
guest3456
Posts: 3084
Joined: 09 Oct 2013, 10:31

Re: Added Method Functionality

20 May 2020, 10:59

why not just use commented lines for the #2 suggestion?

swagfag
Posts: 3617
Joined: 11 Jan 2017, 17:59

Re: Added Method Functionality

20 May 2020, 16:09

What is a Docstring?
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.
doesnt seem of much use, unless lexikos also creates corresponding docgen utils
User avatar
kczx3
Posts: 1051
Joined: 06 Oct 2015, 21:39

Re: Added Method Functionality

20 May 2020, 19:34

@swagfag which I highly doubt he would do that
lexikos
Posts: 6725
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Added Method Functionality

22 May 2020, 20:29

1. Being a feature of many mature programming languages, this is an old idea that has been discussed before. For instance, https://www.autohotkey.com/boards/viewtopic.php?f=13&t=28150

My preferred method of dealing with this is to use convention and documentation. If you want to prevent users of your library from calling the method, make it awkward to call, tell them not to call it (or just what is acceptable to call), and let them deal with the consequences of ignoring your recommendations. If calling your private method is the only way to achieve what they want, let them do it. It's not like you can stop them from making a private method public.

ECMAScript 6 uses the prefix # for private fields. One possible implementation is to replace all such symbols at load time with unique values that only the compiler/interpreter knows. For example, inside class A, #x could internally be the string #A.x/89234, while in class B it could be #B.x/03248, and outside of a class it would be an error. The class name prefix allows it to be identified by a debugger (or enumerator if the field can be enumerated), while the random suffix prevents the user from using something like that["#A.x"] or that.%"#A.x"%.

As for making private methods awkward to call, one strategy is to use a hotstring to generate a prefix that can't be typed easily. For instance, in Object.ahk I used the prefix ← for private fields and a hotstring like :*:<-::← to type it (although this was actually just to avoid key conflicts).


2. In Visual Studio, when I hover over a variable, function or type name, I get a tooltip showing the content of an adjacent comment, which is usually documentation. I find this to be fairly effective. Sometimes the comments aren't documenting what I'm hovering over, but that's because they weren't written with this specific feature in mind. Perhaps the only benefit of using a standalone multi-line string in place of a comment is that the string is otherwise completely useless, whereas the comment might relate to something else. However, I think it carries the wrong meaning.

In any case, tools already exist to generate documentation from comments. Comments are fundamentally documentation; there is no need for another type of comment.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 18 guests