3 ms·
Indeed, that is basically what you get from unions, except the exhaustiveness check. Unless I'm mistaken, if one were to later implement a new class that exten
by kroltan 6y ago
Indeed, that is basically what you get from unions, except the exhaustiveness check.
Unless I'm mistaken, if one were to later implement a new class that extends AppState, all existing code would compile, but possibly fail or misbehave at runtime, unless you meticulously checked every place that tries to determine something based on those derived types.
In TypeScript, adding a new case for an union and not handling it everywhere is a compilation error on every incomplete usage site.
For example, try deleting one of the arms of the switch in this playground: https://www.typescriptlang.org/play?ts=4.2.2#code/C4TwDgpgBAMhBuEA2UC8UDeBfKB6XUAIgPYQDOAdgOTBQC2AhsMBAE5QDuAFk1AJa0+ZKADNi7YFyFQIADwZ0wSCADoAUGtCQoAQTBgAysCbRUaqBagAfTFDLGWALigAiJMQYATPhQDmLgBooMFZiX1ZyMmcKAFc6ACM2KCxzSxsMOwcIZxcyZAgAY2AfXwB9ZUQkQOTUi3TMkxylBhAS6orkZzhKmstrW3tG1zIYgoLIl160gayckQY+JBiI6o4GCmAyACUIYFYQZ3jiYmV1qbqZoZc2UNZqiIYyYgpne1YSmrURGIoivmfRMcABSDJy6fRGEwASleew+GFqdg4AgKXCgIKyKlBEChmERfQKj2gbg83j8LmcEWAywoUAABgBxBTQaTuLwlZwAEgwAFkmFwVKEfp4MSYVCEwhEyMIAFRQACMAAZFVCsABSOkAbnxlkJeWG+T+fnKCGQFKgVJprgACqd9XllEUoAwoB0qtq+gSia5mq1yZTdlaXAAJBiIUQ-ACELg9noseuJIzGEwD1NYtJcAE1iDEqBFOD4KGxo5q8AQQXwClCdfHvS55otlhBzZb064GQIoL4Yp4YzWoAnXDdxC3A226QBRVi3LkYbGCiCPZ5YLWIlIpL7AhF9bE5JPjaUuNRYKGaoA https://www.typescriptlang.org/play?ts=4.2.2#code/C4TwDgpgBA...
I have to say, the default diagnostic isn't brilliant, but some tooling will give a better error and actually point out the missing arms, instead of complaining about the return type.
- okamiueru 6y agoI suppose. In practice, I haven't experienced this to be a problem. Since you already check which implementation you are dealing with, any code that relied on any state, should still work without any issue. This is the same as with typescript unions. Any code that somehow needs to handle a new state hm... I suppose getting a compile time error is nice to immediately see all places where it is used... but, I mean, so would a "find all uses" search. It's also not all that different from the linting warning error you'd get from iterating over runtime types without handling all cases. All in all, sufficiently analogous to not consider unions a missing feature of the dart language? Seems nice to have, but, maybe not very necessary. Especially if the only difference is whether it is considered an error by the syntax, or a warning by the linter.