|
|
|
|
|
by junke
3586 days ago
|
|
> A method defined in a new package might break a type switch (or some reflection-based code) in another package. In that case the code would break because it assumes too much. If the language was designed differently, people would code differently too. This is like having a Java abstract class which "knows" all the possible subclasses in advance: if it breaks, it is the responsibility of that abstract class for having too much coupling. > Plus it's unclear what should happen with interfaces when multiple packages define methods with the same name. I am not sure I understand: if methods are defined in different packages, they have different qualified names, don't they? is there any ambiguity here? |
|
Sure, but we want interfaces to work the way they work now, because it's useful and reduces coupling.
> This is like having a Java abstract class which "knows" all the possible subclasses in advance: if it breaks, it is the responsibility of that abstract class for having too much coupling.
The analogy is not very apt, because in Go code the consumer is the one which is broken, not the producer. In the current design the consumer can make static assumptions about code, if those assumptions are helpful. This works because the assumptions don't change when new packages are imported. In other words, they work because the coupling is known and stable.
You know exactly what a type is because its definition is only in a single package, the one you import. If you could define methods anywhere, you introduce hidden dependencies and more coupling. A new package might change the nature of the type. This is not acceptable.
Of course people would write code differently if this weren't the case, but then the language would be less useful.
> I am not sure I understand: if methods are defined in different packages, they have different qualified names, don't they? is there any ambiguity here?
No, they don't have different qualified names.
PrintHex(T(42)) will return "2a".Now what happens when you
where (in a hypothetical version of Go): and: What will PrintHex(T(42)) return? "2a", "0x2a", or "0x2A"?