-
Notifications
You must be signed in to change notification settings - Fork 139
[reference] Reduce reading ambiguity #168
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
| let mut v = vector[]; | ||
| vector::push_back(&mut v, 0); | ||
| vector::push_back(&mut v, 10); | ||
| vector::push_back(&mut v, some(0)); |
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.
should be option::some
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.
Or add a use statement above.
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.
The relevant use statement is below, which is what this example needs to demonstrate.
| fun new_vec(): vector<Option<u8>> { | ||
| fun new_vec(): vector<std::option::Option<u8>> { | ||
| use std::vector::push_back; | ||
| use std::option::{Option, some, none}; |
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.
Why did you remove Option type import?
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.
As far as I understand, use inside a function body is not effective during the function declaration. The previous writing method does not report an error because std::option::Option is imported by default. I made this change to make that clear.
Of course, I just made the changes based on my understanding. If my understanding itself is wrong, please correct me.
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.
To illustrate further, I wrote a small example:
module examples::a {
public struct MyOption<T> has copy, drop, store {
vec: vector<T>
}
public fun some<T>(e: T): MyOption<T> {
MyOption {
vec: vector[e]
}
}
public fun none<T>(): MyOption<T> {
MyOption {
vec: vector[]
}
}
}
module examples::b {
// If you replace the two commented lines below, you will get an error.
// fun new_vec(): vector<MyOption<u8>> {
// use examples::a::{MyOption, some, none};
fun new_vec(): vector<examples::a::MyOption<u8>> {
use examples::a::{some, none};
let mut v = vector[];
v.push_back(some(0));
v.push_back(none());
v
}
}
Adjust the code according to the context of the article to reduce ambiguity.