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

Suggestion: Add a compiler indication for "get the same type that that variable has" #3241

Closed
blyxyas opened this issue Mar 5, 2022 · 6 comments

Comments

@blyxyas
Copy link
Member

blyxyas commented Mar 5, 2022

What I mean is (for example) add a t# before a variable to get that type, instead of using a function that slows performance, the compiler gets the same type of that variable.

Example

Example of how this may work.

fn translate(input: String) -> Vec< t# ram > {
    
    let mut ram: Vec<u32> = Vec::new();
    let mut result: Vec< t# ram > = Vec::new(); /*
This improves readability, we can see at first glance that this Vec is made up of various "ram" types */
    
    let mut sum: u32 = 0;

    for phrase in vec {

        for word in phrase.split_whitespace() {

            for c in word.chars() {
                sum += multiplier * c as u32;
            }

            ram.push(sum);
            sum = 0;

        }

        result.push(ram.clone());
        ram.clear();

    }

    return result;
}

This is just a quick example I made up from a real code I'm using, but it would be really useful if the first thing you could see is the same type that another without using the type keyword for only two variables.

This addition could improve readability without losing performance, because it just adds a little bit of time at compile time.
Thanks for reading, that is my suggestion.

@SOF3
Copy link

SOF3 commented Mar 5, 2022

Do you mean something like typeof in TypeScript?

@blyxyas
Copy link
Member Author

blyxyas commented Mar 5, 2022

Yes, but without being a run-time function that slows down performance

@Lokathor
Copy link
Contributor

Lokathor commented Mar 5, 2022

Type info in rust is (mostly) all removed at compile time, so this would be a compile time only operation.

@shepmaster
Copy link
Member

Perhaps the same as #2704 or #3175. See also #2706

@SOF3
Copy link

SOF3 commented Mar 6, 2022

Yes, but without being a run-time function that slows down performance

I get your point but just to clarify, typeof in a type context in TypeScript is compile-time only; only typeof used as a value is a runtime string.

But is there anything you need that cannot be achieved with existential types?

let mut ram: Vec<u32> = Vec::new();

type T;
let _: &T = &ram;

let mut result: Vec<T> = Vec::new();

@Havvy
Copy link
Contributor

Havvy commented Mar 6, 2022

Your example of using this typeof in return type is ambiguous in the presence of multiple bindings of the same name within a function:

fn unfun() -> t# declared_multiple_times {
  let declared_multiple_times = 1;
  let declared_multiple_times = "1";

  generic_return_fn() // could return an i32, &'static str, and possibly others
}

It also goes against the spirit of type signatures of items not being inferred.

@blyxyas blyxyas closed this as completed Mar 9, 2022
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

No branches or pull requests

5 participants