this post was submitted on 25 Mar 2024
542 points (98.6% liked)
Programmer Humor
19697 readers
97 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
You can do that in memory safe languages too. Kotlin extension functions, for example.
Extension functions are not the same at all. Extension functions are syntactic sugar. For example if you have an extension function like
You can call that function on an object by doing
object.DoSomething()
- Yes. But underneath it's the same as doingObjectExtension.DoSomething(object)
That function does not actually become part of the object, and you can't use it to override existing functions
A closer example of how to do something similar in a memory safe language would be - in C# - using something like Castle DynamicProxy - where through a lot of black magic - you can create a DynamicProxy and fool the CLR into thinking it's talking to an object, while it's actually talking to a DynamicProxy instead. And so then you can actually intercept invocations to existing methods and overrule them
Generally overruling existing functions at runtime is not that easy
That actually sounds pretty cool
Sometimes what I'd like to be able to do is treat part of an app as a core and the rest like user provided scripts, but written and evaluated in the host language and not running an embedded scripting language like lua with all the extra burden.
E.g. you have an image editor and you want the user to be able to write native functions to process the image. Or you have a game engine and you want to inject new game code from the user without the engine being a compiler or the game logic being bundled scripts.
You'd probably use a different approach for that. Like you'd make your program dynamically load all the .dlls in a "plugins" folder -
Then you'd provide some plugin interface for the users to create plugins, for example:
And then you can load plugin classes from all the dlls with dependency injection, and execute them though something like this:
This is a very simple example obviously, normally you'd send more meta-data to the plugins, or have multiple different interfaces depending on the kinda plugin it is, or have some methods to ask plugins when they're suitable to be used. But this way a user can provide compiled versions of their plugins (in the same language as the core application) - instead of having to provide something like lua scripts