Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Function accepting a slice of interface types.
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ian Lance Taylor  
View profile   Translate to Translated (View Original)
 More options Nov 11 2010, 5:39 pm
From: Ian Lance Taylor <i...@google.com>
Date: Thu, 11 Nov 2010 14:39:32 -0800
Local: Thurs, Nov 11 2010 5:39 pm
Subject: Re: [go-nuts] Re: Function accepting a slice of interface types.

Scott Pakin <scott+...@pakin.org> writes:
> On Nov 10, 8:14 pm, Nigel Tao <nigel.tao.gn...@gmail.com> wrote:
>> That might work for reads, but I don't think it works for writes. To
>> repeat my earlier example, what should this program do:

>> -------------
>> f := func(z []interface{}) {
>>   z[2] = "notAnInt"
>>   println(z[2])}

>> x := make([]int, 3)
>> f(x)
>> println(x[2])
>> -------------

>> f looks like a perfectly reasonable function, since string satisfies
>> interface{}, but you can't store a string in x.

> I'd think it should fail.  x has a concrete type with a known layout
> in memory.  Passing x to f() and binding it to a z of type
> []interface{} shouldn't affect either the type or layout of x.  f() is
> just saying it doesn't know at compile time what concrete type it'll
> be given.  At run time, z has a concrete type: an array of ints, each
> of which properly implements the empty interface.  Assigning a string
> to z[2] should therefore be no different from assigning a string to
> x[2].

But there is a real difference.  Assigning a string to x[2] fails at
compile time.  Assigning a string to z[2] only fails at run time.  One
aspect of Go is that it carefully indicates which operations can fail at
run time.  E.g., a simple assignment can never fail at runtime.  A type
assertion can.

Failing to maintain that clear distinction between compile time failures
and runtime failures is one of the key difficulties of Java's generics
implementation.

Ian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.