I was surprised to see that the Mata compiler seems to ignore much of the eltype and orgtype specifications when explicitly declaring variables inside functions. [Note: This is not the case for function return types and function argument types. Here the compiler works as I would have expected.]
I've tested the following in Stata 13 to 16.
The following runs through:
Code:
capture noi mata mata drop noerror() mata: void noerror() { `" --- ASSIGN TO REAL SCALAR --- "' real scalar rs `" --- rs = (1\2\3) --- "' rs = (1\2\3) rs eltype(rs) orgtype(rs) `" --- rs = J(3,3,3) --- "' rs = J(3,3,3) rs eltype(rs) orgtype(rs) `" --- rs = J(3,3,"def") --- "' rs = J(3,3,"def") rs eltype(rs) orgtype(rs) `" --- ASSIGN TO STRING SCALAR --- "' string scalar ss `" --- ss = J(3,3,"def") --- "' ss = J(3,3,"def") ss eltype(ss) orgtype(ss) `" --- ss = J(3,3,3) --- "' ss = J(3,3,3) ss eltype(ss) orgtype(ss) } noerror() end
The output is:
Code:
: noerror() --- ASSIGN TO REAL SCALAR --- --- rs = (1\2\3) --- 1 +-----+ 1 | 1 | 2 | 2 | 3 | 3 | +-----+ real colvector --- rs = J(3,3,3) --- [symmetric] 1 2 3 +-------------+ 1 | 3 | 2 | 3 3 | 3 | 3 3 3 | +-------------+ real matrix --- rs = J(3,3,"def") --- [symmetric] 1 2 3 +-------------------+ 1 | def | 2 | def def | 3 | def def def | +-------------------+ string matrix --- ASSIGN TO STRING SCALAR --- --- ss = J(3,3,"def") --- [symmetric] 1 2 3 +-------------------+ 1 | def | 2 | def def | 3 | def def def | +-------------------+ string matrix --- ss = J(3,3,3) --- [symmetric] 1 2 3 +-------------+ 1 | 3 | 2 | 3 3 | 3 | 3 3 3 | +-------------+ real matrix
Code:
capture noi mata mata drop error1() mata: void error1() { string scalar s2 s2 = 3 } end
Code:
. mata: ------------------------------------------------- mata (type end to exit) ----- : void error1() { > string scalar s2 > s2 = 3 type mismatch: string = real not allowed (1 line skipped) ------------------------------------------------------------------------------- r(3000);
Code:
capture noi mata mata drop error2() mata: void error2() { real scalar r1 r1 = "def" } end
Code:
. mata: ------------------------------------------------- mata (type end to exit) ----- : void error2() { > real scalar r1 > r1 = "def" type mismatch: real = string not allowed (1 line skipped) ------------------------------------------------------------------------------- r(3000);
Unfortunately, the documentation is not clear on this. I haven't found any clear statement anywhere about whether the above code that runs through should generate or should not generate an error.
Mata's strong type-checking features are wonderful and it would come as a bit of a disappointment if the only benefit of eltype/orgtype variable declarations inside functions were the readability of code. If that is the case, it would be very helpful to have it clearly stated in the manuals.
Any help or comment on this matter is greatly appreciated. Please let me know if I am overlooking or misstating something.
Best,
Daniel
0 Response to Mata compiler ignores eltypes / orgtypes for variable declarations
Post a Comment