-
Notifications
You must be signed in to change notification settings - Fork 17
[RFC] Consider adding callinfo for introspecting method calls #189
Description
callinfo would be a language-managed variable, similar to self, that provides some introspective information about the caller of a method. As a language variable, callinfo would also be added as a new keyword.
Having access to meta information about the caller of a method is primarily helpful for debugging and tooling inside of Myst. The most obvious usecases are the Spec and Assert libraries.
Most spec libraries provide location information about failing specs to help users locate them quickly and easily. In Myst, this isn't a practical possibility yet, because the library has no way of accessing that location information without requiring the user to pass in __FILE__ and __LINE__ values for every it block.
Adding callinfo would provide that location information to these modules automatically, without any burden on the user. It would also avoid having conditional semantics for the magic constants, which I've never particularly liked.
Semantics
Referencing callinfo outside of a method definition will always return nil.
Inside of a method definition, callinfo returns a Map with these entries:
:file: the file containing the call to this method.:line: the line number of the call to this method.:dir: the directory containing the file with the call to this method.
For simplicity, this Map is newly-created every time callinfo is invoked. This ensures both "psuedo-immutability", and that the values are accurate for the current call.
Example usage
Retrieving the location information of a Call using callinfo could look something like this:
def foo(a, b, c)
{line: line, file: file} = callinfo
STDOUT.puts("line <(line)>, file <(file)>")
end
foo(1, 2, 3) #=> line 6, file /Users/.../foo.mt