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

proposal: Go 2: Keyword to reflect an empty value for scenarios where the type is not always known (specially in context to use with Generics) #50395

Closed
blaubaer opened this issue Dec 30, 2021 · 1 comment

Comments

@blaubaer
Copy link

Background

Currently there are a couple of different empty values, like:

var aString string // =  ""
var anInt int // = 0
var aBool bool // = false
var aPointer *string // = nil

type AStruct struct { /* [...] */ }
var aStruct AStruct  // = AStruct{}

type AnInterface interface { /* [...] */ }
var anInterface AnInterface // = nil

// ...

Assuming now you have a functions like this...

func doWithString() (string, error) {
    // [..]
   return "", errors.New("Doh!")
}

func doWithInt() (int, error) {
    // [..]
   return 0, errors.New("Doh!")
}

func doWithAStruct() (AStruct, error) {
    // [..]
   return AStruct{}, errors.New("Doh!")
}

func doWithAnInterface() (AnInterface, error) {
    // [..]
   return nil, errors.New("Doh!")
}

Problem

We got used to it, that we need for all of the scenarios above to know which type it is, although it is sometimes quite annoying although it might be clear what kind of value we need at this point (due to the fact that Go is that type safe).

But once we start with Generics it is getting tricky. Assuming the following example:

func doSomething[T any]() (T, error) {
    // [..]
   return ???, errors.New("Doh!")
}

In this case you cannot simply return a value. There is a workaround, but it is not quite handy in all scenarios:

func doSomething[T any]() (T, error) {
    // [..]
   var emptyVal T
   return emptyVal, errors.New("Doh!")
}

Proposal

As the compiler is currently already doing some context related assumptions, like:

func returnFloat64() float64 {
	return 0 // float64(0)
}

func returnUint16() uint16 {
	return 0 // uint16(0)
}

...extend this functionality with another token to all meaningful contexts. I would purpose the - sign here, because I would assume it will not break old code, with not clash with future use and might be quite intuitive, like:

func returnFloat64() float64 {
	return -
}

func returnUint16() uint16 {
	return - // uint16(0)
}

func returnBool() bool {
	return - // bool(false)
}

func returnString() string {
	return - // string("")
}

func returnPtrString() *string {
	return - // nil
}

func returnSlice() []string {
	return - // nil
}

func returnMap() map[int]string {
	return - // nil
}
func returnAStruct() AStruct {
	return - // AStruct{}
}

func returnAnInterface() AnInterface {
	return - // nil
}

Consequently this will solve our Generics problem the following way:

func doSomething[T any]() (T, error) {
   // [..]
   return -, errors.New("Doh!")
}

Additional benefit

It might also improve the checking for emptiness after we retrieved a value from a method, because here we can use the same token again, in all cases if something is empty, like:

if returnFloat64 == - { 
     // [..]
}
if returnSlice == - { // in case of slice it should match `nil` and `len(x) == 0`
     // [..]
} 
if returnMap == - { // in case of map it should match `nil` and `len(x) == 0`
     // [..]
} 
if returnAStruct == - { // same as `returnAStruct == AStruct{}`
     // [..]
} 
if returnAnInterface == - { // same as `returnAnInterface == nil`
     // [..]
} 
// ..
@gopherbot gopherbot added this to the Proposal milestone Dec 30, 2021
@seankhliao
Copy link
Member

Duplicate of #35966

@seankhliao seankhliao marked this as a duplicate of #35966 Dec 30, 2021
@golang golang locked and limited conversation to collaborators Dec 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants