fork(1) download
  1. f3 = {__index = {}}
  2. f4 = {__index = {}}
  3. f3.__index.cross = function(a, b) return "cross 3" end
  4. f4.__index.cross = function(a, b) return "cross 4" end
  5.  
  6. function float3() local ob = {}; setmetatable(ob, f3); return ob end
  7. function float4() local ob = {}; setmetatable(ob, f4); return ob end
  8.  
  9. function is_compatible(a, b)
  10. return getmetatable(a) == getmetatable(b)
  11. end
  12.  
  13. function cross(a, b)
  14. if not is_compatible(a, b) then return "error" end
  15. return a.cross(b)
  16. end
  17.  
  18. print("wariant 33: " .. cross(float3(), float3()))
  19. print("wariant 44: " .. cross(float4(), float4()))
  20. print("wariant 34: " .. cross(float3(), float4()))
  21. print("wariant 43: " .. cross(float4(), float3()))
Success #stdin #stdout 0s 2788KB
stdin
Standard input is empty
stdout
wariant 33: cross 3
wariant 44: cross 4
wariant 34: error
wariant 43: error