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

Can and should unconvert simplify this case? #7

Closed
dmitshur opened this issue Jun 3, 2016 · 5 comments
Closed

Can and should unconvert simplify this case? #7

dmitshur opened this issue Jun 3, 2016 · 5 comments

Comments

@dmitshur
Copy link
Collaborator

dmitshur commented Jun 3, 2016

Suppose there's a declaration of a []int32 slice done in the following way:

x := []int32{
    (int32)(1),
    (int32)(5),
    (int32)(8),
}

Is there a reason unconvert can't or shouldn't simplify it to:

x := []int32{
    1,
    5,
    8,
}

Since constants are used, the type conversion is not needed, as the constants will have their types inferred correctly from the context. Any thoughts?

@mdempsky
Copy link
Owner

mdempsky commented Jun 4, 2016

Nice, I agree the type conversions there are unnecessary because of context, just like in

var x int32 = int32(1)

It should also work the same for struct and map literals.

@dmitshur
Copy link
Collaborator Author

dmitshur commented Jun 4, 2016

Great!

As I understand, this will only be possible in some cases. For example, if it looks like this:

var x, y, z = 1, 2, 3

s := []int32{
    int32(x),
    int32(y),
    int32(z),
    4,
    5,
}

Then it won't be possible, since variables x, y and z will need to be converted. But if they were declared as int32, then it could be dropped.

It should also work the same for struct and map literals.

Yep. Also arrays. I didn't mention others because I wanted to start the conversation simpler.

@mdempsky
Copy link
Owner

mdempsky commented Jun 4, 2016

Correct, in the int32(x) case, x's type is int, so it's a necessary conversion. It's only unnecessary if x is untyped or int32 already.

@dmitshur
Copy link
Collaborator Author

dmitshur commented Sep 1, 2017

I suspect this issue is also related to #20 (comment). Specifically, it could be that the reason this case isn't detected and reported because the constants would become untyped as a result.

@mdempsky
Copy link
Owner

Going to close as a duplicate of #20 since the latter has more details.

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

2 participants