YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Confusing "in" and "rawin"
toschenk
#1 Posted : Thursday, October 20, 2016 2:38:09 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/20/2016(UTC)
Posts: 2

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi,

the operator "in" and the method "rawin" for tables seem to do exactly the same thing. Example:

Code:

local delegate = {
  foo = 1
}

local table = {
  bar = 1
}

table.setdelegate(delegate)

print("'foo' in table: " + ("foo" in table))
print("'foo' rawin table: " + table.rawin("foo"))


Both checks return 'false'. As I understood the documentation, only 'rawin' is supposed to return 'false' here, is that correct?

A possible fix would be (current github version):
Code:

diff -Nur a/squirrel/sqvm.cpp b/squirrel/sqvm.cpp
--- a/squirrel/sqvm.cpp 2016-10-20 16:28:00.643738448 +0200
+++ b/squirrel/sqvm.cpp 2016-10-20 16:29:41.603737179 +0200
@@ -956,7 +956,7 @@

                         } continue;
             case _OP_CMP:   _GUARD(CMP_OP((CmpOP)arg3,STK(arg2),STK(arg1),TARGET))  continue;
-            case _OP_EXISTS: TARGET = Get(STK(arg1), STK(arg2), temp_reg, GET_FLAG_DO_NOT_RAISE_ERROR | GET_FLAG_RAW, DONT_FALL_BACK) ? true : false; continue;
+            case _OP_EXISTS: TARGET = Get(STK(arg1), STK(arg2), temp_reg, GET_FLAG_DO_NOT_RAISE_ERROR, DONT_FALL_BACK) ? true : false; continue;
             case _OP_INSTANCEOF:
                 if(type(STK(arg1)) != OT_CLASS)
                 {Raise_Error(_SC("cannot apply instanceof between a %s and a %s"),GetTypeName(STK(arg1)),GetTypeName(STK(arg2))); SQ_THROW();}

absence
#2 Posted : Sunday, October 23, 2016 3:02:49 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 105
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 9 time(s) in 9 post(s)
as long as i remember the in operator was "raw" and did NOT employ delegation. I guess the documentation for "rawin" metamethod is simply wrong.
(BOTH should return false)
directly GETTING a value will emply delegation, so you can simply use a surrogate like

Code:

function delegate_in(needle,haystack)
{
  try{
    haystack[needle] ;  //unless compiler optimizes such expressions away one day, this is a valid expression
    return true ;
  } catch (x) { }
  return false ;
}


but you can't do it vice versa (emulate a non-delegate "in") without costly iterations. So the in operator is fine as it is.

I wouldn't change that behaviour but rather fix the docs about rawin (which erroneously suggest the "in" operator is employing delegation - which it definitely does not, should not, and it's documentation doesn't say anything explicit about it, but implicit it does... it tests "if a key is in a table". I wouldn't assume it's checking delegates by that wording).

Hence, the rawin default delegate is somewhat obsolete FOR TABLES. (it quite does make sense for classes and instances, where the 'in' operator is not available)
toschenk
#3 Posted : Tuesday, October 25, 2016 2:40:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/20/2016(UTC)
Posts: 2

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Quote:
as long as i remember the in operator was "raw" and did NOT employ delegation.


Quote:
it quite does make sense for classes and instances, where the 'in' operator is not available


This implies, that the rawin metamethod would be completely redundant since one could extend the 'in' operator to also accept classes and instances and to remove rawin completely.

Quote:
but you can't do it vice versa (emulate a non-delegate "in") without costly iterations.


And that - to my understanding - is exactly the reason, why there is a rawin metamethod in the first place.

I think the question is what Alberto originally intended/currently intends and the cases for which 'in' is currently used in squirrel scripts/projects. If most projects use 'in' with non-delegate meaning, I think it would be wrong to change 'in' to invoke delegation, I agree with you on that. However, in that case it would be great to add another metamethod 'delegatein' or similar, which does invoke delegation and to mark 'rawin' as deprecated in the documentation.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.087 seconds.