Add simple way to create struct wrappers for types #1346
Replies: 8 comments
-
Related: #234 |
Beta Was this translation helpful? Give feedback.
-
Also see #410 |
Beta Was this translation helpful? Give feedback.
-
Could be managed by the mythical source generators! [StructWrapper(typeof(int))]
public partial struct Celcius { } |
Beta Was this translation helpful? Give feedback.
-
Wow, it's been a while since someone mentioned them 😀 |
Beta Was this translation helpful? Give feedback.
-
Well, the reason we won't be getting them has been pretty relentlessly explained, so I've been wondering if the right thing to do is go back to all the issues we said were covered by source generators and work towards alternative solutions. |
Beta Was this translation helpful? Give feedback.
-
I think a general and short (short in the sense of a keyword) will not work for all cases. There are many decissions one has/can take especially for the possible operator overloads. Which cast direction is implicit, can I use addition, can I use multiplication, etc. But you can reduce much of the code with the proposed and championated record syntax (#39): public struct ChallengeId(int Value)
{
public static readonly ChallengeId Empty = default;
public static explicit operator ChallengeId(int value) => new ChallengeId(value);
public static implicit operator int(ChallengeId cid) => cid.Value;
public override string ToString() => Value.ToString();
} With default interface methods (#52) this can maybe even be more reduced, although I'm not sure if the public interface IStrongTypedefMixins<TSelf, T>
{
T Value { get; }
public static readonly TSelf Empty = default;
public string ToString() => Value.ToString();
}
public struct ChallengeId(int Value) : IStrongTypedefMixins<ChallengeId, int>
{
public static explicit operator ChallengeId(int value) => new ChallengeId(value);
public static implicit operator int(ChallengeId cid) => cid.Value;
} Edit: |
Beta Was this translation helpful? Give feedback.
-
I like the records/default interface methods suggestion; it's short and sweet and customisable; but I wonder if interactions would get confusing between mixins defined in different libraries, with different expectations on explicit/implicit casting behaviour. |
Beta Was this translation helpful? Give feedback.
-
Source generators have finally landed for C# 9.0, and should be able to deal with generating all this code easily. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Add simple way to wrap types like int, long, Guid etc.. to a struct without need to do all overrides and operators yourself.
Let's say you have double that is Celsius and you want a method to convert it to Fahrenheit, something like this
double ConvertToFahrenheit(double celsius)
. It's much more typesafe with wrapper structsFahrenheit ConvertToFahrenheit(Celsius celsius)
.We use wrapper structs in our company's code base for typesafety and we have lot of code like this
Beta Was this translation helpful? Give feedback.
All reactions