1 A compound assignment dispatches one read and one write of the same key. The 2 backing store below these metamethods lives under a different key, so the 3 property stays virtual throughout the test. 4 5 -- Testcase -- 6 {% 7 let log = []; 8 9 let o = proto({}, { 10 __get__(key) { 11 push(log, "get:" + key); 12 return rawget(this, "_" + key); 13 }, 14 __set__(key, val) { 15 push(log, "set:" + key + "=" + val); 16 rawset(this, "_" + key, val); 17 } 18 }); 19 20 o.n += 5; 21 o.n *= 2; 22 23 print(log, "\n"); 24 print(o.n, "\n"); 25 %} 26 -- End -- 27 28 -- Expect stdout -- 29 [ "get:n", "set:n=5", "get:n", "set:n=10" ] 30 10 31 -- End -- 32 33 The short-circuiting assignments read the property, but write only when the 34 current value does not already satisfy them. 35 36 -- Testcase -- 37 {% 38 let log = []; 39 40 let o = proto({}, { 41 __get__(key) { 42 return rawget(this, "_" + key); 43 }, 44 __set__(key, val) { 45 push(log, key + "=" + val); 46 rawset(this, "_" + key, val); 47 } 48 }); 49 50 o.a ??= 1; 51 o.a ??= 2; 52 o.b ||= 3; 53 54 print(log, "\n"); 55 print(o.a, " ", o.b, "\n"); 56 %} 57 -- End -- 58 59 -- Expect stdout -- 60 [ "a=1", "b=3" ] 61 1 3 62 -- End -- 63 64 Increment and decrement dispatch the read and the write of the same key. 65 66 -- Testcase -- 67 {% 68 let o = proto({ n: 1 }, { 69 __get__(key) { 70 return rawget(this, key); 71 }, 72 __set__(key, val) { 73 rawset(this, key, val); 74 } 75 }); 76 77 print(++o.n, " ", o.n--, " ", o.n, "\n"); 78 %} 79 -- End -- 80 81 -- Expect stdout -- 82 2 2 1 83 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt