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

why a nil interface cannot cast to sub type pointer? #29763

Closed
egmkang opened this issue Jan 16, 2019 · 10 comments
Closed

why a nil interface cannot cast to sub type pointer? #29763

egmkang opened this issue Jan 16, 2019 · 10 comments

Comments

@egmkang
Copy link

egmkang commented Jan 16, 2019

this is a language design question

//HelloReplay is a type implemented proto.Message interface (protobuf's proto.Message)
var nilInterface proto.Message

// 1
var msg = nilInterface.(*HelloReply)

why the 1 will panic?

proto.Message and HelloReplay has a stable relationship.

Why this type conversion is not allowed? Especially a nil to *T nil cast.

what if a empty proto.Message object can cast to *HelloReply object, will the system become unsafe?

but if it can, we could write less codes, we could not write useless codes for example type switch!!!

it's useful.

@mvdan
Copy link
Member

mvdan commented Jan 16, 2019

For questions about Go, see https://golang.org/wiki/Questions. Or just read the language spec, which goes into detail to explain how this works and why.

@mvdan mvdan closed this as completed Jan 16, 2019
@ianlancetaylor
Copy link
Contributor

For future questions, please show a small self-contained program that demonstrates what you are asking. Go does not have any concept like "sub type pointers" so I'm not sure what you mean by that.

@egmkang
Copy link
Author

egmkang commented Jan 16, 2019

type Interface interface {
  Get()
}

type A struct {
}

func (this *A) Get() {
}

var a Interface
var b = a.(*A)     //this will panic

do you know what i mean?
@iamoryanmoshe

@ianlancetaylor
Copy link
Contributor

I'm sorry, I do not know what you mean. I agree that with the code you show the initialization of b will panic, because the value in a is not a *A. It is nil. On the other hand, you could write

var a Interface = &A{}
var b = a.(*A)

and then the code would not panic.

@egmkang
Copy link
Author

egmkang commented Jan 17, 2019

@ianlancetaylor
i know how to write some code will not panic. i'm considering, why not allow a nil interface convert to a nil pointer?

@ianlancetaylor
Copy link
Contributor

Because a nil interface has no type. It is not the same as an interface that holds a typed nil pointer. For example, these lines will also not panic:

var a Interface = (*A)(nil)
var b = a.(*A)

See also https://golang.org/doc/faq#nil_error for a related issue.

@egmkang
Copy link
Author

egmkang commented Jan 17, 2019

@ianlancetaylor
i know a nil interface has no type. but when it could convert to *T, it's just a nil *T, nil can do nothing.

for example, if a nil interface cast to a pointer will not panic:

var a Interface
var b = a.(*A)     //then b is nil

but right now , i must write some code like:

var a Interface 
var b, ok = a.(*A)

switch a.(type) {
case *A:
  b = a.(*A)
default:
  b = nil
}

if a == nil {
  b = nil
} else {
  b = a.(*A)
}

the codes is short and beautiful.

@ianlancetaylor
Copy link
Contributor

var a Interface
var b, _ = a.(*A)

@egmkang
Copy link
Author

egmkang commented Jan 17, 2019

@ianlancetaylor

i wrote this sample.

three ways to cast a nil interface to a nil pointer, none is simple.

@mvdan
Copy link
Member

mvdan commented Jan 17, 2019

As said before, please take this question to the forums. This thread is going in circles and there's no issue here, so I'm going to lock it for now.

@golang golang locked and limited conversation to collaborators Jan 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants