https://devblogs.microsoft.com/oldnewthing/20250217-00/?p=110873 Skip to main content [RE1Mu3b] Microsoft Dev Blogs Dev Blogs Dev Blogs * Home * Developer + Microsoft for Developers + Visual Studio + Visual Studio Code + Develop from the cloud + All things Azure + Xcode + DevOps + Windows Developer + Developer support + ISE Developer + Engineering@Microsoft + Azure SDK + Command Line + Perf and Diagnostics + React Native * Technology + AutoGen + DirectX + OpenAPI + Semantic Kernel + SurfaceDuo + Windows AI Platform * Languages + C++ + C# + F# + TypeScript + PowerShell Community + PowerShell Team + Python + JavaScript + Java + Java Blog in Chinese + Go * .NET + All .NET posts + .NET Aspire + .NET MAUI + AI + ASP.NET Core + Blazor + Entity Framework + Servicing + .NET Blog in Chinese * Platform Development + #ifdef Windows + Azure Government + Azure VM Runtime Team + Bing Dev Center + Microsoft Edge Dev + Microsoft Azure + Microsoft 365 Developer + Microsoft Entra Identity Developer Blog + Old New Thing + Power Platform + Windows MIDI and Music dev * Data Development + Azure Cosmos DB + Azure Data Studio + Azure SQL + OData + Revolutions R + SQL Server Data Tools + Unified Data Model (IDEAs) * More [ ] Search Search * No results Cancel * Dev Blogs * The Old New Thing * API design note: Beware of adding an "Other" enum value February 17th, 2025 API design note: Beware of adding an "Other" enum value Raymond Chen Raymond Chen Show more Consider the following API: enum class WidgetFlavor { Vanilla, Chocolate, Strawberry, Other, }; WidgetFlavor GetWidgetFlavor(HWIDGET widget); The idea here is that Widgets come in three flavors today, but we might add new flavors in the future, so we add an Other value to cover any future flavors. This is a problem. Suppose we do indeed add a new flavor Mint in the next version. enum class WidgetFlavor { Vanilla, Chocolate, Strawberry, Other, Mint, }; What flavor should you report if somebody calls GetWidgetFlavor and the widget is mint? If you return WidgetFlavor::Mint, then this will confuse code written with the Version 1 API, because they expected to get Other for anything that isn't vanilla, chocolate, or strawberry. The word "other" means "not mentioned elsewhere", so the presence of an Other logically implies that the enumeration is exhaustive. On the other hand, you obviously should return WidgetFlavor::Mint because that's why you added the value to the enum in the first place! My recommendation is not to have an Other at all. Just document that the enumeration is open-ended, and programs should treat any unrecognized values as if they were "Other". Any code that uses this enumeration will therefore put all unrecognized values into an app-defined "Other" category on their own. Now you can add Mint: New code will put minty widgets in a "Mint" category, and old code will continue to put them in the app=defined "Other" category. 6 7 1 * [facebook] Share on Facebook * Share on Twitter * [linkedin] Share on Linkedin Category Old New Thing Topics Code Author Raymond Chen Raymond Chen Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information. 7 comments Join the discussion. Leave a commentCancel reply Sign in [ ] [Reply] [Cancel] Code of Conduct Sort by : Newest Newest Popular Oldest * [png] Vk TestA 1 week ago 0 Collapse this comment Copy link If you are doing C++ then you should specify the type of enum base, if you are doing this trick. If you are doing C then it's best to add MaximumValue =2147483647 to ensure that different compilers and platforms wouldn't change the layout of your class after you would add 256th flavor. Because otherwise, according to the C/C++ standard, adding Mint to Vanilla, Chocolate, and Strawberry is legal, but adding Watermelon to that is no longer legal! Log in to Vote or Reply * [png] Solomon Ucko 1 week ago 0 Collapse this comment Copy link Rust has `#[non_exhaustive]` for this. https://doc.rust-lang.org/ reference/attributes/type_system.html# the-non_exhaustive-attribute Log in to Vote or Reply * [png] Shawn Van Ness 2 weeks ago 0 Collapse this comment Copy link I like the idea of an [Open] attribute to apply to enums.. similar to [Flags]. Documentation isn't enough - there are so many de/serialization and ORM etc frameworks, that will throw exceptions when they see "flavor: 7". And this would help settle, once and for all, the ageless debate over whether or not it's a breaking-change to add a value to an enum. Log in to Vote or Reply * [png] Kalle Niemitalo 2 weeks ago 0 Collapse this comment Copy link In the Apache Avro schema language, an enum type can have a default value, to which any unrecognized values are mapped during deserialization. This feature improves compatibility if a message is written using a newer version of the schema but is then read by software that uses an older version. Unlike C# and C++, Avro does not support casting arbitrary integers to an enum type. Log in to Vote or Reply * [png] Jeremy Sachs February 17, 2025 0 Collapse this comment Copy link I'm sorry for this unrelated comment, but I'd like to suggest an article topic, and I don't see a suggestion box anyplace. Last year you shared with us the origins of the "Windows 3D Pipes" screensaver, which in part was a demonstration of OpenGL. I've been researching three interrelated screensavers from Plus! for XP that I believe were made to demonstrate Direct3D 8 shaders: "Mercury Pool", "Robot Circus" and "Sand Pendulum". Their team or project may have been called "MeSmer"; if there's any record of their work, or who it was that made the screensavers in the first place, I'd... Read more I'm sorry for this unrelated comment, but I'd like to suggest an article topic, and I don't see a suggestion box anyplace. Last year you shared with us the origins of the "Windows 3D Pipes" screensaver, which in part was a demonstration of OpenGL. I've been researching three interrelated screensavers from Plus! for XP that I believe were made to demonstrate Direct3D 8 shaders: "Mercury Pool", "Robot Circus" and "Sand Pendulum". Their team or project may have been called "MeSmer"; if there's any record of their work, or who it was that made the screensavers in the first place, I'd love to learn about them. Read less Log in to Vote or Reply * [png] Joe Beans February 17, 2025 3 Collapse this comment Copy link In C# I always make the first value (0) in an enum "Unknown" which should always be seen as an invalid value, and contextually different from "Other". Then default(T) will always naturally set to this value in case someone forgets to assign to it. I'm willing to bet WidgetFlavor will probably be set to Vanilla much more often than it ought to... Log in to Vote or Reply + [png] Raymond ChenMicrosoft employee Author 1 week ago 0 Collapse this comment Copy link Unknown is not the same as Invalid. Unknown means "I don't know what it is." Invalid means "I know what it is, but it's not an allowed value." For example, if somebody fills out a form and they leave the "country" field blank, then it's Unknown. If they write "Saturn", then it's Invalid. Log in to Vote or Reply Read next February 19, 2025 C++/WinRT implementation inheritance: Notes on winrt::implements, part 1 Raymond Chen Raymond Chen February 20, 2025 C++/WinRT implementation inheritance: Notes on winrt::implements, part 2 Raymond Chen Raymond Chen Stay informed Get notified when new posts are published. [ ] Subscribe By subscribing you agree to our Terms of Use and Privacy Follow this blog youtube Are you sure you wish to delete this comment? OK Cancel Sign in Theme Insert/edit link Close Enter the destination URL URL [ ] Link Text [ ] [ ] Open link in a new tab Or link to existing content Search [ ] No search term specified. Showing recent items. Search or use up and down arrow keys to select an item. Cancel [Add Link] Code Block x Paste your code snippet [ ] Ok Cancel Feedback What's new * Surface Pro * Surface Laptop * Surface Laptop Studio 2 * Surface Laptop Go 3 * Microsoft Copilot * AI in Windows * Explore Microsoft products * Windows 11 apps Microsoft Store * Account profile * Download Center * Microsoft Store support * Returns * Order tracking * Certified Refurbished * Microsoft Store Promise * Flexible Payments Education * Microsoft in education * Devices for education * Microsoft Teams for Education * Microsoft 365 Education * How to buy for your school * Educator training and development * Deals for students and parents * Azure for students Business * Microsoft Cloud * Microsoft Security * Dynamics 365 * Microsoft 365 * Microsoft Power Platform * Microsoft Teams * Microsoft 365 Copilot * Small Business Developer & IT * Azure * Microsoft Developer * Documentation * Microsoft Learn * Microsoft Tech Community * Azure Marketplace * AppSource * Visual Studio Company * Careers * About Microsoft * Company news * Privacy at Microsoft * Investors * Diversity and inclusion * Accessibility * Sustainability Your Privacy Choices Your Privacy Choices Consumer Health Privacy * Sitemap * Contact Microsoft * Privacy * Manage cookies * Terms of use * Trademarks * Safety & eco * Recycling * About our ads * (c) Microsoft 2025