-
Notifications
You must be signed in to change notification settings - Fork 8
Feat: implement fromNKI? using c++ #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stopped an 1/8 of the way in to this review. But here's what I have
struct Builtin, struct Ref, struct Fun, | ||
struct Var, | ||
std::monostate, // used with None, Elipsis, Mgrid | ||
bool, int, float, struct Slice, klr::Address, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utterly trivial: don't need struct
in these lines
In .c files, you need to put struct
everywhere you use custom types. But in C++ it's unnecessary.
|
||
struct Error { | ||
std::string msg; | ||
Error(std::string&& m) : msg(std::move(m)) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you just take the string by value, it's way easier for users (and just as efficient). They can call it like: Error("oh no")
instead of Error(std::move(std::string("oh no")))
. I see you discovered that and made a TraceHelper(std::string msg)
helper function later to get the same result as what I'm suggesting here. You wouldn't need that helper anymore
The only advantage of the "r-value only" version you made here is that users are forced to call move() themselves and can't accidentally do a copy instead. But you can see the downside, as you had to make a helper function to work around the clunkiness.
Error(std::string&& m) : msg(std::move(m)) {} | |
Error(std::string m) : msg(std::move(m)) {} |
|
||
Error TraceError(std::string msg) { | ||
return Error{std::move(msg)}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need this if you make change to Error constructor I suggested above
Error TraceError(std::string msg) { | |
return Error{std::move(msg)}; | |
} |
|
||
// Simple error handling | ||
template<typename T> | ||
using TraceResult = std::variant<T, Error>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need it in this PR, but I'd love a proper Result class.
Or use c++ exceptions.
T get_ok(const TraceResult<T>& result) { | ||
return std::get<T>(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is returning a copy of T. Because it takes result by reference, but return a T by value.
We might want it to be more like std::get for variant and std::expected::value(). Returning either a reference, or rvalue, to what's inside
template<typename T>
T& get_ok(const TraceResult<T>& result) {
return std::get<T>(result);
}
template<typename T>
T&& get_ok(TraceResult<T>&& result) {
return std::get<T>(std::move(result));
}
Implement
FromNKI?
functionality defined in lean using c++Notes: