At first read, I really like everything but the syntax's use of the period/dot and I am eager for something like this to exist in Go.
In my opinion the period is too inconspicuous-
Given: t := make([]T, 10)
Here are a few ideas:
var vt[]#T = t
var vt[]$T = t
var vt[]:T = t
var vt[]~T = t
--
---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
On Tue, May 14, 2013 at 8:01 AM, Brad Fitzpatrick <brad...@golang.org> wrote:
Design doc for adding read-only slices to Go:https://docs.google.com/a/golang.org/document/d/1UKu_do3FRvfeN5Bb1RxLohV-zBOJWTzX0E8ZU1bkqX0/edit#heading=h.2wzvdd6vdi83
Comments welcome.So this is the (part of?) revised contribution process that you mentioned in another post?"The Go Language Change Proposal"
Will readonly slices support slicing? (i think it should, but it's not mentioned anywhere in
the docs)
i'm wondering if string could be treated as a [].byte?that is, at universal block, we have a declaration:type string [].byte
how does the readonly slices interact with the reflect system?will it introduce a new Kind or just a readonly flag for the slice kind (so as to pave the wayfor future extensions like readonly map)?
On Mon, May 13, 2013 at 5:42 PM, Brad Fitzpatrick <brad...@golang.org> wrote:One way to view this question is: are we introducing a new type, or
>>
>> how does the readonly slices interact with the reflect system?
>> will it introduce a new Kind or just a readonly flag for the slice kind
>> (so as to pave the way
>> for future extensions like readonly map)?
>
>
> I'm open to suggestions. You can imagine reflect.Value.Cap panicing if it's
> a read-only slice, for instance. I don't know whether keeping the same Kind
> would cause problems, if the representation changes.
>
> Alternatively, we could say that the representation of a read-only slice is
> the same as a slice, and cap() still works, but the cap(v) == len(v) always.
are we introducing a special case of an interface type?
In one sense
you are proposing a polymorphic interface type that supports the
operations
Len() int
At(int) T
Slice(int, int) [].T
If we view this as an interface type, then reflect should return the
original type. If not, then it could be a new Kind, or it could be
Slice with a new field.
Either way we need to think about converting a value out of the type
and back to []byte and/or string? Is that ever possible? Does it
always involve a copy? If we adopt the interface approach, should we
support x.([]byte) and x.(string)?
Either way we need to think about converting a value out of the type
and back to []byte and/or string? Is that ever possible? Does it
always involve a copy?
For what its worth, I think I like var vt[]:T = t best.
It is (subjectively) aesthetically clean and in Go has connotations related to slicing [x : y].
At first read, I really like everything but the syntax's use of the period/dot and I am eager for something like this to exist in Go.
In my opinion the period is too inconspicuous-
Given: t := make([]T, 10)
Here are a few ideas:
var vt[]#T = t
var vt[]$T = t
var vt[]:T = t
var vt[]~T = t
On Monday, May 13, 2013 7:01:57 PM UTC-5, Brad Fitzpatrick wrote:
Thanks Brad, that looks interesting. A few comments:On Mon, May 13, 2013 at 9:01 PM, Brad Fitzpatrick <brad...@golang.org> wrote:
> Design doc for adding read-only slices to Go:
>
> https://docs.google.com/a/golang.org/document/d/1UKu_do3FRvfeN5Bb1RxLohV-zBOJWTzX0E8ZU1bkqX0/edit#heading=h.2wzvdd6vdi83
>
> Comments welcome.
- The comparison with channels seems distracting
- Behavior when mutating the slice? Undefined, or defined with memory barriers?
- Should the printf family of functions change their formatting argument?
- Does it support + in a similar way to string?
- append?
If we introduce Immutability for slices we might as well introduce a universal immutability mechanism for all types.
- The comparison with channels seems distracting
Another thing to consider is if we have an immutable slice for types with methods. Whats the rules for methods for pointers? What if one of the slice elements modifies itself by referencing itself via a globally scoped pointer. Do we create a snapshot copies of the slice, and if so how can we update immutable slice to reflect changes made to the original mutable slice?
These are the kinds of things that need to be thought about.
Another thing to consider is if we have an immutable slice for types with methods. Whats the rules for methods for pointers?
What if one of the slice elements modifies itself by referencing itself via a globally scoped pointer.
Do we create a snapshot copies of the slice,
On Mon, May 13, 2013 at 10:41 PM, Brad Fitzpatrick <brad...@golang.org> wrote:
> On Mon, May 13, 2013 at 6:38 PM, Gustavo Niemeyer <gus...@niemeyer.net>
>> - Behavior when mutating the slice? Undefined, or defined with memoryThe behavior of []byte is defined with explicit synchronization. If
>> barriers?
>
> Undefined. Like I said in the doc, we already permit data races with []byte.
> This is no different.
you say undefined, then this is different.
>> - append?I suppose it should still be mentioned, at least to define the
>
> It's not mutable, so no.
cross-compatibility with []byte appending.
>>>> In one sense>> you are proposing a polymorphic interface type that supports theChannels feel a bit different to me: they remain channels, but
>> operations
>> Len() int
>> At(int) T
>> Slice(int, int) [].T
>> If we view this as an interface type, then reflect should return the
>> original type. If not, then it could be a new Kind, or it could be
>> Slice with a new field.
>
>
> In what sense do you view our current send-only channels?
converting to a send-only or receive-only channel eliminates certain
operations. Converting []byte to [].byte changes the nature of the
value, since it no longer has a capacity.
Converting string to
[].byte changes the meaning of the string significantly--e.g., range
and slice operations act differently.
I'm sure performance could be improved with this.It feels just different enough to feel confusing. I find the idea of a read-only map interesting. I think you've pulled a small thread on a much larger concept. Or tried to overly generalize a much smaller one (convert []byte <-> string w/o copy).I don't like introducing such a specialized type.Another option to creating a new read-only type is to create a region that can lock the mutability of a slice or un-lock the mutability of a string.bb := make([]byte, 10)lock(bb)defer unlock(bb)s := string(bb[:3]) // No copy.bb[2] = 'a' // Compile error or panic.However, I'm sure you've thought through all these options already.
That's true, it could work that way. I think one of Brad's goals isOn Mon, May 13, 2013 at 8:41 PM, Dan Kortschak
<dan.ko...@adelaide.edu.au> wrote:
> On Mon, 2013-05-13 at 20:27 -0700, Ian Lance Taylor wrote:
>> Converting []byte to [].byte changes the nature of the
>> value, since it no longer has a capacity.
>
> Why in principle should a [].byte not have a capacity? The elements of
> the view are not writable, but why should a holder of the [].byte not be
> able to expand the length to the capacity? They can make no change to
> the underlying data, all they can do is see it. The capacity means
> something slightly different here, but it still seems to have a meaning.
the copy-free conversion of string to [].byte,
and of course a string
does not have a capacity. But I suppose we could implement that by
setting the capacity of the [].byte to the length of the string.
Still, it's hard to see any particular use for a capacity for [].byte.
Your immutable slice proposal sounds very similar to strings in general. Therefore I was thinking if it's a good idea to change the string type to be an alias for [].byte in the first place (similar to rune and byte in the current specification). Have you thought about that?