Why does Phobos use enum to define constants? For example, in std.math:
enum real E = 2.7182818284590452354L;
Why not use a global immutable? What are the advantages/disadvantages of enum over immutable?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In general, for things that are compile-time constants as opposed to runtime constants, there’s no disadvantage to using an enum, and it has the advantages of making your intentions absolutely clear and being marginally more efficient.
Edit: One other use case for enums can be disambiguating to the compiler whether a function should be evaluated at runtime or compile time. If the result of a function is assigned to an
immutablestack variable, the function will be evaluated at runtime. If you use anenumin the same scope, the result will be evaluated at compile time.