String prototype not available (AHK2) Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
madsounds
Posts: 34
Joined: 31 May 2019, 08:14

String prototype not available (AHK2)

12 Jul 2020, 10:58

How to define new method/property in String class in AHK2?

This code results with "This variable has not been assigned a value. Specifically: String".

Code: Select all

String_IsBlank( str )
{
	if str ~= "^\s*$"
	{
		return true
	}
	return false
}

String.Prototype.DefineMethod( "IsBlank", (str) => String_IsBlank(str) )
String.Prototype.DefineProp( "Length", { get: (str) => StrLen(str) } )
swagfag
Posts: 3909
Joined: 11 Jan 2017, 17:59

Re: String prototype not available (AHK2)  Topic is solved

12 Jul 2020, 11:10

Code: Select all

%{}.GetMethod('DefineMethod')%(''.Base, "IsBlank", (str) => str ~= "^\s*$")
%{}.GetMethod('DefineProp')%(''.Base, "Length", { get: (str) => StrLen(str) } )
madsounds
Posts: 34
Joined: 31 May 2019, 08:14

Re: String prototype not available (AHK2)

12 Jul 2020, 11:18

swagfag wrote:
12 Jul 2020, 11:10

Code: Select all

%{}.GetMethod('DefineMethod')%(''.Base, "IsBlank", (str) => str ~= "^\s*$")
%{}.GetMethod('DefineProp')%(''.Base, "Length", { get: (str) => StrLen(str) } )
Omg so complex :D Thanks for solution and optimization 8-)
User avatar
kczx3
Posts: 1119
Joined: 06 Oct 2015, 21:39

Re: String prototype not available (AHK2)

12 Jul 2020, 11:30

swagfag wrote:
12 Jul 2020, 11:10

Code: Select all

%{}.GetMethod('DefineMethod')%(''.Base, "IsBlank", (str) => str ~= "^\s*$")
%{}.GetMethod('DefineProp')%(''.Base, "Length", { get: (str) => StrLen(str) } )
Is it like this because String Is not yet an exposed class?
lexikos
Posts: 6972
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: String prototype not available (AHK2)

12 Jul 2020, 17:42

The nature of the String prototype is explicitly documented under Primitive Values, which includes some "polyfill" to define the classes.

The classes do not exist yet, because there'd be no point defining them if they're never exposed to script. I think I was deferring it because I wasn't sure I wanted to reserve those names globally by default.

Also,

Code: Select all

%{}.GetMethod('DefineMethod')%(''.Base, "IsBlank", Func("IsSpace"))
%{}.GetMethod('DefineProp')%(''.Base, "Length", { get: Func("StrLen") } )
madsounds
Posts: 34
Joined: 31 May 2019, 08:14

Re: String prototype not available (AHK2)

16 Jul 2020, 15:05

lexikos wrote:
12 Jul 2020, 17:42
The nature of the String prototype is explicitly documented under Primitive Values, which includes some "polyfill" to define the classes.

The classes do not exist yet, because there'd be no point defining them if they're never exposed to script. I think I was deferring it because I wasn't sure I wanted to reserve those names globally by default.

Also,

Code: Select all

%{}.GetMethod('DefineMethod')%(''.Base, "IsBlank", Func("IsSpace"))
%{}.GetMethod('DefineProp')%(''.Base, "Length", { get: Func("StrLen") } )
Oh thanks, didn't know about IsSpace :bravo:
User avatar
kczx3
Posts: 1119
Joined: 06 Oct 2015, 21:39

Re: String prototype not available (AHK2)

16 Jul 2020, 15:13

@madsounds That makes two of us!
lexikos
Posts: 6972
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: String prototype not available (AHK2)

16 Jul 2020, 21:36

Before IsSpace(x) there was x is 'space'. Before that (in v1) there was If x is space. Of course, neither of those were functions that you could pass to DefineProp/DefineMethod.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 1 guest