Print out the instance methods defined class? or the path where the current class is?

I’ve been thinking about this a lot these past days.

It’s tricky because Ruby is dynamic so everything is a VALUE and there’s a lot of runtime information that isn’t present in Crystal.

That said, I wonder if it would be nice for Class to provide methods like subclasses, methods, etc., that return runtime values. These can be used to quickly introspect types without having to open a browser or source code to see the API. Or they can be used to maybe generate documentation or transform the code to something else, like a schema. It’s tricky because type restrictions aren’t solved, so for now they can just be represented as strings.

So, the idea would be that Class#sublcasses doesn’t return an Array of Class. That’s kind of impossible. Same with Class#methods, there’s no existing type that we could return there.

But we could introduce intermediary types.

Here’s what I got so far:

https://play.crystal-lang.org/#/r/dfjf

Thoughts?

The nice thing about this is that all of this runtime information is only produced if asked. If you don’t call any of that nothing of that will exist at runtime.

You could also use this in the interpreter to quickly find out about methods and types (I just tried it and all the code runs fine in interpreted mode).

10 Likes