Type mismatch. AutoHotkey_2.0-a096-2ad11cb

Discuss the future of the AutoHotkey language
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 05:10

Why?

Code: Select all

;Gdip part
Screen:= ""

if ((Screen & 1) != "")
{
	msgBox("Cool")
}
And in AutoHotkey_2.0-a096-2ad11cb

Code: Select all

---------------------------
TEST_Send.ahk
---------------------------
Error:  Type mismatch.

	Line#
	004: Screen := ""
--->	006: if ((Screen & 1) != "")
	007: {
	008: msgBox("Cool")
	009: }
	010: Exit
	011: Exit
	011: Exit

The current thread will exit.
---------------------------
OK   
---------------------------
Ok string not work with any arithmetic operators (+-*/&...) so code must be:

Code: Select all

;Gdip part
Screen:= "100|200|5|7"

if (Screen is "number" && Screen & 1 != "")
{
	msgBox("True")
}
else
{
	msgBox("False")
}
Is that mean the v2 going to variable types?
AHKv2.0 use the future now.
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 12:26

I think to be compatible "nonNumber" must return 0 and must be treated as 0 in expressions.
In Autohotkey "" is used to clear any type of variables so it must clear and "number" variables.
AHKv2.0 use the future now.
Helgef
Posts: 3191
Joined: 17 Jul 2016, 01:02
Contact:

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 13:22

If you clear your variable, you shouldn't use it in a math expression afterwards, luckily, we now get notified if we make such a mistake. This is the best thing that has happened to the language since sliced butter. Now, we only need var := [color=#008000]"1"[/color] to behave the same way, that is, throw an exception in a math expression / function.

Cheers.
User avatar
nnnik
Posts: 3340
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 15:51

I dont think so.
Recommends AHK Studio
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 22:58

In AHK no typed variables so in 100 lines code you can`t know what inside the variable to use it at math or no. And if there 5 variables we need 5 extra checks is "number".

Code: Select all

number(arg){
	return arg is "number"? arg: 0
}
I don`t understand why we need to throw exception instead of treated "nonnumber" as 0

Code: Select all

;...
a:= b + 123
;...
Till now it was 123 if b == "" now exception ???

Exception make type separation: number - nonnumber, that is not in AHK style.
AHKv2.0 use the future now.
User avatar
nnnik
Posts: 3340
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

14 May 2018, 23:40

You need to adjust the way you write code for v2.
Disregarding Helgefs love for forcing explicit type conversions I think he's right about throwing an error on invalid math is a good thing.
Especially since you can use a try Statement to detect all errors.
Recommends AHK Studio
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

15 May 2018, 10:18

nnnik wrote:You need to adjust the way you write code for v2.
Disregarding Helgefs love for forcing explicit type conversions I think he's right about throwing an error on invalid math is a good thing.
Especially since you can use a try Statement to detect all errors.

Code: Select all

; Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11
; Modifed by Rseding91 using fincs 64 bit compatible Gdip library 5/1/2013
; Corrected by _3D_ (A_PtrSize incorect use)	2/22/2014
; Supports: v2.0	(syntax v2.0 by _3D_)		2/27/2014
;			v2.081	(syntax v2.0 by _3D_)		8/28/2017
;			v2.096	(syntax v2.0 by _3D_)		5/14/2018
;#####################################################################################
; Function				Gdip_BitmapFromScreen
; Description			Gets a gdi+ bitmap from the screen
;
; Screen		0 = All screens
;						Any numerical value = Just that screen
;						x|y|w|h = Take specific coordinates with a width and height
; Raster		raster operation code
;
; return    If the function succeeds, the return value is a pointer to a gdi+ bitmap
;						-1:		one or more of x,y,w,h not passed properly
;
; notes			If no raster operation is specified, then SRCCOPY is used to the returned bitmap
Gdip_BitmapFromScreen(Screen:=0, Raster:="") {
	if (Screen == 0) {
		x:= Sysget(76)
		y:= Sysget(77)	
		w:= Sysget(78)
		h:= Sysget(79)
	} else if (SubStr(Screen, 1, 5) == "hwnd:") {
		Screen := SubStr(Screen, 6)
		if !WinExist( "ahk_id " Screen)
			return -2
		WinGetPos(,, w, h, "ahk_id" Screen)
		x := y := 0
		hhdc := GetDCEx(Screen, 3)
	} else if (Screen is "number" && Screen & 1 != "") { ;AutoHotkey_2.0-a096-2ad11cb
		MonitorGet(Screen, x, y, w, h)
		w -= x, h -= y
	} else {
		S:= StrSplit(Screen, "|")
		x := S[1], y := S[2], w := S[3], h := S[4]
	}
	if (x == "") || (y == "") || (w == "") || (h == "")
		return -1

	chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
	BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
	ReleaseDC(hhdc)
	
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
	return pBitmap
}
As I said compatible.
AHKv2.0 use the future now.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

15 May 2018, 10:33

_3D_ wrote:

Code: Select all

; Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11
; Modifed by Rseding91 using fincs 64 bit compatible Gdip library 5/1/2013
; Corrected by _3D_ (A_PtrSize incorect use)	2/22/2014
; Supports: v2.0	(syntax v2.0 by _3D_)		2/27/2014
;			v2.081	(syntax v2.0 by _3D_)		8/28/2017
;			v2.096	(syntax v2.0 by _3D_)		5/14/2018

Gdip_BitmapFromScreen(Screen:=0, Raster:="") {
...
	} else if (Screen is "number" && Screen & 1 != "") { ;AutoHotkey_2.0-a096-2ad11cb
		MonitorGet(Screen, x, y, w, h)
		w -= x, h -= y
	} 
	...
}
As I said compatible.
we are maintaining a v1/v2 compatible GDIP library here:
https://github.com/mmikeww/AHKv2-Gdip

this looks like one of the changes we need to make. this BitmapFromScreen() function allows the "Screen" param to either be:
1. "0" for all screens
2. "hwnd:" for a specific window
3. any number, for a specific monitor
4. "p|i|p|e" separated string for a specific screen area

this particular Screen&1 != "" seems to just be verifying case #3 above, that its a numbered of a screen. wouldnt just Screen is "number" be sufficient? and we can remove the bitwise-AND altogether? of course to be backwards compatible with v1 we'd do the same as this:
https://github.com/mmikeww/AHKv2-Gdip/b ... .ahk#L2773
by the way, there are two other places that do if (Matrix&1 = "") in other functions, which maybe we can just replace with if Matrix is not number

and if you have other changes please advise or submit a pull request

Helgef
Posts: 3191
Joined: 17 Jul 2016, 01:02
Contact:

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

15 May 2018, 11:27

Hello :wave: .
_3D_ wrote:

Code: Select all

;...
a:= b + 123
;...
Till now it was 123 if b == "" now exception ???
No it wasn't, non-numerics in math expressions was treated as an error and yielded an empty string. Even in v1 using non-numerics in math expressions are considered an error, according to the documentation.
guest3456 wrote:wouldnt just Screen is "number" be sufficient?
Indeed, Screen & 1 != "" is superfluous, it can never be false, it can only be true or an exception :thumbup: .

Cheers.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

16 May 2018, 08:42

i've updated the v2 Gdip library:
https://github.com/mmikeww/AHKv2-Gdip/c ... 0e91c64b84

this changed in v2-a95:
Changed math functions and operators to throw an exception instead of returning "" for errors.

_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

16 May 2018, 11:58

guest3456 wrote:i've updated the v2 Gdip library:
https://github.com/mmikeww/AHKv2-Gdip/c ... 0e91c64b84

this changed in v2-a95:
Changed math functions and operators to throw an exception instead of returning "" for errors.

Code: Select all

if (x == "") || (y == "") || (w == "") || (h == "")
		return -1
What about if x=="abc"?
Let replace with:

Code: Select all

try {
		_:= x + y + w + h ;check all if numbers
	} catch {
		return -1
	}
AHKv2.0 use the future now.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Type mismatch. AutoHotkey_2.0-a096-2ad11cb

16 May 2018, 15:00

_3D_ wrote:

Code: Select all

if (x == "") || (y == "") || (w == "") || (h == "")
		return -1
What about if x=="abc"?
Let replace with:

Code: Select all

try {
		_:= x + y + w + h ;check all if numbers
	} catch {
		return -1
	}
x="abc" only if the user did something like this: Gdip_BitmapFromScreen("abc|def|ghi|xyz")
what if Screen="abc" ? Gdip_BitmapFromScreen("abc") ?

at some point the user is responsible for reading the API and using the function correctly. plus, the original Gdip used this, so we're not making changes unless necessary. this isn't meant to be a rewrite


Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 7 guests