✅ Writing a scuffed way to use an "assignment" (=) operator

Obviously, there's no such thing as an assignment operator. I'd like to find some way around that. I have a class that looks something like this:
struct MyType<T> {
public int AField;
public T Value {
get {
// Stuff to do with AField
}
set {
// Stuff to do with AField
}
}

public Type<T>(int aField) {
AField = aField;
}
}
struct MyType<T> {
public int AField;
public T Value {
get {
// Stuff to do with AField
}
set {
// Stuff to do with AField
}
}

public Type<T>(int aField) {
AField = aField;
}
}
I'd like to be able to assign values to this:
MyType<float> myType = new(20);

// What I want:
myType = 30.5f;
// What I have to do at the moment:
myType.Value = 30.5f;
MyType<float> myType = new(20);

// What I want:
myType = 30.5f;
// What I have to do at the moment:
myType.Value = 30.5f;
Is there any hacky, bodged, whatever way of doing this? I can't do it like this, obviously, because operators are static:
public static implicit operator MyType<T>(T value) {
MyType<T> ret = new(this.AField);
ret.Value = value;
return ret;
}
public static implicit operator MyType<T>(T value) {
MyType<T> ret = new(this.AField);
ret.Value = value;
return ret;
}
Any ideas are appreciated, thanks!
13 Replies
nukleer bomb
nukleer bomb13mo ago
There is no such thing as assigment operator in C# I mean, you can't modify it in any means
Jimmacle
Jimmacle13mo ago
what would this be for PepeHmmm
NoSkillPureAndy
NoSkillPureAndy13mo ago
I just think it'd be nice not to type .Value everywhere, I use this type a lot. Definitely not strictly necessary, just maybe a nice-to-have. It's just for brevity's sake.
Jimmacle
Jimmacle13mo ago
yeah that doesn't make sense to me semantically assigning a property of an object isn't the same as assigning a whole different object
NoSkillPureAndy
NoSkillPureAndy13mo ago
In my case it doesn't really matter.
Jimmacle
Jimmacle13mo ago
well, it matters to C# 😛
NoSkillPureAndy
NoSkillPureAndy13mo ago
Also, just realized that I just typed class instead of struct.
333fred
333fred13mo ago
Add an implicit conversion from double For this particular case, it doesn't look like you actually have any extra state beyond AField If I'm misunderstand and you actually do have that state, then there is no way
Vi Ness
Vi Ness13mo ago
The implicit operator would have to deal with setting the AField because you could write something like MyType<float> myType = 30.5f; But it looks like they need to pass the AField into the constructor and use that when getting or setting Value
Pacyfist
Pacyfist13mo ago
Super duper bad coding practice. You'll make something that everyone (including future you) will assume is a float. This simply begs for a bug somewhere in the future. Someone smart once wrote a rule for Python: "Explicit is better than implicit" this is also true in C# What pattern is the MyType supposed to be in? Is it some sort of Guard? Because I don't see any other reason for it to look like that. Seems like the responsibilities are mixed.
Pacyfist
Pacyfist13mo ago
The XY Problem
Asking about your attempted solution rather than your actual problem
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts