Skip to content

[reference] Reduce reading ambiguity#168

Open
zcy1024 wants to merge 1 commit into
MystenLabs:mainfrom
zcy1024:reference-uses
Open

[reference] Reduce reading ambiguity#168
zcy1024 wants to merge 1 commit into
MystenLabs:mainfrom
zcy1024:reference-uses

Conversation

@zcy1024

@zcy1024 zcy1024 commented Jun 30, 2025

Copy link
Copy Markdown
Contributor

Adjust the code according to the context of the article to reduce ambiguity.

Comment thread reference/uses.md
let mut v = vector[];
vector::push_back(&mut v, 0);
vector::push_back(&mut v, 10);
vector::push_back(&mut v, some(0));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be option::some

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Contributor Author

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.

Comment thread reference/uses.md
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};

Copy link
Copy Markdown
Collaborator

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?

@zcy1024 zcy1024 Jul 9, 2025

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor Author

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
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants