AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AutoHotkey_L: Arrays, Debugger, x64, COM, #If expression ...
Goto page Previous  1, 2, 3 ... 26, 27, 28 ... 68, 69, 70  Next
 
This topic is locked: you cannot edit posts or make replies.    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Lexikos



Joined: 17 Oct 2006
Posts: 7299
Location: Australia

PostPosted: Mon Feb 15, 2010 9:19 am    Post subject: Reply with quote

HotKeyIt wrote:
obj.[key]
As I mentioned earlier, I initially thought obj.() seemed a bit odd/ambiguous. Even more so for obj.[key], and I think there is some risk that users will write it without realising it is not equivalent to obj[key]. Additionally, obj.() is intended to invoke the "default" method of the object. obj.[key] should therefore retrieve the [key] property of the default value of obj; but for obj's default value to even be an object wouldn't make much sense.

If a built-in "default value" feature is implemented, it might use obj[], which is distinct from obj[""] -- rather, because they are distinct and obj[] typically does nothing unless it is specifically coded within the object's meta-functions. For example:
Code:
obj := Object("base", Object("__Get", "Get", "__Set", "Set"))
obj[] := "foo"
obj[""] := "bar"
MsgBox % obj[] obj[""]
Get(obj, x="#oMittEd#") {
    if (x == "#oMittEd#")
        return obj.default
}
Set(obj, x, y="#oMittEd#") {
    if (y == "#oMittEd#")
        return obj.default := x
}
If obj.[x] were allowed, it would be equivalent to obj[""][x] and not obj[][x]. (Then again, an exception could be made for it at the expense of consistency with obj.().)

Since I don't see a clear/obvious/common use for obj.[], I'd rather not allow it.

Btw, obj.(prm) must necessarily be treated as obj[""](prm), since in the current design it would otherwise be indistinguishable from obj[prm](), which almost certainly wouldn't be the user's intention.
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4653
Location: AHK Forum

PostPosted: Mon Feb 15, 2010 8:25 pm    Post subject: Reply with quote

Lexikos wrote:
HotKeyIt wrote:
obj.[key]
As I mentioned earlier, I initially thought obj.() seemed a bit odd/ambiguous. Even more so for obj.[key], and I think there is some risk that users will write it without realising it is not equivalent to obj[key].

I agree about the risk but in my view it is worth it.
Lexikos wrote:
Additionally, obj.() is intended to invoke the "default" method of the object. obj.[key] should therefore retrieve the [key] property of the default value of obj; but for obj's default value to even be an object wouldn't make much sense.

If a built-in "default value" feature is implemented, it might use obj[], which is distinct from obj[""] -- rather, because they are distinct and obj[] typically does nothing unless it is specifically coded within the object's meta-functions.


Do I understand correct that default value is the same as default method?
So all these will be valid when implemented?
Code:
obj.(p) ;will this use [] or [""]?
obj[](p)
obj[""](p)


Why do you think an object as default value would not make sense?
Example using WideObject():
Code:
obj:=WideObject()
Loop 5
   obj[A_Index]:=Chr(A_Index+64)

;currently ["",0] holds count of items and ["",1],["",2] hold keys
While obj["",0]>=A_Index
   MsgBox % "Key " obj["",A_Index] "`nValue " obj[A_Index]

;using . syntax it would look much better and obviously I think
While obj.[0]>=A_Index
   MsgBox % "Key " obj.[A_Index] "`nValue " obj[A_Index]


Edit:
Btw. will this be allowed?
Code:
obj.obj.(p)
obj[obj].(p)
;-------------EDIT---------
obj:=Object()
obj.1:=Object("","func")
obj[1].(1) ;this already works
;obj.1.(1) ;this does not

