Extension functions are not the same at all. Extension functions are syntactic sugar. For example if you have an extension function like
public static class ObjectExtension
{
public static void DoSomething(this object input) { }
}
You can call that function on an object by doing object.DoSomething() - Yes. But underneath it’s the same as doing ObjectExtension.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
I thought you were talking about keeping an unmaintained library intact but building onto it.
I thought C was a really dangerous way to use that syntactic sugar pattern. Actual manipulation of the bytecode to maintain and extend a compiled binary is wild
This is sometimes practical, too. For example, hooking and extending functions in compiled code that will never be updated by the original author, while preserving the original executable/library files.
Your original comment made it seem more like extensions - extend and preserve. That’s the misunderstanding.
When I said it’s wild to manipulate bytecode I means “wow that’s a terrifying practice, I would hate to check that PR”
Fair enough. What threw me is that you said “bytecode”, which is generally not used when referring to hardware machine instructions. My original comment is about patching the in-memory image of a running program or library, replacing machine instructions in order to intercept certain calls and extend their behavior.
I thought my phrase “compiled code” would convey this, but I guess nowadays bytecode-compiled languages are so common that some people assume that instead.
Yeah and part of this is that the domain I’ve been working in for years now is very far from machine code, and I’m probably overly lax with my language here.
The result of being in very corporate app dev - I’m usually talking in much higher level abstractions. My bad on conflating bytecode and machine code
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.
And then you can load plugin classes from all the dlls with dependency injection, and execute them though something like this:
publicclassImageEditor(IEnumerable plugins)
{
publicvoidEditImage(int[,] imageData)
{
foreach (var imageEditorPlugin in plugins)
{
imageEditorPlugin.BeforeImageEdit(imageData);
// Do internal image edit function
imageEditorPlugin.AfterImageEdit(imageData);
}
}
}
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
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
Ah my bad, misunderstood the use case.
I thought you were talking about keeping an unmaintained library intact but building onto it.
I thought C was a really dangerous way to use that syntactic sugar pattern. Actual manipulation of the bytecode to maintain and extend a compiled binary is wild
Just wait until you learn about machine code. :)
I do have a degree in this. I am aware.
Your original comment made it seem more like extensions - extend and preserve. That’s the misunderstanding.
When I said it’s wild to manipulate bytecode I means “wow that’s a terrifying practice, I would hate to check that PR”
Fair enough. What threw me is that you said “bytecode”, which is generally not used when referring to hardware machine instructions. My original comment is about patching the in-memory image of a running program or library, replacing machine instructions in order to intercept certain calls and extend their behavior.
I thought my phrase “compiled code” would convey this, but I guess nowadays bytecode-compiled languages are so common that some people assume that instead.
Yeah and part of this is that the domain I’ve been working in for years now is very far from machine code, and I’m probably overly lax with my language here.
The result of being in very corporate app dev - I’m usually talking in much higher level abstractions. My bad on conflating bytecode and machine code
Ah, corporate work. I hope they’re treating you well.
Different strokes - some would find what I’m doing hell. I personally love it.
The 260k/yr salary may help alleviate the pain.
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:
public interface IImageEditorPlugin { public void BeforeImageEdit(int[,] imageData); public void AfterImageEdit(int[,] imageData); }
And then you can load plugin classes from all the dlls with dependency injection, and execute them though something like this:
public class ImageEditor(IEnumerable plugins) { public void EditImage(int[,] imageData) { foreach (var imageEditorPlugin in plugins) { imageEditorPlugin.BeforeImageEdit(imageData); // Do internal image edit function imageEditorPlugin.AfterImageEdit(imageData); } } }
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