Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: OOP or not

I'm working on Juno with Julia.

I don't know if Julia supports OOP or not.

For example, is there something like class or struct of c++?

How to declare it with members such as a data or a function?

like image 925
Yves Avatar asked Nov 17 '15 11:11

Yves


People also ask

Is Julia a functional or OOP?

Julia is not object-oriented in the full sense because you cannot attach methods to Julia's objects ("types"). The types do seem very similar to objects though. However, since they do not have their own associated methods and there is no inheritance the objects themselves don't do the acting.

Does Julia have object-oriented?

Julia does not have classes in the object-oriented sense as it is not an object-oriented language. The main paradigm of Julia which replaces object-oriented programming is multiple dispatch, see here.

Does Julia support object-oriented programming?

Julia is a multi-paradigm language — and that includes support for object-oriented sorts of patterns.

Why are there no classes in Julia?

Julia does not have classes. Instead we define new types and then define methods on those types. Methods are not "owned" by the types they operate on. Instead, a method can be said to belong to a generic function of the same name as the method.


1 Answers

When in doubt, read the documentation...

https://docs.julialang.org/en/v1/manual/types/#Composite-Types-1

Long story short:

struct MyType     a::Int64     b::Float64 end  x = MyType(3, 4)  x.a 

EDIT: Methods are defined outside the type definition, e.g.

function double(x::MyType)     x.a *= 2 end 

Methods do not live inside the type, as they would do in C++ or Python, for example. This allows one of the key features of Julia, multiple dispatch, to work also with user-defined types, which are on exactly the same level as system-defined types.

like image 51
David P. Sanders Avatar answered Sep 23 '22 10:09

David P. Sanders