func(p){
   MsgBox % p
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink


Last edited by HotKeyIt on Mon Feb 15, 2010 10:02 pm; edited 2 times in total
Back to top
View user's profile Send private message
IsNull



Joined: 10 May 2007
Posts: 593
Location: .switzerland

PostPosted: Mon Feb 15, 2010 9:53 pm    Post subject: Reply with quote

Quote:
;using . syntax it would look much better and clearer I think

That would be amazing.
_________________
http://securityvision.ch
AHK 2D GAME ENGINE
Back to top
View user's profile Send private message Visit poster's website
Lexikos



Joined: 17 Oct 2006
Posts: 7299
Location: Australia

PostPosted: Mon Feb 15, 2010 10:09 pm    Post subject: Reply with quote

HotKeyIt wrote:
I agree about the risk but in my view it is worth it.
Merely to save a few characters? I don't think so. What special meaning/purpose would it have?
Quote:
Do I understand correct that default value is the same as default method?
So all these will be valid when implemented?
No. No.
Quote:
obj.(p) ;will this use [] or [""]?
I already explained it in my previous post. Or did you mean "will it be changed?" No.
Quote:
Why do you think an object as default value would not make sense?
The default value would be invoked automatically when the script attempts to use an object in a context which requires a non-object value, such as:
Code:
MsgBox %obj%
DllCall("func", "uint", obj)
Quote:
Btw. will this be allowed?
Code:
obj.obj.(p)
obj[obj].(p)
It already is. However, syntax validation currently treats obj.() and obj.x.() as errors when they begin a line. This will be fixed.
Quote:
While obj.[0]>=A_Index
You remind me why I didn't allow it in the first place: the distinction between obj[0] and obj.[0] is far from intuitive, and not very obvious even to me. In fact, at one point I thought of making them equivalent. Also, I never really saw the benefit of your WideObject over something more typical/intuitive, like:
Code:
Loop % obj.count
   MsgBox % "Key " obj.key[A_Index] "`nValue " obj[A_Index]
If you need some way to distinguish a key from all others (including ""), you can use a unique object, or as I demonstrated in my previous post:
Code:
Loop % obj[][0]
   MsgBox % "Key " obj[][A_Index] "`nValue " obj[A_Index]
(I also don't see why you used a While loop in this case. Loop is much more succinct, and more efficient to boot.)
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4653
Location: AHK Forum

PostPosted: Mon Feb 15, 2010 10:29 pm    Post subject: Reply with quote

Lexikos wrote:
HotKeyIt wrote:
I agree about the risk but in my view it is worth it.
Merely to save a few characters? I don't think so. What special meaning/purpose would it have?

When it gets to loop trough objects that contain objects it gets very messy in my point of view, using . makes it definitely easier for me.
Lexikos wrote:
Quote:
Do I understand correct that default value is the same as default method?
So all these will be valid when implemented?
No. No.
Quote:
obj.(p) ;will this use [] or [""]?
I already explained it in my previous post. Or did you mean "will it be changed?" No.
Quote:
Why do you think an object as default value would not make sense?
The default value would be invoked automatically when the script attempts to use an object in a context which requires a non-object value, such as:
Code:
MsgBox %obj%
DllCall("func", "uint", obj)

But Object(obj) and IsObject(obj) will still work, right?
Lexikos wrote:
Quote:
Btw. will this be allowed?
Code:
obj.obj.(p)
obj[obj].(p)
It already is. However, syntax validation currently treats obj.() and obj.x.() as errors when they begin a line. This will be fixed.
Thanks.
Lexikos wrote:
Quote:
While obj.[0]>=A_Index
You remind me why I didn't allow it in the first place: the distinction between obj[0] and obj.[0] is far from intuitive, and not very obvious even to me. In fact, at one point I thought of making them equivalent. Also, I never really saw the benefit of your WideObject over something more typical/intuitive, like:
Code:
Loop % obj.count
   MsgBox % "Key " obj.key[A_Index] "`nValue " obj[A_Index]
If you need some way to distinguish a key from all others (including ""), you can use a unique object, or as I demonstrated in my previous post:
Code:
Loop % obj[][0]
   MsgBox % "Key " obj[][A_Index] "`nValue " obj[A_Index]
(I also don't see why you used a While loop in this case. Loop is much more succinct, and more efficient to boot.)

WideObject lets you enumerate trough any key beside [""], so a key can be anything, string or digit or object.

While is important here on one side because a key might be deleted or added inside the loop/while. Using loop you would not notice that and get out of range or not reach last key. On the other side when using multithreading again keys can be deleted or added in separate thread.

When a key is deleted, WideObject changes index of last key to index of deleted key.
Though you might overloop a key because another key got deleted, adding a key will always work and this is the best method I think because it is very fast.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
lexikos*
Guest





PostPosted: Tue Feb 16, 2010 4:03 am    Post subject: Reply with quote

HotKeyIt wrote:
When it gets to loop trough objects that contain objects it gets very messy in my point of view, using . makes it definitely easier for me.
Besides saving one character (obj.[...] vs obj[][...]), what benefit does it have?
Quote:
But Object(obj) and IsObject(obj) will still work, right?
IsObject(obj) is not "a context which requires a non-object value" and therefore would not cause the default value to be invoked. Object(obj) didn't work in the first place, so obviously it won't "still" work.
Quote:
While is important here on one side because a key might be deleted or added inside the loop/while.
Can your While loop handle insertions/deletions at <= A_Index?
Quote:
... this is the best method I think because it is very fast.
Evaluating the condition expression every iteration (as While does) has a performance cost which is not present with Loop.
Quote:
WideObject lets you enumerate trough any key beside [""], so a key can be anything, string or digit or object.
Using [] would allow any key to be used.

As far as I can tell, WideObject will be obsolete when I implement proper enumeration methods. If you can think of a better reason to allow obj.[...], I'd like to hear it.
Back to top
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 16, 2010 7:25 am    Post subject: Reply with quote

I agree with Lexikos on this one. Syntax obj.[key] leads to confusion, can be missed in faster code reading, and leads to infinite dot syntax which is hard to read and understand.

About default value, that which have no key if I understand correctly, I think it overlaps a bit with idea for new meta operators like __add, __unary etc... in this case __tostring. Both can be and will be used in non-object scenarios but unlike default value, those can be used in object scenarios too.

So, instead default value , AHKL calls __toString(). If nothing else is implemented, this value acts the same as currently envisioned default value (since it can be used in all non-object scenarios). In expressions, AHKL will check for operation meta function (__add) in left 2 right order (which is mathematically correct btw). If objects are not the same type, the left object should be able to tell and return empty string (for instance by checking presence of mandatory keys and/or some values - obj[__type] - of an right object or any other way, ). If both objects are the same type it returns result which can be object itself in which case its defalt value is taken (__toString i.e. default). Unary operators work the same, first call unary meta function, then if result is an object, take its default value.

So, to be short, AHKL calls _toString of an object when it encounters it in non-object expressions except in some scenarios where explicit "handlers" are defined when such functions take precedence.
_________________


Last edited by majkinetor on Tue Feb 16, 2010 9:18 am; edited 9 times in total
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4653
Location: AHK Forum

PostPosted: Tue Feb 16, 2010 8:42 am    Post subject: Reply with quote

Actually currently it would save 2 characters and when default value is implemented 1.
Code:
o["",0]
o.[0]


Quote:
Object(obj) didn't work in the first place

I meant Object(&obj) and newobj:=obj.

Quote:
Can your While loop handle insertions/deletions at <= A_Index?
There is no option to insert something at A_Index. Inserted key will be placed at the end. When a key is deleted, last key is put in place of deleted key. So deletion at "< A_Index" will move last key and you might not catch it in the while loop.
Quote:
Evaluating the condition expression every iteration (as While does) has a performance cost which is not present with Loop.

Using a loop you will still need to check total counted items in your object and again you would loose that little performance, right?
In general it is so fast that it really does not matter Smile
Code:
SetBatchLines,-1
o:=WideObject()
Loop 100000
   o[A_Index]:=A_Index
start:=A_TickCount
Loop % o["",0]
   val:=o[A_Index]
loop:=A_TickCount-start ; ~281 ms
start:=A_TickCount
While % o["",0]>=A_Index
   val:=o[A_Index]
while:=A_TickCount-start ; ~344 ms
MsgBox % "Loop took " loop " msec`nWhile took " while " msec"


Quote:
Using [] would allow any key to be used.

That will be great Wink

Quote:
As far as I can tell, WideObject will be obsolete when I implement proper enumeration methods. If you can think of a better reason to allow obj.[...], I'd like to hear it.
I am waiting for this feature impatiently.
In the end I agree with you, [] default value will be the way to go.
Thanks very much for your time Wink
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7299
Location: Australia

PostPosted: Tue Feb 16, 2010 10:07 am    Post subject: Reply with quote

majkinetor wrote:
Whats your thoughts on that ?
I don't have any concrete plans for either, yet. Meta-operators would provide a lot more control and have a wider range of applications, so will probably get priority. However, there are still some areas they don't cover.

There are a few different contexts an object can be used in that might require its default value to be retrieved:
  • In an expression, with an unsupported operator.
  • In a built-in function, when it attempts to "coerce" a parameter to a particular type.
  • In traditional syntax, via a variable.
Each case has its own limitations, ways of handling memory, etc. Also, traditional syntax works in two stages: 1) measure all substrings and ensure buffer space, 2) build the string. This would mean that either the object would be invoked twice (unacceptable) or the value must be cached between the first and second stages (perhaps in the variable through which the object is accessed), taking care not to re-invoke the object in the second stage but conversely taking care not to use a stale value. There are bound to be other issues; basically, the apparent complexity is reason enough to postpone its implementation.

On the other hand, meta-operators would be invoked only during expression evaluation, immediately before attempting the usual handling of the operator. This seems more reasonable. Default values could be implemented with much the same code, but in that case would be more limited in scope (no support for built-in functions or traditional syntax).
Quote:
So, instead default value , AHKL calls __toString().
I think maybe you're confusing "default value" (a loosely-defined concept) with obj[], which is one form it could take. obj.__toString() is another form it could take. Either way the functionality could be the same; neither syntax implies much difference.
Quote:
If both objects are the same type it returns result which can be object itself in which case its defalt value is taken (__toString i.e. default). Unary operators work the same, first call unary meta function, then if result is an object, take its default value.
That sounds backwards to me. Here's what I was thinking:
  • For an operator op, if at least one operand is an object, invoke its __op meta-function. Like other meta-functions, if it returns a value, use it and treat the operation as complete.
  • If no meta-operator handled the operation, invoke the appropriate mechanism to convert the object to a usable value (obj[], obj.__toString() or perhaps obj.__toInt() where appropriate).
  • If none of the above handled it, use the current behaviour: treat the object as an empty string. (When a boolean value is needed - as in if(obj) - I think objects should unconditionally be true.)
I'd prefer obj[], since handling it in __Get seems appropriate. Having just __toString wouldn't seem right for math ops, but having a meta-function for each type seems unnecessarily complex.
HotKeyIt wrote:
Actually currently it would save 2 characters and when default value is implemented 1.
The only connection between a hypothetical "default value" feature and obj[] syntax is that it might end up invoking the same method.
Quote:
That will be great Wink
What's stopping you then? I already showed how you can implement it...
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 16, 2010 11:35 am    Post subject: Reply with quote

Quote:
I'd prefer obj[], since handling it in __Get seems appropriate. Having just __toString wouldn't seem right for math ops, but having a meta-function for each type seems unnecessarily complex.

I agree about naming. The proposal was not about names anyway but about concept. Also, __toString isn't that illogical for math operations because they work that way. Variable is essentially the string (or comes from it), so passing a string result anywhere in AHK would produce desired results. Maybe not the fastest one but the correct one.

Quote:
The apparent complexity is reason enough to postpone its implementation.

OK then, default value is good step forward in any case.
_________________
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4653
Location: AHK Forum

PostPosted: Tue Feb 16, 2010 2:50 pm    Post subject: Reply with quote

Lexikos wrote:
Quote:
That will be great Wink
What's stopping you then? I already showed how you can implement it...


This will slow down accessing index currently because obj["",0] will give you the value straight away, while obj[] will be using __Get method.

Code:
While % o[][0]>=A_Index
   val:=o[A_Index]
;will be around 1.5 times slower than
While % o["",0]>=A_Index
   val:=o[A_Index]


I will wait until its implemented and try again Smile
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 16, 2010 4:18 pm    Post subject: Reply with quote

Lexikos wrote:
majkinetor wrote:
If both objects are the same type it returns result which can be object itself in which case its defalt value is taken (__toString i.e. default)
That sounds backwards to me. Here's what I was thinking:
  • For an operator op, if at least one operand is an object, invoke its __op meta-function. Like other meta-functions, if it returns a value, use it and treat the operation as complete.

What I was thinking is, if I have code like:
Code:
Msgbox % p := c + d
MsgBox % q := (5 + (p := c+d))

where c & d are complex numbers, the addition is complex number again, and sometimes real number. In case of complex number, if you treat operation as finished, MsgBox shows nothing in first case. So, if result of object expression is another object, its again subject to _op meta function call depending on "incoming" expression. In first case AHKL will have to call default value on p, while in second case it doesn't have to do so, but calls __add(p, 5). Then it will have to use q[] if q is an object, if not, the %q%.
_________________
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7299
Location: Australia

PostPosted: Wed Feb 17, 2010 6:18 am    Post subject: Reply with quote

HotKeyIt wrote:
I will wait until its implemented and try again Smile
What is it you think will be implemented? What obj[] does won't change. I'd explain but I'd just be repeating myself.
Some wise guy wrote:
In general it is so fast that it really does not matter

majkinetor wrote:
In case of complex number, if you treat operation as finished, MsgBox shows nothing in first case.
I see what you mean: if the operation yields an object, it may need to be converted to a string. However, this would be completely separate from processing of the operator. For instance, it would be done for the final result of the expression, but not for return (which passes the "typed" value back to the caller) or standalone expressions (for which the result is discarded).
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4653
Location: AHK Forum

PostPosted: Wed Feb 17, 2010 7:11 am    Post subject: Reply with quote

Lexikos wrote:
HotKeyIt wrote:
I will wait until its implemented and try again Smile
What is it you think will be implemented? What obj[] does won't change. I'd explain but I'd just be repeating myself.

I thought this is going to work in future, am I wrong Confused
Code:
o:=Object()
0[]:="ahk"
MsgBox % o[]

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Wed Feb 17, 2010 8:38 am    Post subject: Reply with quote

It can't be that simple because objects default value may be dynamic. So, its a function call.

As far as I understood it HotKyeIt, Lexikos will implement default values in a scenarios we talked above (objects in expressions, dll calls etc...), i.e. in those that require objects to be treated as variables.

Other then that, it will probably be the same thing as o[] we have today, so speed will be the same.

BUT, I think you may have a valid question there. For non-dynamic scenarios, like the one you mentioned, it might be good to avoid function call. On the other hand, its questionable in what and how many occasions will default value actually be static in regular usage. It will at least have to return value of some property of the object. Your code would make sense only if default values get implemented in a way that it keeps the name of the object's field that is to be taken as default (so, in above code o.ahk or o.ahk() is used as default value)
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies.    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3 ... 26, 27, 28 ... 68, 69, 70  Next
Page 27 of 70

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group