Skip to content
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

Add typecast page to Docs #3059

Open
alphak3y opened this issue Oct 18, 2022 · 3 comments
Open

Add typecast page to Docs #3059

alphak3y opened this issue Oct 18, 2022 · 3 comments
Labels
The Sway Book Everything to do with the Sway Book

Comments

@alphak3y
Copy link
Contributor

Currently there is no documented way to typecast from a u32 to a u64.

@mohammadfawaz
Copy link
Contributor

So Sway has implicit casting for integers. So you could do

    let y:u32 = 5;
    let x:u64 = y;

Is this what you're referring to? The compiler will also warn you if there is a potential loss of precision (i.e. downcasting).

@mohammadfawaz
Copy link
Contributor

@anton-trunov assigning this to you. Once we get rid of implicit casting and add into() for integers (like Rust), we should document that behaviour in the Sway book.

@mohammadfawaz mohammadfawaz added the The Sway Book Everything to do with the Sway Book label Jan 29, 2023
@anton-trunov
Copy link
Contributor

We cannot have into() for Sway as of now, because the From trait has both directions:

pub trait From<T> {
    fn from(b: T) -> Self;
    fn into(self) -> T;
}

meaning that the types T and U in From<T> for U are isomorphic (see this issue: #3637).

So, currently the way to go would be let x : u64 = 5u32.as_u64().

@anton-trunov anton-trunov removed their assignment Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
The Sway Book Everything to do with the Sway Book
Projects
None yet
Development

No branches or pull requests

3 participants