On Tue, Aug 13, 2019 at 8:10 AM Sathish VJ <sathis...@gmail.com> wrote:

> So doing *type X Y* is just a type declaration then?
>
>
In a certain sense

  type X Y

and

  type X = Y

are both type declarations. They differ in that the first is generative,
whereas the other is a synonym. In a generative pattern, you are
generating, or minting, a new type X which differs from Y; whereas in the
the synonym situation, the types X and Y admits equality at the type level.
Historically, Go only had generative types. The second case was added to
the language, because of a need to facilitate large scale program
rewriting. By aliasing a type, you can point an older implementation to a
new one or vice versa, until the work has been completed. This avoids the
need of one large patch across several modules, and allows for a more
partial approach in which the programmer can split the patch over multiple
commits.

As a simple example of why you would often prefer types to differ, consider
something like

  type meter int
  type yard int

Clearly, you don't want to admit the interchange of two different distance
units (Even worse: Pound-Force to Newton's). This is by far the common case
in programming, and they are crucial for producing modular code via
encapsulation and isolation.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGrdgiXCSygqAC%3DyqPY%2But0Hm_MZ49qX2ZJnixuE1%2BLt6aVv-w%40mail.gmail.com.

Reply via email to