Re: [julia-users] Re: Julia equivalent to a static member function in C++/MATLAB

2015-09-26 Thread Joel Andersson
Yes, this is all clear. Joel


[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB

2015-09-25 Thread Eric Forgy
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

2015-09-25 Thread 'Greg Plowman' via julia-users
 

> 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

2015-09-25 Thread Joel Andersson
Great, thanks! Joel


[julia-users] Re: Julia equivalent to a static member function in C++/MATLAB

2015-09-25 Thread Steven G. Johnson


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) = .