-
-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable explicit cast for widening conversions #1638
Comments
Thanks, but applying |
Indeed, the above code with public class DtoA
{
public long Count { get; set; }
}
public class DtoB
{
public int Count { get; set; }
}
[Mapper(EnabledConversions = MappingConversionType.All & ~MappingConversionType.ExplicitCast)]
public static partial class AnimalCloningExtensions
{
public static partial DtoB Map(DtoA source); //<- RMG007 Could not map member ConsoleApp1.DtoA.Count of type long to ConsoleApp1.DtoB.Count of type int
} Are you getting a different result? If so, I think you should provide information about your environment and code sample (see the bug issue template) and wait for the developers to respond |
As mentioned by @TonEnfer, your use case should be covered by the EnabledConversions configuration. Note that Mapperly also adds the cast syntax for implicit conversions. This approach simplifies code generation, as the type may not always be implicitly clear (e.g., in lambdas). However, this should not cause any issues, since it is not possible to define both an explicit and an implicit operator for the same conversion. |
Is your feature request related to a problem? Please describe.
Currently there's always an explicit cast when casting one numeric type to another. However, it's not needed for widening conversions! Consider the following example:
It produces this mapper:
The cast is redundant. Some might also view the explicit cast as a code smell, since if you change the
DtoA
Count to be long:the mapping code stays the same - but now you have potential data loss as it's a narrowing conversion!
Describe the solution you'd like
Either:
OR
[Mapper(NoExplicitWideningCast = true)]
The text was updated successfully, but these errors were encountered: