Inheritance/Multiple: Difference between revisions

Content added Content deleted
Line 658: Line 658:
=={{header|Julia}}==
=={{header|Julia}}==


Julia supports inheritance via abstract types. In Julia, multiple dispatch allows objects of different types to have the same function interfaces. Julia also can support traits via parameters in type declarations or with macros. This makes multiple inheritance in Julia mostly unnecessary, except for the inconvenience of composing the data in a mixed type when declaring multiple similar types, for which there are macros.<br /> <br />For example, the functions <code> call(equipment, name) </code> and <code> video(equipment, filename) </code> could be used as generic interfaces to implement methods for a <code>Telephone</code>, a <code>Camera</code>, and a <code>SmartPhone</code>, and Julia would dispatch according to the type of the equipment.
Julia supports inheritance via abstract types. In Julia, multiple dispatch allows objects of different types to have the same function interfaces. Julia also can support traits via parameters in type declarations or with macros. This makes multiple inheritance in Julia mostly unnecessary, except for the inconvenience of composing the data in a mixed type when declaring multiple similar types, for which there are macros.<br /> <br />For example, the functions <code> call(equipment, name) </code> and <code> video(equipment, filename) </code> could be used as generic interfaces to implement methods for a <code>Telephone</code>, a <code>Camera</code>, and a <code>SmartPhone</code>, and Julia would dispatch according to the type of the equipment.<lang julia>
abstract type Phone end

struct DeskPhone <: Phone
book::Dict{String,String}
end

abstract type Camera end

struct kodak
roll::Vector{Array{Int32,2}}
end

struct CellPhone <: Phone
book::Dict{String,String}
roll::Vector{AbstractVector}
end

function dialnumber(phone::CellPhone)
println("beep beep")
end

function dialnumber(phone::Phone)
println("tat tat tat tat")
end

function snap(camera, img)
println("click")
push!(roll, img)
end

dphone = DeskPhone(Dict(["information" => "411"]))
cphone = CellPhone(Dict(["emergency" => "911"]), [[]])

dialnumber(dphone)
dialnumber(cphone)
</lang>{{output}}<pre>
tat tat tat tat
beep beep
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==