golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.97k stars 17.53k forks source link

spec: allow 'any' for 'interface{}' in non-constraint contexts #33232

Closed Lexkane closed 2 years ago

Lexkane commented 5 years ago

Note, 2021-09-02: Now that any is an alias for interface{}, we have a special case that it can only be used in type parameter constraints. This proposal is now to remove that special case, allowing any, well, anywhere.

This CL shows what it would look like if used throughout the Go tree.

- rsc


I would lilke to be able to declare any type with keyword any instead of empty interface, like rune means alias int32. Such feature make language more readable , without polluting declarations with many empty interfaces. Imho any keyword is perfect for such alias.

alanfo commented 5 years ago

I often use a type alias for this which isn't much extra work:

type any = interface{}
ccbrown commented 5 years ago

I agree with the sentiment that interface{} is terrible for readability, but I'm really hoping Go 2 has good enough generics to make nearly all uses of interface{} an avoidable anti-pattern. If that happens, an any type probably wouldn't be warranted at all. But if for some reason generics just completely fall through, sure, I'd support a builtin any alias. I do agree that it would increase readability.

dotaheor commented 5 years ago

I support this proposal, though not strongly.

In the early days of using Go, I often encountered the map[string]interface{} type, which is really confusing for a new gopher. I used it for about a year without knowing what it exactly means.

whoiswentz commented 5 years ago

I support this proposal too.

Its simple to implements and the readability becomes better

Freeaqingme commented 5 years ago

I don't support this proposal. The power of Go is that for many things there's a single way of doing things. A language gets bloated easily once we start adding additional syntaxes only because some people prefer one over the other.

People new to the language may initially learn the 'any' keyword, but then read existing code, and still have to look up the interface{} keyword. Effectively, the burden of learning the language increases, rather than decreases.

MaerF0x0 commented 5 years ago

Yes and on w/ what @Freeaqingme has said, we also must take care when creating different ways of doing the same thing because:

  1. It makes tooling more difficult: now go tools have to support a new path to the same thing, and likely the users of said tools will expect output to match their chosen flavor of alias.

  2. It reduces the token space for future golang iterations. Consider alternate uses for the any token in future iterations of the language. We lose the option of clear expressiveness if we choose to alias any to something that is already provided. Some random ideas of how any could be something else: it could refer to any length of array ([any]string) , it could refer to non ZeroValue elements in a slice ( any mySlice) like lodash's compact function .

The examples are unimportant, but the point should be clear. If we choose to make this our any then we're stuck with it for quite some time, likely for good.

dotaheor commented 5 years ago

Besides readability reason, less verbose and weird (esp. for new gophers) is another good point of this proposal.

@Freeaqingme I began using Go by using Go templates to serve some web pages, I didn't need understand what interface{} is. I just needed to know I can assign any vale to it. any is far better for this purpose.

When a gopher later suddenly realizes that any is just interface{}, it is a viva moment, which in fact helps gophers understand Go better.

@MaerF0x0

It makes tooling more difficult:

any is just an alias of interface{}, there are many aliases in Go. If a tool can't handle such cases, it is a not qualified tool.

and likely the users of said tools will expect output to match their chosen flavor of alias.

This is not what a qualified tool should do.

It reduces the token space for future golang iterations.

Builtin identifiers are not keywords, users can shadow them as needed.

Freeaqingme commented 5 years ago

@dotaheor

I just needed to know I can assign any vale to it. any is far better for this purpose.

If you really believe interface{} is so bad, I'd suggest you extend this proposal to deprecate interface{} and eventually remove it. I'm not a fan of that either, but my primary argument was against having two keywords for essentially the same thing. That argument would be moot if interface{} keyword is removed.

@MaerF0x0

We lose the option of clear expressiveness if we choose to alias any to something that is already provided.

We still have 'whatever' and 'anythingReally' as alternatives :yum:

changkun commented 5 years ago

Is there anyone cloud explain the historical reason of why interface{} is designed as interface{} rather than anything else?

dotaheor commented 5 years ago

@Freeaqingme any will not be a keyword, it will be just a builtin identifier, just like, rune and byte, etc.

interface{} means an interface type specifying no methods, which happens to make it act as the any type. interface{} is not a bad thing generally, but using it as the any type is bad for new gophers. It confuses many new gophers.

fzipp commented 5 years ago

@Freeaqingme

If you really believe interface{} is so bad, I'd suggest you extend this proposal to deprecate interface{} and eventually remove it. I'm not a fan of that either, but my primary argument was against having two keywords for essentially the same thing. That argument would be moot if interface{} keyword is removed.

@changkun

Is there anyone cloud explain the historical reason of why interface{} is designed as interface{} rather than anything else?

It's not a special design, but a logical consequence of Go's type declaration syntax.

You can use anonymous interfaces with more than zero methods:

func f(a interface{Foo(); Bar()}) {
    a.Foo()
    a.Bar()
}

Analogous to how you can use anonymous structs anywhere a type is expected:

func f(a struct{Foo int; Bar string}) {
    fmt.Println(a.Foo)
    fmt.Println(a.Bar)
}

An empty interface just happens to match all types because all types have at least zero methods. Removing interface{} would mean removing all interface functionality from the language if you want to stay consistent / don't want to introduce a special case.

Freeaqingme commented 5 years ago

@fzipp good point. I realized that after posting my earlier comment as well. Then it makes zero sense to remove that.

I'm sticking with my original reply; we can simply explain why interface{} is interface{} and how it works. There's then no need to add an additional keyword that basically is/does the same thing.

sirkon commented 5 years ago

@Freeaqingme

If you really believe interface{} is so bad, I'd suggest you extend this proposal to deprecate interface{} and eventually remove it. I'm not a fan of that either, but my primary argument was against having two keywords for essentially the same thing. That argument would be moot if interface{} keyword is removed.

There was a built in type alias working like

type byte = uint8

in every Go package from the very early days.

It is not me being this proposal supporter or the other way, just a well known fact.

y-usuzumi commented 5 years ago

In Haskell when we write guards:

abs n
  | n < 0     = -n
  | otherwise =  n

where

otherwise = True

Just imagine how much less readable it is if one uses True in place of otherwise.

It's only good if there's a single GOOD way of doing things.

ianlancetaylor commented 5 years ago

If we are able to add generics to the language, then I suspect there will be many fewer cases where people write interface{}, so the alias would have correspondingly less value. Putting on hold pending a decision on generics.

conilas commented 5 years ago

The any value acts like the top type in a type system and it may be usefull sometimes even with generics.

Say we have a higher order function that composes just for logs. You do not need a contract of anything like that for that type if you want to log the argument and then perform the action. Something like (take it as a pseudocode):

func add(a interface{}, b interface{}, action fn_type<args>){
  return fn(a, b) -> {
    fmt.Printf("%v %v", a, b)
    action(a, b)    
  }
}

The top type would be good in this case instead of writing interface{} either way, so I think this proposal still have some value.

yiyus commented 4 years ago

Relevant: https://groups.google.com/forum/#!topic/golang-nuts/pxDVtPjatXo

sfllaw commented 4 years ago

Also relevant: https://groups.google.com/g/golang-nuts/c/iAD0NBz3DYw

beoran commented 4 years ago

In the latest generics design, we find the following:

However, it‘s tedious to have to write interface{} every time you write a generic function that doesn’t impose constraints on its type parameters. So in this design we suggest a type constraint any that is equivalent to interface{}. This will be a predeclared name, implicitly declared in the universe block. It will not be valid to use any as anything other than a type constraint.

(Note: clearly we could make any generally available as an alias for interface{}, or as a new defined type defined as interface{}. However, we don't want this design draft, which is about generics, to lead to a possibly significant change to non-generic code. Adding any as a general purpose name for interface{} can and should be discussed separately).

I think the "It will not be valid to use any as anything other than a type constraint." is a bad idea. It is far more simple and consistent to allow the use of the any alias type any = interface{} everywhere, in the hope that as generics are introduced, we'll see it getting use less and less in non-generic code.

billinghamj commented 4 years ago

I feel very strongly that any should either be allowed in both type parameters and everywhere else, or allowed nowhere at all. And quite strongly that it should be allowed.

Having a confusing-to-beginners pattern of using interface{} is negative

Having any which means the same thing but is only sometimes allowed will add to this confusion and thus is significantly worse

The only thing we must not do is make it more confusing. Adding any would be a nice improvement, but not conditionally

quenbyako commented 3 years ago

I disagree too. Maybe in Dart, Typescript, Rust, etc. it can be good improvement, otherwise in go, you can just put in package simple file like extra.go

package something

type any = interface{}
type null = struct{} // for channels f.e.

// something that you want to syntax sugaring

could be amazing practice! I mean, any/null is good, it's more readable, than interface{}/struct{}, but it can be implemented just now, in specific cases that you want.

pellared commented 3 years ago

@ianlancetaylor

If we are able to add generics to the language, then I suspect there will be many fewer cases where people write interface{}, so the alias would have correspondingly less value. Putting on hold pending a decision on generics.

From design proposal:

So in this design we suggest a type constraint any that is equivalent to interface{}

If I understand it correctly, it would result in having the "alias" ONLY for type parameter constrains (and nowhere else). IMO it would make the language not consistent.

g13013 commented 3 years ago

I believe it would be wise to either rename any to interface{}, or simply deprecate interface{}, having two types that mean the same thing would put a lot confusion and ambiguity for new adopters of go in the future.

fzipp commented 3 years ago

I believe it would be wise to either rename any to interface{}, or simply deprecate interface{}, having two types that mean the same thing would put a lot confusion and ambiguity for new adopters of go in the future.

@g13013 They are not two types. One is the name (an identifier), the other is the actual thing. It's like being confused about fmt.Stringer and interface{String() string} existing at the same time.

g13013 commented 3 years ago

@fzipp I think that actually any means interface{}, am I wrong ?

fzipp commented 3 years ago

@g13013 Yes, any is an identifier for interface{}, like fmt.Stringer is an identifier for interface{String() string}.

g13013 commented 3 years ago

any is just an alias to interface{} it might be called an identifier, still it's the same thing, the point is, having two things that mean the same thing should be avoided.

fzipp commented 3 years ago

@g13013 Why do you not complain about having two ways to write fmt.Stringer? You can write func f(s fmt.Stringer) {} or you can write func f(s interface{String() string}) {}

Why do you not complain about having two ways to write io.Writer? You can write func f(w io.Writer) {} or you can write func f(w interface{Write(p []byte) (n int, err error)}) {}

Why is it suddenly a problem with any and interface{}?

quenbyako commented 3 years ago

@fzipp if so, how to use json.Unmarshal without interface{}/any?

any could be just alias) but i agree, that this problem could be solved without change language specification

g13013 commented 3 years ago

@fzipp I would complain about func f(s interface{String() string}) {} and func f(interface{Write(p []byte) (n int, err error)}, we would definitely ban them in my team as it makes the code unnecessarily unreadable.

interface{} is an another story, its the de facto standard in go to accept any type, I think you got me wrong, if the go team choses to introduce any for generics, than the deprecation should happen in the documentation and guides not in the language itself, if we are willing to keep interface{}, then I would definitely prefer to write generics with interface{} instead of any.

ucirello commented 3 years ago

re: https://github.com/golang/go/issues/33232#issuecomment-525489884

Now that the proposal is accepted (https://github.com/golang/go/issues/33502#issuecomment-776951191), what's next?

ianlancetaylor commented 3 years ago

I believe the proposal here is to add a new predeclared identifier any that is an alias for interface{}.

I'll take this proposal off hold.

nadiasvertex commented 3 years ago

I strongly support use of "any" as an alias for interface{}, and I also support deprecation of interface{}. I think there may be an argument for having "any" and "anyref". The argument could be that "any" in a generic context has a different meaning and performance consideration than "any" in a non-generic context.

For example:

func M[T any](p1 T, p2 any) {
  // ...
}

The "p1" parameter is fundamentally different than the "p2" parameter. I can imagine some user confusion here.

That being said, I would like to discourage adopting two separate type names. I strongly support using "any" as both the generic constraint, and the alias for reflection-based polymorphism. Semantically "any" in both places means the same thing: a type must satisfy the constraint to be legally passed. There are differences in how the type looks to the user at compile time, but they aren't significant enough to warrant bifurcation.

gudvinr commented 3 years ago

I don't think this is a good idea.

Semantically speaking, interface{} means "interface that holds value, whose type guaranteed to have empty method set". Important thing there is that interface{} does not define type of value. It just wraps value of concrete type to use it in runtime.

I see that recent rise in discussion is probably because of approval of type parameters and its any type parameter. It is wrong to extend its usage to language, because in scope of type parameters any means concrete type rather than value wrapped into interface{}.

This change can be misleading since one may assume that any is concrete type while it is not.

quenbyako commented 3 years ago

@gudvinr in terms of go generics: interface{} == any, but any != interface{}. you can't set any value as any type, but you can set, for example (interface{})(nil)

gudvinr commented 3 years ago

in terms of go generics: interface{} == any, but any != interface{}. you can't set any value as any type, but you can set, for example (interface{})(nil)

It makes perfect sense because in terms of generics interface{} and any both aren't types but type constraints which later will be deducted into concrete type. In second statement interface{} is a valid type for value that holds another value.

quenbyako commented 3 years ago

@gudvinr absolutely not. In the world of golang, the interface{} is a type, same as fmt.Stringer, or error or even http.Server. The interface{} type itself is a pointer to anything that corresponds to the specified interface (f.e. error allows to store pointer to literally any value, that has at least Error() string method, interface{} allows to store pointer to literally any value that has at least be correct for go runtime). any is not a type as you correctly said, but it's constraint for specific types.

fzipp commented 3 years ago

I don't understand where the confusion or the desire to distinguish comes from.

These are interfaces:

any             // identifier
interface{}         // no identifier

fmt.Stringer            // identifier
interface{String() string}  // no identifier

error               // identifier
interface{Error() string}   // no identifier

You can use them as types:

var x any
var x interface{}

var x fmt.Stringer
var x interface{String() string}

var x error
var x interface{Error() string}

You can use them as constraints:

[T any]
[T interface{}]

[T fmt.Stringer]
[T interface{String() string}]

[T error]
[T interface{Error() string}]

An interface is not intrinsically a type or a constraint, it's just a method set. Whether an interface acts as a type or as a constraint depends on its usage position, not on its identifier or lack of identifier.

nadiasvertex commented 3 years ago

This is a great summary of why the feature should exist. It is syntactic sugar and isn't more confusing than the existing situation. The current situation is not easier for learners, it does not provide more information or syntactic clues. And "any" is not more confusing for advanced users than interface{} is.

Providing an identifier that means "interface{}" is strictly better in all ways.

On Thu, Mar 11, 2021 at 3:45 PM fzipp @.***> wrote:

I don't understand where the confusion or the desire to distinguish comes from.

These are interfaces:

any // identifier interface{} // no identifier

fmt.Stringer // identifier interface{String() string} // no identifier

error // identifier interface{Error() string} // no identifier

You can use them as types:

var x any var x interface{}

var x fmt.Stringer var x interface{String() string}

var x error var x interface{Error() string}

You can use them as constraints:

[T any] [T interface{}]

[T fmt.Stringer] [T interface{String() string}]

[T error] [T interface{Error() string}]

An interface is not intrinsically a type or a constraint, it's just a method set. Whether an interface acts as a type or as a constraint depends on its usage position, not on its identifier or lack of identifier.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/golang/go/issues/33232#issuecomment-797037075, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPQTPAZILO32FHT4GND573TDETXLANCNFSM4IF5A7QQ .

gudvinr commented 3 years ago

@nadiasvertex If you consider yourself "advanced user" you probably already know that usage of empty interfaces kinda discouraged.
In reality, most code written by inexperienced folks won't naturally contain interface{}. There are limited number of cases where you can't avoid them and it's basically either libraries for data serialization or some sort of containers and wrappers (like sync/atomic).

These kind of libraries usually require interface{} as input parameter(s). Even then it's being converted to reflect.Value right at the beginning most of the time.
In some cases type parameters reduce usage of interface{} even more.
And most importantly, this alias won't benefit existing libraries in any way because rewriting them using new alias is waste of time.

So why bother adding alias for sake of adding it? It has zero impact on existing code, it can't be widely used by "learners" and "advanced users" only use interface{} in write-once kind of libraries.
It isn't better in any meaningful way outside of discussions on how cool it would be.

gudvinr commented 3 years ago

@fzipp You should try to expand your example outside of definitions since they constructed in a way that only benefits your point but doesn't show difference in semantics while focusing on specific difference between having identifier or not.

In your last example, you only define constraints as type parameters. Code inside functions that use those parameters won't use these constraints explicitly.

While if you have interfaces as types you will probably use them a little more often than that. In that case, assigning something to variable of type any seems very wrong in statically typed language.

An interface is not intrinsically a type or a constraint, it's just a method set.

Interfaces are types that specify method sets. Not in some ephemeral sense, it is a struct that holds pointer and RTTI header.
And when you have some value with interface{} type you explicitly state that "value of that type will be wrapped in a struct".
It is well-known behaviour for advanced users and explained basically in every tutorial and article.

Alias for interface{} hides that subtle yet important meaning since any can easily be misinterpreted as "any type" which is not true. That means you must always keep this in mind this additional information.
For example, C++ users might assume that it works like auto and does some deduction at compile time.
If you have some previous experience with dynamically typed languages you might also wrongly assume that any has some properties that it doesn't have. Interfaces allow you to have type information in runtime but Go still statically typed language.

Constraints, on the other hand, are meta-types. You do not use constraint as a type inside parameterized function. In that case any literally means "any type" and after applying [T any] constraint, var value T will have value of type T, not type any.

nadiasvertex commented 3 years ago

@nadiasvertex If you consider yourself "advanced user" you probably already know that usage of empty interfaces kinda discouraged.

It doesn't matter if they are discouraged or not. They are the only mechanism that exists for accomplishing certain tasks. If you look through the go standard library you will see them used over and over. Whether or not a new user needs to (or should) "write" an interface{} taking function is immaterial since they certainly will need to use one.

For example, what is more immediately clear to a new user reading the following (real) prototype from the library:

func SliceStable(x interface{}, less func(i, j int) bool)

or

func SliceStable(x any, less func(i, j int) bool)

nadiasvertex commented 3 years ago

And when you have some value with interface{} type you explicitly state that "value of that type will be wrapped in a struct".

That's not true. You can turn integers into receivers with no more ceremony than a "type" statement. All function calls in go are statically bound at compile time. There is no implicit automatic runtime dispatch. While type assertions do invoke the reflection machinery, the asserted value is statically bound which (among other reasons) is why you have to check to make sure it worked before using it.

gudvinr commented 3 years ago

They are the only mechanism that exists for accomplishing certain tasks. If you look through the go standard library you will see them used over and over.

As you may notice, I said exact same thing after sentence you quoted. These usages limited by basically libraries for data serialization or some sort of containers and wrappers.

For example, what is more immediately clear to a new user reading the following (real) prototype from the library

Neither. You still need to explain what to pass in documentation. This case is also covered by "type parameters reduce usage of interface{}". So I don't get what's your point here.
And you still don't pass value of "any type", you pass wrapped value of concrete type so it's probably important to have this information right on hand.

gudvinr commented 3 years ago

And when you have some value with interface{} type you explicitly state that "value of that type will be wrapped in a struct".

That's not true.

What do you mean? This is struct that describes interfaces in Go runtime:

type iface struct {
    tab  *itab
    data unsafe.Pointer
}

type statement uses information from tab (which contains both interface type information and dynamic type information of stored type) to extract value from data. It is neither implicit nor automatic.

fzipp commented 3 years ago

@fzipp You should try to expand your example outside of definitions since they constructed in a way that only benefits your point but doesn't show difference in semantics while focusing on specific difference between having identifier or not.

In your last example, you only define constraints as type parameters. Code inside functions that use those parameters won't use these constraints explicitly.

@gudvinr Ok, here's an example with implementation:

func Concat1(a []fmt.Stringer) string {
    r := ""
    for _, s := range a {
        r += s.String()
    }
    return r
}

func Concat2[T fmt.Stringer](a []T) string {
    r := ""
    for _, s := range a {
        r += s.String()
    }
    return r
}

fmt.Stringer is an interface. In Concat1 it acts as a type, in Concat2 it acts as a type constraint. The difference in semantics between these two functions does not come from a difference in names (it's fmt.Stringer in both cases) but from a difference in usage position. So why should interface{} / any be an exception. Why artificially limit the scope of any as an identifier for interface{} to type parameter lists when this limitation does not exist for all the other interfaces. Implementation details like boxed in one case and not boxed in the other case (depends on how type parameters will be implemented) can't be the criterion as the fmt.Stringer example shows.

atdiar commented 3 years ago

I think that all the debate stems from the overloading of interface with type constraints which I don't like.

Any is a constraint. interface{} is a type that the overarching proposal wants to be allowed as a use to denote the same constraint.

The issue is that an interface has in general adhoc constraints itself (adhoc because the constraints are different depending on the compiler implementation of interface types)

Using interfaces as types as well asconstraints can be confusing at best. Better way would be to have a notation to explicitly derive a constraint object from a type.

In our present case,

const Any = type constraint{
    interface{} 
} 

And even that needs to be looked into because the equality of constraints is not assured if we want to implement a constraint checker at the compiler level and not only user code. In that case, we would need some type of constraint deriving function that would massage the type's constraint sets a bit.

Anyway, semantically, any should be different from interface{}. It's more obvious when we look at interfaces and constraints in general. Using constraints should not be boxing since constraints should not be types. Using interfaces is boxing. Having double semantics for interfaces is a bit unfortunate for future-proofing potential language changes.

[edit] Cf. semantic subtyping for the warnings (the type systems are similar to Go but with variance.. We do not even have vatiance in Go which is actually great for constraint checking)

Also cf. intensional semantics where meaning is representation which is not the case for interfaces as constraints.

Cf. 2D semantics.

fzipp commented 3 years ago

I think that all the debate stems from the overloading of interface with type constraints which I don't like.

The fact that interfaces can also act as constraints is the whole genius and beauty of the type parameters design.

atdiar commented 3 years ago

Unfortunately, I don't think it's manageable after having thought hard about it. It's confusing and the literature seems to warn against it. I initially also thought that interfaces would be sufficient but the problem is that they are types.

fzipp commented 3 years ago

Unfortunately, I don't think it's manageable after having thought hard about it. It's confusing and the literature seems to warn against it. I initially also thought that interfaces would be sufficient but the problem is that they are types.

The Featherweight Go paper has shown that interfaces as constraints work out. I'd be interested in the literature that you're referring to. I'm more worried about type lists in interfaces if they make some interfaces unusable as types. But I hear that they are working on an update to the design that addresses this.