<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>
variables, type mismatch and conversion in V2a107 Topic is solved
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
Can’t help you without code
Re: variables, type mismatch and conversion in V2a107
i too dont understand what ure getting at. post an example
classes have properties and methods, not key-value pairs
classes have properties and methods, not key-value pairs
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
Here is the program you requested,
I put in some comment for three errors:
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
}
}
}
Re: variables, type mismatch and conversion in V2a107
; ObjRawSet depricated, what to use now?
Code: Select all
instance.DefineProp('propName', {get: /* implementation function of ur choosing */})
Code: Select all
Object.Prototype.DefineMethod('ToString', this => Type(this))
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
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
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:
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:
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
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)
}
}
Re: variables, type mismatch and conversion in V2a107
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 docsCode: Select all
Lg( X.GetStr() ) ;returns 1 String "", should be 0 ? ?
what should? the assignment? why?Code: Select all
X := 3 ; should give type mismatch
maybe. maybe notCode: Select all
Lg( IsSet(X) . " " . Type(X) . " " . X ) ;returns 1 Integer 3, should give type mismatch
why?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
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.)
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
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.
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.
Re: variables, type mismatch and conversion in V2a107 Topic is solved
except it doesnt do any of thatRogerWilcoNL wrote: ↑29 Nov 2019, 02:41When 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!
Code: Select all
class Foo
{
X := ''
__New() {
this.X := Object.New()
}
}
f := Foo.New()
-
- Posts: 17
- Joined: 13 Nov 2019, 10:41
Re: variables, type mismatch and conversion in V2a107
My bad, see how confused I am?
Re: variables, type mismatch and conversion in V2a107
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
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
Re: variables, type mismatch and conversion in V2a107
Probably won’t know the answer to that until if/when the parser is rewritten.