Difference between type and struct

Georgery :

I'm trying to learn Julia and am reading a book that shows the following two code example in a chapter about composite types:

1.

type Points
    x::Int64
    y::Int64
    z::Int64
end

2.

struct Point
    x::Int
    y::Int
    z::Int
end

The book however does not explain, when to use struct and when to use type.

What is the difference?

phipsgabler :

This is quite a mess in your sources, since it mixes different meanings from non-compatible epochs in the history of the language.

  • Originally (pre 0.7, I think?), composite types were declared with either type or immutable, where type was used for mutable types (there also was bitstype for what is now called "primitive types").
  • Now, we have mutable struct and struct for the sampe purposes (and, consistently with it, primitive type and abstract type).

So, basically the names have changed so that all ways to define types becomes a bit more consistent, and immutable structs have become the "unmarked" case.

"Mutable" in this context that you can't reassign a field (p.x = 3). It does not mean that the contents of a field cannot be changed, it they happen to be mutable (something.v[1] = 2 will also work if something is an immutable type!).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33228&siteId=1