Bridge the gap, please! (from Matlab to Julia)

Julia’s version of varargin is multiple dispatch. I mean, not literally, but in general you don’t just customize behavior of functions by the number of inputs, you just define multiple methods.

function f(x)
    ...
end

function f(x, y)
    ...
end

For your particular example, just overload the functions you need

mean(b::B) = mean(b.x)

etc.

3 Likes

@lmiq
Yes, this approach seems to fit, thanks for showing!
Need to experiment with it.

@pdeffebach
Not sure how to apply.
As said, could not implement e.g. fun3 with your approach.

You need to provide the error message for me to help you more.

But overall, I highly suggest you read the documentation more. It seems like you are trying to port highly specific matlab idioms to Julia. If you are encountering problems with implementing operate or writing a new method for mean, the docs will help.

Sorry, I was not even capable of formulating my running example using operate.
For studying the documentation, you are definitively right.
Talk to you tomorrow.

Also found this:
https://stackoverflow.com/questions/39133424/how-to-create-a-single-dispatch-object-oriented-class-in-julia-that-behaves-l

Jeff Bezanson showed this (struct-less) approach:

In julia v0.5 there is an especially slick way to do this due to the fact that closures represent their captured variables as object fields automatically. For example:

julia> function Person(name, age)
        getName() = name
        getAge() = age
        getOlder() = (age+=1)
        ()->(getName;getAge;getOlder)
       end
Person (generic function with 1 method)

julia> o = Person("bob", 26)
(::#3) (generic function with 1 method)

julia> o.getName()
"bob"

julia> o.getAge()
26

julia> o.getOlder()
27

julia> o.getAge()
27

plus the following comment with a possible improvement.

I think I am done here. Thanks for your company and patience!