-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix nim-lang/RFCs#18; add reflection.moduleSymbols
to list symbols in a module
#9560
Conversation
I don't like the feature (the implementation mostly follows the existing pattern). Previously I told people to use |
I am closing as rejected. Adding API's like this needs careful thinking instead of exposing all internal structures. |
Question: Can we open up it again, I find this very useful.
A disadvantage of this feature is the non-localness literally allowing any local code change to break your code, however at least at compile time. @Araq wrote:
Understand, so assignments could be created for non local vars. But I think bindSym hackery already allows such things, okay dynamically scoped bindSyms are experimental yet. |
yes, it is very useful and an often requested feature (see some links i added on top), I've finally re-opened this PR and updated it. @Araq PTAL refs https://forum.nim-lang.org/t/1817#37522
update since original PR:
why std/reflectionthis is the most sensible place for both moduleSymbols (this PR) and the other reflection routines from #17641
nim is a static compiled language, so CT being implied by reflection will surprise no-one; in any case I've added a doc comment to make it clear that the scope of this module is CT reflection. For RT reflection / RTTI, we already have a module, https://nim-lang.github.io/Nim/typeinfo.html std/sourceinfo doesn't reflect the intended scope as well as std/reflection. why not
|
422beea
to
36cde6d
Compare
reflections.moduleSymbols
to list symbols in a module
reflections.moduleSymbols
to list symbols in a modulereflections.moduleSymbols
to list symbols in a module
reflections.moduleSymbols
to list symbols in a modulereflection.moduleSymbols
to list symbols in a module
5e43e07
to
0e13536
Compare
ping @Araq, see updates in #9560 (comment) |
0e13536
to
a541fea
Compare
Is there a general overview of what reflection capabilities Nim should have. and what are feasible? While I think this has merit, modules aiming to serve a functionality like "reflection" are best designed as a whole, rather than piecemeal. For example, see how https://blog.golang.org/laws-of-reflection and Go's reflection package work. |
It's pretty simple: Nim should never have reflection like Java and C# because Dead-code elimination becomes fragile or impossible. In general, musing about what would harm an optimizer is a good design principle if "easy to reason about" is not just a buzzword phrase. |
@Araq I fail to see how this PR impacts dead code elimination in any way. Example
when true:
import std/[macros, reflection, strutils]
macro moduleSymbolsTest(mymod: typed, enablePrivate: static bool = false): untyped =
for ai in mymod.moduleSymbols(enablePrivate):
if $ai == "cmpIgnoreCase": return ai
when defined case4a:
const foo1 = moduleSymbolsTest(strutils, enablePrivate = true)
echo foo1("ab", "Ab")
when defined case4b:
let foo2 = moduleSymbolsTest(strutils, enablePrivate = true)
echo foo2("ab", "Ab") The feature is useful, and has been requested many, many times over; can we please merge this? Most other languages I've used (D, go, java, python3, js, C#, ruby ...) allow this kind of reflection APIs, those languages didn't stop being analyzable or optimizable because of this; it's a very useful thing to have when you need it. |
I didn't mean to imply that it does impact it. I merely replied to @Varriount and outlined some of the design principles that we follow. |
ping @Araq this keeps coming up, eg https://discord.com/channels/371759389889003530/768367394547957761/894724802185687071 (for nimscripter) |
This would be very useful to me, I want to extern the exposed types/procs in a module compiled with the cpp backend so it can be used from other languages. Having a way to simply list the public contents of a module would make this a lot easier. We already have std/importutils so this seems like the next logical step. |
In order to proceed
|
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
links