Open KevJin97 opened 2 months ago
Thanks for creating this! This looks like a great start. I don't see a way to set const vars for the user (edit: I mean from the user interface), would you like me to implement this (and in the different UIs)? I'd also be happy to help provide some pointers if you're interested in doing it yourself. Feel free to message me with questions and I can answer-- I personally don't like to dig through someone else's code base, so I'd be happy to save you the trouble. But I should have time to work on it myself at some point over the next few weeks.
For the user to set them, I'm not actually sure what syntax would be most intuitive. Maybe adding the word const
before or after the variable? e.g. something like this:
1.380649e10-23 J K^-1 -> k_B const
And in the GUI, maybe there could be a toggle to make a variable defined as constant. (And for viewing currently defined variables, it would be good to show some indication which ones are constant).
And what would happen if a user defines both a variable k_B
and constant k_B
? Since there are separate hashmaps, this could happen. Maybe there should only be a single "variables" hashmap, but it could point to a structure that defines a variable and some metadata, such as whether or not it is constant? Of course, we could just check both when setting a variable, but I feel like that could be messy.
And FYI there is a global CONSTANTS
map already containing pi
, e
, and imaginary i
and j
, but I think those are fundamental enough that most people shouldn't need to override those[1]. I do like the idea of having a separate set of constants defined per CalcData, this could allow for separate calculator sessions in the future (e.g. one per class, a separate one for electrical engineering assignments and economics, which may use similarly named variables that mean completely different things).
And for raising an error when the user tries to overwrite a constant, you can just throw an exception like here, the error I throw if a user tries to overwrite one of the pre-defined CONSTANTS
[2].
Also, as a future feature that you or I could add, it might be nice to define many constants in the code which can be optionally enabled in the calculator. This way people can exclude constants that they don't use, which may use variable names that they use for other purposes.
[1] I may actually want to allow people to change the definition of i
, both (maybe) for electrical engineers using it for current, but also for compound interest calculations (one of the things I learned in school 10+ years ago that I still use in my day-to-day life). So it may be worth initializing the new CalcData 'const vars' field to the values of the current CONSTANTS
global, perhaps renaming it to INITIAL_CONSTANTS
. I could do this in a future commit, I am just sharing my initial thoughts, feel free to share other ideas you might have.
[2] I originally wrote this code when first learning C++, so I threw exceptions like Java, doing throw new MyException("message");
. But I think the proper C++ way is to do throw MyException("message")
and catch (const MyException &ex)
instead of catching the pointer and having to delete it. But for now you could just follow the same way that I did, and I may change it in a future commit.
I added a quick little idea for utilizing constant values like "k_B" or "h" by having them defined within in a separate global list within CalcData and having all functions that define values of CalcData::vars to first check if there's a constant named that first.
I also added the functions:
along with corresponding changes to other calls of CalcData::vars. This should let constants be set to whatever variable people want.
Suggestion: both "var_is_defined" and "const_var_is_defined" could honestly be replaced by the standard std::unordered_map::contains(std::string)