String prototype not available (AHK2) Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
madsounds
Posts: 59
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: 6222
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: 59
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: 1640
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: 9592
Joined: 30 Sep 2013, 04:07
Contact:

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: 59
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: 1640
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: 9592
Joined: 30 Sep 2013, 04:07
Contact:

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 “Ask for Help (v2)”

Who is online

Users browsing this forum: CrowexBR, Draken, ntepa, wineguy and 42 guests