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

Declare field in Interface #412

Closed
gopherbot opened this issue Dec 11, 2009 · 2 comments
Closed

Declare field in Interface #412

gopherbot opened this issue Dec 11, 2009 · 2 comments

Comments

@gopherbot
Copy link
Contributor

by Ziyu4Huang:

From this thread, I think it might be useful to let you declare field
other then write an access function
http://groups.google.com/group/golang-nuts/browse_thread/thread/ffa1c12a24ef8c5c#

type Foo struct {
  Index int;
}

type FooIF interface {
  GetIndex() int;
  Index int;//can we have field declare in interface ?
  //let compiler check if structure have this field
}

//access function is annoy.
func (f *Foo) GetIndex() int { return f.Index}


See full explain codes here.
http://gopaste.org/view/eGini


Actually this suggestion is just let user acces witout use reflection.
Since compiler can using Interface do thing like vtable in C++,
I think it should be possible to know what is offset of a field in
structure hide by interface.
@griesemer
Copy link
Contributor

Comment 1:

Allowing field declarations in interfaces is a very bad idea. It destroys the strict 
separation between implementation and interface. Once the feature is available, it 
will be (mis)used, and the resulting code will become less flexible because such 
interfaces can only be satisfied by struct with the respective field. We had discussed 
this feature very early on in the language design and decided against it.
A better approach is to use accessor functions (and one might argue that Go should 
provide automatic accessors, but that is a different discussion).

Status changed to WontFix.

@gopherbot
Copy link
Contributor Author

Comment 2 by Ziyu4Huang:

1. I understand it may work only on structurt, but why feature on interface must
apply too all kind of data ?
2. You know what you want to export, I really don't see the difference to export
accessor function.
I don't know what's the rule called "strict sparation from implementation", what does
it mean and what does it cuase trouble in real?
You have to face this issues,  (look how scala and groovy solve accessor functions).
At least this approach is less confusing, faster(no function call overhead).

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

2 participants