InlineMapping icon indicating copy to clipboard operation
InlineMapping copied to clipboard

#nullable enable

Open robrich opened this issue 4 years ago • 2 comments

If you put #nullable enable at the top of each file then you won't need to do this:

public Destination MapToDestination(this Source source)
{
  source == null ? throw new IHateYouException(nameof(source))
    : new Destination {
      // ...
    };
}

but instead can do this:

public Destination MapToDestination(this Source source)
{
  new Destination {
    // ...
  };
}

public Destination? MapToDestinationNullable(this Source? source)
{
  source == null
    ? null
    : new Destination {
      // ...
    };
}

robrich avatar Jul 02 '21 01:07 robrich

I already put #nullable enable at the top of my generated code. Even with that, it's possible that someone could pass in a null value, and my take is that you should still check for null and throw ArgumentNullException.

One thing I would consider is some kind of configuration setting that says, "If you're given a null value, return null instead of throwing ArgumentNullException". This could be done as a setting in the attribute (that could be done as work with this issue), and/or a configuration setting.

JasonBock avatar Jul 02 '21 13:07 JasonBock

Ah, I see I mis-read the generation code. Then throwing the YouAreADorkException here is completely the right move.

It still would be nice to allow cloning a nullable object though. Thinking further, would this work?:

Destination? destionation = source?.MapToDestination();

robrich avatar Jul 02 '21 17:07 robrich