variables, type mismatch and conversion in V2a107 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
RogerWilcoNL
Posts: 17
Joined: 13 Nov 2019, 10:41

variables, type mismatch and conversion in V2a107

28 Nov 2019, 10:32

<t>Hi,<br/>
I have been trying to make a v2 a081 script to work under v2 a107.<br/>
The problem is the error "Type mismatch" I am getting:<br/>
I can understand the need for clean programming, although languages like REXX and Autohotkey are famous for their autoconversion capabilities.<br/>
My point is that, by checking for mismatches, a problem has rissen: there are NO conversion methods available for the programmer!<br/>
<br/>
For example: I am building a data structure using classes, now i need to replace an empty key-value pair by an object under the same key<br/>
When I try to overwrite the string ("") by an object it gives the correct but annoing mismatch error. Is the only solution to delete the key-value pair and than entering them again?<br/>
Are there other ways to accomplish a replacement of different type values and will there be conversion functions for maps to object,array etc.?</t>
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 11:09

Can’t help you without code
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 11:44

i too dont understand what ure getting at. post an example

classes have properties and methods, not key-value pairs
RogerWilcoNL
Posts: 17
Joined: 13 Nov 2019, 10:41

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 12:48

Here is the program you requested,
I put in some comment for three errors:

Code: Select all

LogFile := "testClass.log"
FileDelete LogFile
Lg( LogFile . " " . A_AhkVersion . " " . A_Now)
try {	
	iTm := Item.New( {} )
	Lg( "At,By = " . iTm.At . "," . iTm.By )
	iTm["lvlone"] := "lvlTwo"
	Lg( "iTm[lvlOne] = " . iTm.lvlOne . " " . iTm["lvlOne"] )
	iTm.lvlOne := Map.New()
	iTm.een.twee := 4
	iTm["X"] := "Y"
	Lg( "set iTm[X] to Y (" . iTm["X"] . ")" )
	iTm.X := "Z"
	Lg( "set iTm.X to Z (" . iTm.X . ")" )
	iTm.X := Map.New()
	Lg( "set iTm.X to New Map (" . iTm.X . ")" )
}
catch e
	Throw Exception( Lg( e.Message . " at line " . e.line ) ) 

Lg( text ) {
	global LogFile
	FileAppend text . "`r`n", LogFile
	return text
}

Class Item
{
	static Data := ""
	
	__New( oOwner )
	{
		ObjRawSet( this, "At", FormatTime( A_Now, "yyyy-MM-dd hh:mm''''ss." ) . SubStr( A_TickCount, -3 ) )
		ObjRawSet( this, "By", &oOwner )
		ObjRawSet( this, "Rd", false )
		ObjRawSet( this, "Wr", true )
		Item.Data := Object.New()
		; ObjRawSet depricated, what to use now?
	}
	
	__Get( Prop, aParm* )
	{ 
		Lg( A_ThisFunc . ": Prop(" . Prop . ") Parm(" . Type(aParm) . "," . aParm.Length . ")" . " not present" )
		if !Item.Data.HasOwnProp(Prop)
			; Swap next line with COMMENT TO CONTINUE TO NEXT PROBLEM
			Item.Data[Prop] := Object.New()
			; Item.Data.%Prop% := Object.New()
		return Item.Data.%Prop%
	}
	
	__Set( Prop, aParm, Value )
	{ 
		; Type mismatch in Lg ( value is not a string but a map, at this point it could be anyting)
		; THERE IS NEED FOR A VARIANT conversion function
		; COMMENT NEXT LINE TO CONTINUE TO NEXT PROBLEM
		Lg( A_ThisFunc . ": Prop(" . Prop . ") aParm(" . Type(aPArm) . ") Value(" . Value . ") " . " not present" )
		if InStr( "At,By,Rd,Wr", Prop )
			this.%Prop% := Value
		else
			Item.Data.%Prop% := Value
		return
	}
	
	__Item[ Prop ]
	{
		get {
			Lg( A_ThisFunc . ": Prop(" . Prop . ")" )
			return Item.Data.%Prop%
		}
		
		Set {
			/* 
			;this code inserted to prevent Type Mismatch
			;CAN THIS BE SOLVED IN A SHORTER WAY?
			try {
				if Item.Data.HasOwnProp(Prop) {
					Old := Type(Item.Data.%Prop%)
					New := Type(Value)
					if Old != New
						Item.Data.Delete(Prop)
				}
			*/	
				Item.Data.%Prop% := Value
			/*
			;this code inserted to prevent Type Mismatch
			}
			catch e
				throw Exception( "ERROR " . e.message )
			*/
			return
		}
	}
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 14:46

; ObjRawSet depricated, what to use now?

Code: Select all

instance.DefineProp('propName', {get: /* implementation function of ur choosing */})
ur "Type mismatch" stems from ur trying to string concatenate a Map with Strings. u can fix this by defining .ToString() and calling String() on the object:

Code: Select all

Object.Prototype.DefineMethod('ToString', this => Type(this))
string concat operator and autoconcat should probably automatically try to invoke this method, but who knows - maybe this will come later

ur other problem i couldnt quite understand. post problematic code samples as is. i cant figure out what permutation of commented out lines produces an error for u
RogerWilcoNL
Posts: 17
Joined: 13 Nov 2019, 10:41

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 15:32

I Think you solved my first problem, I often use logging in new functions and classes to get my parameters right or to track them.
Because my jxon_dump won't work anymore, and logging went wrong, I lost a hand and a foot in developing (Yes I program with my feet too ;-)
The most important point is, that forcing good programming skills, means offering the tools too. Like a variable definition "bMissing := true is boolean" or "bool bMissing := false"
Plus it is work in progress because an undefined variable gets true from function isSet and the resulting value is of Type String ("")
Changing a Variable to a different Type is allowed in the main stream but not from inside classes
Have a look at the following code:
RogerWilcoNL
Posts: 17
Joined: 13 Nov 2019, 10:41

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 15:33

Code: Select all

LogFile := "testVariant.log"
FileDelete LogFile
Lg( LogFile . " " . A_AhkVersion . " " . A_Now)
X := Variant.New( Val )
Lg( X.GetStr() )								;returns 1 String "", should be 0 ? ?
X := "3"
Lg( IsSet(X) . " " . Type(X) . " " . X )		;returns 1 String 3 , is OK
X := 3											; should give type mismatch
Lg( IsSet(X) . " " . Type(X) . " " . X )		;returns 1 Integer 3, should give type mismatch
X := Object.New()								;is OK but should give type mismatch
Lg( IsSet(X) . " " . Type(X) ) 					;returns 1 Object
X := {}											; is OK but should give type mismatch
Lg( IsSet(X) . " " . Type(X) ) 					;returns 1 Object
return

Lg( text ) {
	global LogFile
	FileAppend text . "`r`n", LogFile
	return text
}

Str( vVar )
{
	return vVar . ""
}

Class Variant
{
	__New( vVal )
	{
		this.Set  := IsSet(vVal)
		this.Type := Type(vVal)
		this.Val  := vVal
	}
	
	GetStr()
	{
		return this.Set . " " . this.Type . " " . String(this.Val)
	}
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: variables, type mismatch and conversion in V2a107

28 Nov 2019, 16:22

Code: Select all

Lg( X.GetStr() )								;returns 1 String "", should be 0 ? ?
ure testing with IsSet() whether vVal(read: the function argument) is initialized. function arguments will by definition always end up being initialized. otherwise u wouldnt have been able to call the function in the first place. to test the variable ure passing to this function argument instead(read: the uninitialized Val, as u had probably intended), declare the function argument with the ByRef specifier, as shown in the docs

Code: Select all

X := 3											; should give type mismatch
what should? the assignment? why?

Code: Select all

Lg( IsSet(X) . " " . Type(X) . " " . X )		;returns 1 Integer 3, should give type mismatch
maybe. maybe not

Code: Select all

X := Object.New()								;is OK but should give type mismatch
why?

Code: Select all

X := {}							;is OK but should give type mismatch
why?

some additional info regarding
string concat operator and autoconcat should probably automatically try to invoke this method, but who knows - maybe this will come later
String calls v.ToString() if v is an object. (Ideally this would be done for any implicit conversion from object to string, but the current implementation makes this difficult.)
RogerWilcoNL
Posts: 17
Joined: 13 Nov 2019, 10:41

Re: variables, type mismatch and conversion in V2a107

29 Nov 2019, 02:41

Thanks for all the responses.
Just pointing out, that the behaviour inside and outside classes is different and thus confusing (regarding type mismatch errors)
When in a class, I set an instance variable like X := "" and than in say the __New method, I give this variable its initial value this.X := Object.New(). This generates the type mismatch error!
When in procedural code taking the same actions ( X:="" and later X:=Object.New() ) , this doesn't generate an error .

Thanks for all the other remarks, I promisse to make good use of that.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: variables, type mismatch and conversion in V2a107  Topic is solved

29 Nov 2019, 02:51

RogerWilcoNL wrote:
29 Nov 2019, 02:41
When in a class, I set an instance variable like X := "" and than in say the __New method, I give this variable its initial value this.X := Object.New(). This generates the type mismatch error!
except it doesnt do any of that

Code: Select all

class Foo
{
	X := ''

	__New() {
		this.X := Object.New()
	}
}

f := Foo.New()
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: variables, type mismatch and conversion in V2a107

29 Nov 2019, 14:29

i dont know how confused u are. u seem to advocate for ahk to become a statically typed programming language. which while not necessarily a bad thing, certainly isnt bound to happen in v2(if at all ever)
depending on how lexikos decides to proceed with implicit conversions, u may see ahk evolve into a strongly typed language... or it might remain a weakly typed language. only time will tell
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: variables, type mismatch and conversion in V2a107

29 Nov 2019, 14:44

Probably won’t know the answer to that until if/when the parser is rewritten.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, emp00 and 30 guests