[Proposal] Member access operator #3446
Replies: 5 comments 1 reply
-
This would be an abuse of operator overloading (imo) and only really serves to make the code less clear. |
Beta Was this translation helpful? Give feedback.
-
It can be used in WeakReference scenario or another custom reference-like type, for example in the implementation of some sort of smart pointer like in C++. |
Beta Was this translation helpful? Give feedback.
-
Those are the last place i would ever want to overload these operators. I absolutely do not want to treat the weak-ref the same as teh wrapped type. |
Beta Was this translation helpful? Give feedback.
-
I'm recycling this because it is something that offers key functionality that makes C++ much easier to deal with. Member access operator overloading is something that does not exist solely for the sake of smart pointers. However, instead of overloading the member access operator, I would like to explore something that might offer the functional equivalent. Which involves nothing more than extending the use of the 'this' keyword currently used to declare/define indexers, which is merely one kind of default property. This concept was in-part inspired by Pascal's implements keyword, which allows the value of published property to provide the implementation of an interface declared on the containing type. The conceptual extension of the 'this' keyword as outlined below, offers that same functionality in addition to allowing a 'wrapper' type to impersonate the type it wraps. In the example below, note the use of 'this' in the first type, which enables a functional equivalent (to some extent) of an overloadable member access operator, that would support Proxy/Adapter-type patterns:
Using an extended 'this' keyword to delegate the implementation of an interface:
This example uses an extended 'this' to delegate multiple interfaces:
|
Beta Was this translation helpful? Give feedback.
-
My motivation for this suggestion came from a project that I am working on with some friends of mine, where we are managing an emulator in dotnet, and allowing developers to interface with the emulator through plugins that are also written in dotnet. We encountered a problem where there is no sane syntax for interfacing with the emulated system's memory using structs when the emulated system has a different endianess, the closest we could get to this is forcing developers to use properties for every structure field, which in our circumstance is unacceptable syntax. As an example, I am specifically trying to avoid asking develoeprs to use the following syntax: [StructLayout(LayoutKind.Explicit), EmulatedStruct]
struct Camera {
[FieldOffset(0x50)] public Vector3 Position { get; set; }
};
...
Camera camera = new Camera(0x1248);
var pos = camera.Position; // we must make an additional variable since we cannot assign to a property
pos.X *= 10.0f;
camera.Position = pos; And I additionally want to avoid the Camera camera = new Camera(0x1248);
camera.Position.Value.X *= 10.0f; Both of these solutions are significantly more unclear and would end up having either more implementation hell, or overhead than a smart pointer alternative. That all said, I absolutely do not want to see smart pointers, or the overloading of the My purpose for wanting this feature is so that I can give my plugin developers memory safe, endian-correct ways to interface with memory, and clean syntax. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, there is not able to set custom member access routing that would be useful for wrapper type like we already have in Nullable operator ?., I proposed to add and allow to overload such operator for given type T that wrap type TWrap:
For example, we have in Framework type Lazyproperty Value that provide value of type T, if we want to access some member or call function M of T currenly we should write, for example:
With proposed operator it can be written like:
We already have such semantic for a pointer type, so it can be expanded for a custom type, too.
Sorry if I duplicated a similar proposal somehow.
Beta Was this translation helpful? Give feedback.
All reactions