Skip to content

proposal: Make nil nil again #70367

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

Closed
okm-8 opened this issue Nov 15, 2024 · 3 comments
Closed

proposal: Make nil nil again #70367

okm-8 opened this issue Nov 15, 2024 · 3 comments
Labels
Milestone

Comments

@okm-8
Copy link

okm-8 commented Nov 15, 2024

Proposal Details

Due to learn the issue #70364 I would like to make a proposal:

way one: Because nil is suitable value for any interface, it should be possible to check a value is nll despite some function return nil as a pointer to the concrete type that implements this interface

OR

way two: nil should not to be suitable as value for any interface (As it usually working)

I, personally, mean that it is not correct way to mess pointer and reference, so passing nil as a reference should be prohibited

package main

import (
	"fmt"
)

type Unwrapable interface {
	Unwrap() error
}

type TestStruct struct {
	err error
}

func (err *TestStruct) Unwrap() error {
	return err.err
}

func doSomethingElse() *TestStruct {
	return nil
}

func HandleUnwrapable(err Unwrapable) {
        // should be possible ever by the way one
	if err != nil {
		fmt.Printf("err: Not nil, type: %T, address: %p\n", err, err)
	} else {
		fmt.Printf("err: Nil, type: %T, address: %p\n", err, err)
	}
}

func main() {
	test := doSomethingElse()

	if test != nil {
		fmt.Printf("err: Not nil, type: %T, address: %p\n", test, test)
	} else {
		fmt.Printf("err: Nil, type: %T, address: %p\n", test, test)
	}

	HandleUnwrapable(test) // should be prohibited by the way two

	// Output
	// err: Nil, type: *main.TestStruct, address: 0x0
        // err: Not nil, type: *main.TestStruct, address: 0x0 - unexpected
}

Others languages behaviour with nil reference
image

@okm-8 okm-8 added the Proposal label Nov 15, 2024
@gopherbot gopherbot added this to the Proposal milestone Nov 15, 2024
@seankhliao
Copy link
Member

see the above links

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2024
@Jorropo
Copy link
Member

Jorropo commented Nov 15, 2024

This has been already discussed before and you need a story on how to do without introducing a backward breaking change to the language for a proposal like this to be considerable.

Also https://go.dev/doc/faq#nil_error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants