-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: Go 2: change len, cap to return untyped ints if result is constant #31795
Comments
And unsafe.Sizeof etc. |
Yes, this meets every use case that has frustrated me in code. |
Change https://golang.org/cl/179184 mentions this issue: |
It turns out that per the spec, But we should be able to do it for |
As an aside, changing |
Rather than make Non-constant comparisons are still untyped bool when their operands are typed. Edit: To answer my own question, because if we allowed
We could make them evaluate to "untyped uintptr" lol. |
I concur (consistently untyped for Go 2) but I may be overlooking potential
problems.
…On Sat, Sep 7, 2019 at 10:32 AM Matthew Dempsky ***@***.***> wrote:
Rather than make cap and len only evaluate to untyped when their
arguments are constant, why not just always make them untyped?
Non-constant comparisons are still untyped bool when their operands are
typed.
It turns out that per the spec, unsafe.Sizeof (and its siblings
unsafe.Offsetof, unsafe.Alignof) return a uintptr rather than an int, which
makes it impossible to change these in a backward-compatible way: even
though they always return a constant, if that were an untyped int it would
not default to uintptr.
We could make them evaluate to "untyped uintptr" lol.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#31795?email_source=notifications&email_token=AB4DFJNFWBKI2RU7JSECI5LQIPQRHA5CNFSM4HJ3MHK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6E5XGI#issuecomment-529128345>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB4DFJITHLZSWUKFKOEROOLQIPQRHANCNFSM4HJ3MHKQ>
.
--
*Michael T. Jonesmichael.jones@gmail.com <michael.jones@gmail.com>*
|
@mdempsky As you noted, the problem with making |
Will this change allow |
yes |
I am not sure the costs here are worth the benefit. Today there is a simple rule: len(x) has type int. Changing the type to depend on what x is will interact in non-orthogonal ways with various code changes. For example, under the proposed semantics, this code compiles:
but suppose then someone comes along and wants to be able to modify x for testing or something like that, so they s/const/var/. That's usually fairly safe, but now the f(len(x)) call fails to type-check, and it will be mysterious why it ever worked. This change seems like it might add more rough edges than it removes. |
Agreeing with @rsc here. While this seems like an easy win at first glance, it's not clear that the corner cases are worth the extra complexity. I am retracting this proposal. |
Reminder issue.
Can we change
len
,cap
, etc. to return an untyped value rather than anint
if they are constant expressions? Investigate if this is possible in a backward-compatible way.The text was updated successfully, but these errors were encountered: