diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-08-24 10:17:54 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-08-24 10:17:54 -0300 |
commit | 8c8a91f2ef7acccb99e3737913faad8d48b39571 (patch) | |
tree | 0807151944b7f7fd00eedfcfe94b4ee26fe25b21 /testes/events.lua | |
parent | f99509581ee73c1c2dbddb3398e87c098771d31f (diff) | |
download | lua-8c8a91f2ef7acccb99e3737913faad8d48b39571.tar.gz lua-8c8a91f2ef7acccb99e3737913faad8d48b39571.tar.bz2 lua-8c8a91f2ef7acccb99e3737913faad8d48b39571.zip |
Deprecated the emulation of '__le' using '__lt'
As hinted in the manual for Lua 5.3, the emulation of the metamethod
for '__le' using '__le' has been deprecated. It is slow, complicates
the logic, and it is easy to avoid this emulation by defining a proper
'__le' function.
Moreover, often this emulation was used wrongly, with a programmer
assuming that an order is total when it is not (e.g., NaN in
floating-point numbers).
Diffstat (limited to '')
-rw-r--r-- | testes/events.lua | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/testes/events.lua b/testes/events.lua index 21a822a7..c4d43d51 100644 --- a/testes/events.lua +++ b/testes/events.lua | |||
@@ -1,4 +1,4 @@ | |||
1 | -- $Id: testes/events.lua $ | 1 | -- $Id: testes/events.lua 2018-07-25 15:31:04 -0300 $ |
2 | -- See Copyright Notice in file all.lua | 2 | -- See Copyright Notice in file all.lua |
3 | 3 | ||
4 | print('testing metatables') | 4 | print('testing metatables') |
@@ -217,6 +217,13 @@ t.__lt = function (a,b,c) | |||
217 | return a<b, "dummy" | 217 | return a<b, "dummy" |
218 | end | 218 | end |
219 | 219 | ||
220 | t.__le = function (a,b,c) | ||
221 | assert(c == nil) | ||
222 | if type(a) == 'table' then a = a.x end | ||
223 | if type(b) == 'table' then b = b.x end | ||
224 | return a<=b, "dummy" | ||
225 | end | ||
226 | |||
220 | function Op(x) return setmetatable({x=x}, t) end | 227 | function Op(x) return setmetatable({x=x}, t) end |
221 | 228 | ||
222 | local function test () | 229 | local function test () |
@@ -236,15 +243,6 @@ end | |||
236 | 243 | ||
237 | test() | 244 | test() |
238 | 245 | ||
239 | t.__le = function (a,b,c) | ||
240 | assert(c == nil) | ||
241 | if type(a) == 'table' then a = a.x end | ||
242 | if type(b) == 'table' then b = b.x end | ||
243 | return a<=b, "dummy" | ||
244 | end | ||
245 | |||
246 | test() -- retest comparisons, now using both `lt' and `le' | ||
247 | |||
248 | 246 | ||
249 | -- test `partial order' | 247 | -- test `partial order' |
250 | 248 | ||
@@ -266,14 +264,6 @@ t.__lt = function (a,b) | |||
266 | return next(b) ~= nil | 264 | return next(b) ~= nil |
267 | end | 265 | end |
268 | 266 | ||
269 | t.__le = nil | ||
270 | |||
271 | assert(Set{1,2,3} < Set{1,2,3,4}) | ||
272 | assert(not(Set{1,2,3,4} < Set{1,2,3,4})) | ||
273 | assert((Set{1,2,3,4} <= Set{1,2,3,4})) | ||
274 | assert((Set{1,2,3,4} >= Set{1,2,3,4})) | ||
275 | assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-) | ||
276 | |||
277 | t.__le = function (a,b) | 267 | t.__le = function (a,b) |
278 | for k in pairs(a) do | 268 | for k in pairs(a) do |
279 | if not b[k] then return false end | 269 | if not b[k] then return false end |
@@ -281,10 +271,15 @@ t.__le = function (a,b) | |||
281 | return true | 271 | return true |
282 | end | 272 | end |
283 | 273 | ||
284 | assert(not (Set{1,3} <= Set{3,5})) -- now its OK! | 274 | assert(Set{1,2,3} < Set{1,2,3,4}) |
275 | assert(not(Set{1,2,3,4} < Set{1,2,3,4})) | ||
276 | assert((Set{1,2,3,4} <= Set{1,2,3,4})) | ||
277 | assert((Set{1,2,3,4} >= Set{1,2,3,4})) | ||
278 | assert(not (Set{1,3} <= Set{3,5})) | ||
285 | assert(not(Set{1,3} <= Set{3,5})) | 279 | assert(not(Set{1,3} <= Set{3,5})) |
286 | assert(not(Set{1,3} >= Set{3,5})) | 280 | assert(not(Set{1,3} >= Set{3,5})) |
287 | 281 | ||
282 | |||
288 | t.__eq = function (a,b) | 283 | t.__eq = function (a,b) |
289 | for k in pairs(a) do | 284 | for k in pairs(a) do |
290 | if not b[k] then return false end | 285 | if not b[k] then return false end |
@@ -376,6 +371,7 @@ t1 = {}; c = {}; setmetatable(c, t1) | |||
376 | d = {} | 371 | d = {} |
377 | t1.__eq = function () return true end | 372 | t1.__eq = function () return true end |
378 | t1.__lt = function () return true end | 373 | t1.__lt = function () return true end |
374 | t1.__le = function () return false end | ||
379 | setmetatable(d, t1) | 375 | setmetatable(d, t1) |
380 | assert(c == d and c < d and not(d <= c)) | 376 | assert(c == d and c < d and not(d <= c)) |
381 | t2 = {} | 377 | t2 = {} |