Информация об изменениях

Сообщение Re: Отказ от ООП - пример C# vs С++ vs Rust от 17.09.2023 9:54

Изменено 17.09.2023 9:55 kov_serg

Re: Отказ от ООП - пример C# vs С++ vs Rust
Здравствуйте, Shmj, Вы писали:

S>Давайте пока с простейшего примера, потом, возможно, добавлю чуть интереснее:

S>C# 42 строки
S>C++ 59 строк
S>Rust 73 строки

S>Выводы какие? Даже на простейшем примере видно удобство ООП. А теперь представьте что пример будет реальным — сразу 50 тыс. строк превращаются в 100 тыс.

Никакие. Язык без ООП lua:
require "object"

Widget={ render=function(self) print(self.name.."="..self.id)  end }
TextEditWidget=object(Widget) { render=function(self) inherited(self,"render")() print("Text="..self.text)  end }
CheckWidget=object(Widget) { render=function(self)  inherited(self,"render")() print(("IsChecked=%s"):format(self.is_checked))  end }

local widgets={}
table.insert(widgets, object(TextEditWidget) { id=1, name="Text1", text="text" })
table.insert(widgets, object(CheckWidget) { id=2, name="Check1", is_checked=true })
for i,widget in pairs(widgets) do widget:render() end

10 строк и 12 строк эмуляция ооп
  object.lua
function object(parent)
    return function(prm)
        local res=setmetatable(prm or {},{__index=parent})
        if res.init then res:init() end
        return res
    end
end
function inherited(obj,fn)
    local class=(getmetatable(obj) or {__index={}}).__index
    local parent=(getmetatable(class) or {__index={}}).__index
    return function(...) if parent~=nil then local f=parent[fn] if f~=nil then return f(obj,fn) end end end
end
Re: Отказ от ООП - пример C# vs С++ vs Rust
Здравствуйте, Shmj, Вы писали:

S>Давайте пока с простейшего примера, потом, возможно, добавлю чуть интереснее:

S>C# 42 строки
S>C++ 59 строк
S>Rust 73 строки

S>Выводы какие? Даже на простейшем примере видно удобство ООП. А теперь представьте что пример будет реальным — сразу 50 тыс. строк превращаются в 100 тыс.

Никакие. Язык без ООП lua:
require "object"

Widget={ render=function(self) print(self.name.."="..self.id)  end }
TextEditWidget=object(Widget) { render=function(self) inherited(self,"render")() print("Text="..self.text)  end }
CheckWidget=object(Widget) { render=function(self)  inherited(self,"render")() print(("IsChecked=%s"):format(self.is_checked))  end }

local widgets={}
table.insert(widgets, object(TextEditWidget) { id=1, name="Text1", text="text" })
table.insert(widgets, object(CheckWidget) { id=2, name="Check1", is_checked=true })
for i,widget in pairs(widgets) do widget:render() end

10 строк и 12 строк эмуляция ооп
  object.lua
function object(parent)
    return function(prm)
        local res=setmetatable(prm or {},{__index=parent})
        if res.init then res:init() end
        return res
    end
end
function inherited(obj,fn)
    local class=(getmetatable(obj) or {__index={}}).__index
    local parent=(getmetatable(class) or {__index={}}).__index
    return function(...) if parent~=nil then local f=parent[fn] if f~=nil then return f(obj,...) end end end
end