Message from discussion
Generics - Maybe we don't need them?
Received: by 10.68.125.233 with SMTP id mt9mr19404240pbb.5.1335886601436;
Tue, 01 May 2012 08:36:41 -0700 (PDT)
X-BeenThere: golang-nuts@googlegroups.com
Received: by 10.68.115.43 with SMTP id jl11ls12331923pbb.4.gmail; Tue, 01 May
2012 08:36:35 -0700 (PDT)
Received: by 10.68.136.199 with SMTP id qc7mr580666pbb.17.1335886595850;
Tue, 01 May 2012 08:36:35 -0700 (PDT)
Date: Tue, 1 May 2012 08:36:34 -0700 (PDT)
From: Andy Balholm <andybalh...@gmail.com>
To: golang-nuts@googlegroups.com
Cc: Ian Lance Taylor <i...@google.com>, Erwin <snesse...@gmail.com>,
josvazg <josv...@gmail.com>
Message-ID: <32403964.32.1335886594934.JavaMail.geo-discussion-forums@pbbof7>
In-Reply-To: <CAFm1DREmAHaW7TCeRG7Gjh=GgU4S_buGqLPETP4Q89QwNmJTCg@mail.gmail.com>
References: <29295442.656.1321911879108.JavaMail.geo-discussion-forums@prnv30>
<CABVG1pCD8PK8Vz05GBxEdxSrTam46MQ+9Ke4n0Hk1a8_Op+nEw@mail.gmail.com>
<e603ba00-ed3e-4f75-be26-3a0d79829f34@v31g2000prg.googlegroups.com>
<27206248.641.1322477712092.JavaMail.geo-discussion-forums@prmr38>
<29803728.643.1322477822983.JavaMail.geo-discussion-forums@prmr38>
<CAODcfmAZt2oXu6JPkBOHCq+qnEEBoB038p72tm_tJzP4uzqyUw@mail.gmail.com>
<13515141.137.1322488316939.JavaMail.geo-discussion-forums@yqeu24>
<CABVG1pADc2LeZ=XgxXdN1GnZmXdPScpAyPJ2ki=FykAQG0T9dg@mail.gmail.com>
<27179304.2810.1335351328183.JavaMail.geo-discussion-forums@ynbv36>
<CAHsVM3m-6vE3OjHvObo_ARY+XbAfhEZEuqMoEtPFPiOMvzBXqQ@mail.gmail.com>
<2120367.3338.1335428694155.JavaMail.geo-discussion-forums@vbmi19>
<mcrlililelo.fsf@dhcp-172-18-216-180.mtv.corp.google.com>
<CA+JLj4wR2mtdr6ujd2FjPXWboALsJ6Pwonm4B07Q3PDA9EnDHQ@mail.gmail.com>
<CAKOQZ8w1XQ=XPQdW13X9Wn64eqAKLXw4TkFot7jeqqvhbAJGEg@mail.gmail.com>
<CAFm1DREmAHaW7TCeRG7Gjh=GgU4S_buGqLPETP4Q89QwNmJTCg@mail.gmail.com>
Subject: Re: [go-nuts] Re: Generics - Maybe we don't need them?
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_30_2116562.1335886594933"
------=_Part_30_2116562.1335886594933
Content-Type: multipart/alternative;
boundary="----=_Part_31_5370397.1335886594933"
------=_Part_31_5370397.1335886594933
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
What if generics had to be explicitly instantiated? In other words, a
generic type declared like generic type Vector<T> []T could not be used in
a variable declaration, only to define a concrete type like this: concrete
type intVector Vector<int>
Then the generic type would be compiled exactly once for each concrete type
derived from it.
The same would apply to functions:
generic func Sort<T>(data []T) {
// TODO: replace the bubble sort with something faster!
done := false
for !done {
done = true
for i := range data {
if i > 0 && data[i-1] > data[i] {
data[i-1], data[i] = data[i], data[i-1]
done = false
}
}
}
}
concrete func sortInts Sort<int>
------=_Part_31_5370397.1335886594933
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
What if generics had to be explicitly instantiated? In other words, a gener=
ic type declared like <span class=3D"Apple-style-span" style=3D"font-f=
amily: 'courier new', monospace; ">generic type Vector<T> []T </=
span><span class=3D"Apple-style-span" style=3D"font-family: arial, sans-ser=
if; ">could not be used in a variable declaration, only to define a concret=
e type like this: </span><span class=3D"Apple-style-span" style=3D"fon=
t-family: 'courier new', monospace; ">concrete type intVector Vector<int=
> </span><div><span class=3D"Apple-style-span" style=3D"font-family=
: 'courier new', monospace; "><br></span></div><div><font class=3D"Apple-st=
yle-span" face=3D"arial, sans-serif">Then the generic type would be compile=
d exactly once for each concrete type derived from it. </font></div><d=
iv><font class=3D"Apple-style-span" face=3D"arial, sans-serif"><br></font><=
/div><div><font class=3D"Apple-style-span" face=3D"arial, sans-serif">The s=
ame would apply to functions:</font></div><div><font class=3D"Apple-style-s=
pan" face=3D"'courier new', monospace">generic func Sort<T>(data []T)=
{</font></div><div><font class=3D"Apple-style-span" face=3D"'courier new',=
monospace"> // TODO: replace the bubble sort with something f=
aster!</font></div><div><font class=3D"Apple-style-span" face=3D"'courier n=
ew', monospace"> done :=3D false</font></div><div><font class=
=3D"Apple-style-span" face=3D"'courier new', monospace"> for !=
done {</font></div><div><font class=3D"Apple-style-span" face=3D"'courier n=
ew', monospace"> done =3D true</font></div><div>=
<font class=3D"Apple-style-span" face=3D"'courier new', monospace"> &=
nbsp; for i :=3D range data {</font></div><div><font class=3D=
"Apple-style-span" face=3D"'courier new', monospace"> &=
nbsp; if i > 0 && data[i-1] > data[i] {</font><=
/div><div><font class=3D"Apple-style-span" face=3D"'courier new', monospace=
"> data[i-1], data[i=
] =3D data[i], data[i-1]</font></div><div><font class=3D"Apple-style-span" =
face=3D"'courier new', monospace"> =
done =3D false</font></div><div><font class=3D"Apple-style-s=
pan" face=3D"'courier new', monospace"> &=
nbsp; }</font></div><div><font class=3D"Apple-style-span" face=3D"'courier =
new', monospace"> }</font></div><div><font class=
=3D"Apple-style-span" face=3D"'courier new', monospace"> }</fo=
nt></div><div><font class=3D"Apple-style-span" face=3D"'courier new', monos=
pace">}</font></div><div><font class=3D"Apple-style-span" face=3D"'courier =
new', monospace"><br></font></div><div><font class=3D"Apple-style-span" fac=
e=3D"'courier new', monospace">concrete func sortInts Sort<int></font=
></div>
------=_Part_31_5370397.1335886594933--
------=_Part_30_2116562.1335886594933--