Mediator icon indicating copy to clipboard operation
Mediator copied to clipboard

Incorrect switch case order in polymorphic notifications

Open grendizeras opened this issue 4 months ago • 2 comments

I have base notification class public record DomainEvent(DateTimeOffset Timestamp) : INotification;

and several child classes: public record RoundCreated(long Id, DateTimeOffset Timestamp) : DomainEvent(Timestamp); public record RoundResulted(long Id, WinState? Win, DateTimeOffset Timestamp) : DomainEvent(Timestamp) public record RoundSucceeded(long Id, DateTimeOffset Timestamp) : DomainEvent(Timestamp) I only have handlers for child event notifications. In this case source generator generate switch cases in incorrect order:

 case global::Shared.Aggregate.DomainEvent n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundSucceeded n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundCanceled n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundCreated n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundResulted n: return Publish(n, cancellationToken);

resulting in Error CS8120 "The switch case is unreachable. It has already been handled by a previous case or it is impossible to match. "

Expected behavior would be to place base class case last.

grendizeras avatar Feb 26 '24 18:02 grendizeras