Skip to content

generate cast operatorΒ #68

@hjrb

Description

@hjrb

Hi,
first of all: an outstanding library!
Have you considered adding code generation for cast operators?
Example
[StronglyTypedId]
public partial struct UserID {
// addtionally generated
public static implicit operator int(UserID userID) => userID.Value;
public static explicit operator UserID(int i) => new UserID(i);

It would be userful when using (int) constants
// without StronglyTypedId
public static class Constants {
const int SpecialUserID=4711;
}
...
switch (item.UserID)
{
case Constants.SpecialUserID: // ....
}

After migrating user ID to StronglyTypedID one would need to change the code to
switch (item.UserID.Value)
{
case Constants.SpecialUserID: // ....
}

or make item.UserID a strongly typed ID. But that is often no so easy as this might come from DB DTO etc.
If the above cast operators would be generated, that code would still compile.
Also
//..
public static readonly UserID SpecialUser = (UserID)4711 looks much nicer as
public static readonly UserID SpecialUser = new UserID(4711)

I wonder why MS hasn't added something similar as StronglyTypedId to the compiler. That would really be a benefit. But so far we have your great library :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions