Why can't structs be declared as const?

Lazlo picture Lazlo · Jan 4, 2011 · Viewed 36k times · Source

Answer

cdhowie picture cdhowie · Jan 4, 2011

Because the value type constructor might do anything -- for example, switch logic based on the time of day. Constant value types makes sense intellectually, but it simply cannot work on custom value types in practice due to the flexibility of constructors to do whatever they please. (Remember that constants are evaluated at compile time, which means your constructor would have to be run at compile time.)