[reference] Reduce reading ambiguity#168
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.
Or add a use statement above.
There was a problem hiding this comment.
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.
Why did you remove Option type import?
There was a problem hiding this comment.
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.
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.