Skip to content
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

Make better use of reflection/homoiconicity #338

Closed
GabrielKS opened this issue Mar 9, 2024 · 3 comments · Fixed by #339
Closed

Make better use of reflection/homoiconicity #338

GabrielKS opened this issue Mar 9, 2024 · 3 comments · Fixed by #339

Comments

@GabrielKS
Copy link
Contributor

Another catch-all issue for minor, low-priority refactors. There are some things we currently do using string manipulation, manually keeping track of lists of things, etc. that I think could be more elegantly accomplished using Julia's abilities to operate on itself. I'll start a list here:

@GabrielKS
Copy link
Contributor Author

strip_module_name currently uses string manipulation to turn PowerSystems.HydroDispatch into "HydroDispatch". There's a built-in Julia method that operates on types to do this:

julia> string(Base.nameof(PowerSystems.HydroDispatch))
"HydroDispatch"

There are probably more intricacies to figure out here but I'm pretty confident a reflection-based approach is feasible.

@GabrielKS
Copy link
Contributor Author

Sometimes we want to do the opposite, go from string to type, and currently we manually build a dictionary to avoid using eval. But there is a sort of inverse of Base.nameof:

julia> getproperty(PowerSystems, Symbol("HydroDispatch"))
PowerSystems.HydroDispatch

that I don't think has the same security risk as eval.

@daniel-thom
Copy link
Contributor

Yes, we should use nameof. I noticed this function in Base at some point and started using it…but didn’t go back and change the old code.

Regarding this one:

julia> getproperty(PowerSystems, Symbol("HydroDispatch"))
PowerSystems.HydroDispatch

I’m not sure I follow what you’re referring to in the code, but we use this approach when de-serializing JSON to types. We store the module and type names as strings and then look them back in this way. It is safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants