I was wondering if anyone knew how to define overloaded methods in mata? I have an arbitrary example here:
Running this leads to several error messages:
Each function has a different signature for the arguments and these types of method definitions are fairly typical in other languages, so I was wondering if anyone else had come across anything related to this. Also, if this is covered somewhere in the documentation that I've overlooked I'd appreciate any references to existing documentation for this.
Code:
mata mata clear // Method if scalar is passed to the function real matrix sqelem(real scalar x) { real matrix matv matv = (J(x, 1, (1::x))) return((matv[1::rows(matv), 1]:*matv[1::rows(matv), 1])) } // Method if matrix is passed to the function real matrix sqelem(real matrix x) { return((x[1::rows(x), 1]:*x[1::rows(x), 1])) }
Code:
: mata clear : real matrix sqelem(real scalar x) { > real matrix matv > matv = (J(x, 1, (1::x))) > return((matv[1::rows(matv), 1]:*matv[1::rows(matv), 1])) > } : real matrix sqelem(real matrix x) { sqelem() already exists r(3000); : return((x[1::rows(x), 1]:*x[1::rows(x), 1])) 'return' found where almost anything else expected r(3000); : } expression invalid r(3000);
Comment