pooooky
pooooky

Reputation: 520

How to refer the struct itself in Julia

I have this code:

struct MyStruct
    text::String
    
    function MyStruct(_text::String)
        text = _text
        # do other things
    end
end

When I wrote this, I realized that Julia is not recognizing text as MyStruct's field. How can I do something like this in Julia like in Python?

self.text = _text

Upvotes: 4

Views: 1197

Answers (2)

David Grayson
David Grayson

Reputation: 87376

Taking a quick glance at the constructors documentation and trying out the playground, I was able to come up with this:

struct MyStruct
    text::String   
    function MyStruct(_text::String)
        s = new(_text * "def")
        # do other things
        s
    end
end

s = MyStruct("abc")
println(s.text)     # abcdef

Upvotes: 3

phipsgabler
phipsgabler

Reputation: 20950

Don't try to imitate Python. Julia is not object oriented.

You could emulate a Python-style constructor by

mutable struct MyStruct
    text::String
    
    function MyStruct(text::String)
        self = new()
        self.text = some_stuff(text)
        return self
    end
end

but for this to work the struct needs to be mutable. Then you can set up an uninitialize instance with new() and overwrite the fields.

Note that this is more equivalent to a combination of both __init__ and __new__. In Python, the new part is (99% of the time) already done for you, and you just mutate the already created empty object in __init__. In Julia, you have to do both on your own; this especially also requires returning the new value at the end of the constructor!

Having said all this, it's not often useful to write that way. More idiomatic would be just

struct MyStruct
    text::String

    MyStruct(text::String) = new(some_stuff(text))
end

unless you absolutely need the struct to be mutable (which has consequences with respect to memory layout and possible optimizations).

And also read up on the difference between inner and outer constructors. If you want the above to be the only valid way to construct MyStruct, this is fine. If you want "convenience constructors", e.g. with default arguments or conversions from other types, prefer outer constructors (where you don't have new but recursively call constructors until an inner constructor is reached).

Upvotes: 5

Related Questions