Re: [julia-users] Re: Julia equivalent to a static member function in C++/MATLAB
Yes, this is all clear. Joel
[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB
You probably know this, but the way dispatch works in Julia, it will look for a function matching the signatures. Since, presumably, MyMatrix is not in base Julia, that zeros function will do whatever you want it to do because you have yo implement it.
[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB
> In Julia, you can do the same thing, it is just spelled differently. You > could do x = zeros(MyMatrix, 3, 4) where you have defined Base.zeros(::Type{MyMatrix}, m, n) = . Show trimmed content Of course in Julia you can do anything you want. However, is it recommended to redefine the meaning of generic functions? I would assume zeros(MyMatrix,3,4) creates a 3x4 Array{MyMatrix,2}
[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB
Great, thanks! Joel
[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB
On Friday, September 25, 2015 at 11:56:46 AM UTC-4, Joel Andersson wrote: > > Hi, > > In C++ or in MATLAB I would use static member functions to keep the > constructors nice and simple, e.g. > class MyMatrix { > public: > static MyMatrix zeros(int n, int m); > static MyMatrix ones(int n, int m); > static MyMatrix eye(int n); > ... > }; > > Which allows me to create class instances with an IMO natural syntax, > which should also be relatively efficient due to return value optimization: > MyMatrix x = MyMatrix::zeros(3,4); > > In Julia, you can do the same thing, it is just spelled differently. You could do x = zeros(MyMatrix, 3, 4) where you have defined Base.zeros(::Type{MyMatrix}, m, n) = .