-j
-j
--
why the underlying types of []Age and []int are not the same.
-j
---j
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
There is none. It would be perfectly possible and reasonable to make this possible, but it probably didn't seem worth the effort to introduce this special case into the language which has such a limited use. If you can't live without, you can always use unsafe to do it yourself (though, of course, that's unsafe).
On Fri, Aug 26, 2016 at 6:09 PM, T L <tapi...@gmail.com> wrote:
On Saturday, August 27, 2016 at 12:05:28 AM UTC+8, Jan Mercl wrote:On Fri, Aug 26, 2016, 17:59 T L <tapi...@gmail.com> wrote:--
why the underlying types of []Age and []int are not the same.The underlying type of an anonymous type []T is []T.
What are differences between memory layouts of []Age and []int?
---j
--
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...@googlegroups.com.
package main
import "fmt"
import "unsafe"
type Age int
func main() {
var ages = []Age{17, 18, 19}
var ints = *(*[]int)(unsafe.Pointer(&ages))
fmt.Println(ints)
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
package main
import "fmt"
import "unsafe"
type Age int
func main() {
var ages = []Age{17, 18, 19}
var ints = *(*[]int)(unsafe.Pointer(&ages))
fmt.Println(ints)
}
The effort is putting a special case into the language for this.
var (a float32 = 1.0aa []float32 = []float32{1.0, 2.0, 3.0})b := float64(a) //is okbb := ([]float64)(aa) //not ok
[...] I haven't checked the compiler code for this but would bet that currently there is no step disallowing such an assignment.
Ian
As an aside - there is a complication to this if this (original post) were allowed/added to the language specificationvar (a float32 = 1.0aa []float32 = []float32{1.0, 2.0, 3.0})b := float64(a) //is okbb := ([]float64)(aa) //not okIf the second conversion bb was allowed it would require a lot of extra magic work underneath -as a new slice would have to be created, large enough to store float64s - not just a case of changing the slice header to point at a new type.
V.
Hi,Generally speaking, you may find it a bit of struggling if you are trying to write Go code in an attempt to emulate *how* other languages can do, or to argue with the compiler about how a fresh learner would come to think in the first place. A tip for making the learning/adaption process smoother is to focus on *what* you want to achieve in the problem domain (instead of language domain, like fighting the compiler).Veteran gophers would be most helpful if you ask about the specific functionalities or features you are trying to build. It's very likely that the language problems you encountered don't exist at all in other well-explored approaches.
Hi,Generally speaking, you may find it a bit of struggling if you are trying to write Go code in an attempt to emulate *how* other languages can do, or to argue with the compiler about how a fresh learner would come to think in the first place. A tip for making the learning/adaption process smoother is to focus on *what* you want to achieve in the problem domain (instead of language domain, like fighting the compiler).Veteran gophers would be most helpful if you ask about the specific functionalities or features you are trying to build. It's very likely that the language problems you encountered don't exist at all in other well-explored approaches.
Ok, the alternative of my question is: why []T2 and []T1 have different underlying representations when T2 and T1 have the same underlying representation?
package maintype T1 stringtype T2 T1type T3 []T1type T4 T3type S1 T1type S2 S1type S3 []S1type S4 S3func main() {var (t3 T3t4 T4s3 S3s4 S4r T1)a := T3(t3)b := T4(t4)c := T3(t4)d := T4(t3)e := T3(s3) //not allowedf := T3(s4) //not allowedz := S4(t4) //not allowed//etcr2 := S2(r)v := S3(s3)w := S4(s4)x := S3(s4)y := S4(s3)
Andrew
TL, I have been confused by this thread, your similar questions, and in fact, most of your emails—until now.
When you write ‘Why can’t X’ as you often do, I have presumed that this was a casual and imprecise form of expressing the question “what is the reason why a different path was chosen.” Now, based on this, I see it differently. I think you mean it literally. I imagine myself walking beside you and you saying, “Why can’t trees be blue? Why can’t dogs have square heads? Why can’t cars have windows in the tires? Why don’t cats and pigs swim?” Which is to say, it seems that you are writing email as a way to share a stream of consciousness that every aspect of reality—every choice, decision, evolutionary outcome, taste, desire—could in fact be different in some other place or time or circumstance.
Now that the question unfolds as “Why is it not possible that X” (as you wrote from the start) then the answer to your emails is, “It is possible,” possible as in, “in a different language, with a different goal, on a hypothetical computer, if the most common programmer errors were not as they are, etc.” In engineering, nearly everything is possible. Not all of the possibilities are good, wise, or desirable though, so I think many here thought these other attributes were implied as part of your question. I did before but now do not.
In your spirit of “Why can’t” as in “prove that it is impossible” (of course such proofs are impossible), I offer examples of the uncommon but possible.
A different question, “Why is X disallowed in Go,” is actually much more educational. The example below, that was given you by amkguo, is the example that I thought of right at first—not about “why can’t bytes be interchangeable” but “why are Types treated as separate species.” This is a great topic. Because of huge human experience that mixing types by concept (adding two kinds of temperature or mass) results in unfortunate outcomes (many!) and similar experience in debugging as the result of many languages, C and C++ among them, doing natural-seeming invisible coercion between types. Type coercion in specific and invisible actions generally are a trouble area in large-scale software development. This is why the Go team (superstars with UNIX, C, major Gnu C/C++ implementation, Plan 9, Inferno, and maybe more importantly, careful study of common software development problems by 10,000+ programmers Google) disallowed it during the design of Go and instead require explicit casts (as in “var a int = 3; b := float64(a)”), why they treat Types as non-mixing species, and why the affordances around this are very limited.
I gave a talk in Madrid about Go as a software engineering approach assisted by a computer language. This seems a nice educational way to avoid many of the “but C allows …” or “it should be technically possible to…” confusions.
Michael
TL, I have been confused by this thread, your similar questions, and in fact, most of your emails—until now.
When you write ‘Why can’t X’ as you often do, I have presumed that this was a casual and imprecise form of expressing the question “what is the reason why a different path was chosen.” Now, based on this, I see it differently. I think you mean it literally. I imagine myself walking beside you and you saying, “Why can’t trees be blue? Why can’t dogs have square heads? Why can’t cars have windows in the tires? Why don’t cats and pigs swim?” Which is to say, it seems that you are writing email as a way to share a stream of consciousness that every aspect of reality—every choice, decision, evolutionary outcome, taste, desire—could in fact be different in some other place or time or circumstance.
Now that the question unfolds as “Why is it not possible that X” (as you wrote from the start) then the answer to your emails is, “It is possible,” possible as in, “in a different language, with a different goal, on a hypothetical computer, if the most common programmer errors were not as they are, etc.” In engineering, nearly everything is possible. Not all of the possibilities are good, wise, or desirable though, so I think many here thought these other attributes were implied as part of your question. I did before but now do not.
In your spirit of “Why can’t” as in “prove that it is impossible” (of course such proofs are impossible), I offer examples of the uncommon but possible.
A different question, “Why is X disallowed in Go,” is actually much more educational. The example below, that was given you by amkguo, is the example that I thought of right at first—not about “why can’t bytes be interchangeable” but “why are Types treated as separate species.” This is a great topic. Because of huge human experience that mixing types by concept (adding two kinds of temperature or mass) results in unfortunate outcomes (many!) and similar experience in debugging as the result of many languages, C and C++ among them, doing natural-seeming invisible coercion between types. Type coercion in specific and invisible actions generally are a trouble area in large-scale software development. This is why the Go team (superstars with UNIX, C, major Gnu C/C++ implementation, Plan 9, Inferno, and maybe more importantly, careful study of common software development problems by 10,000+ programmers Google) disallowed it during the design of Go and instead require explicit casts (as in “var a int = 3; b := float64(a)”), why they treat Types as non-mixing species, and why the affordances around this are very limited.
I gave a talk in Madrid about Go as a software engineering approach assisted by a computer language. This seems a nice educational way to avoid many of the “but C allows …” or “it should be technically possible to…” confusions.
Michael
package mainimport ("fmt")func main() {var (x, y, z *string = new(string), new(string), new(string)b []*string)str := "please parse this"b = make([](*string), 3)fmt.Sscan(str, x, y, z)fmt.Sscan(str, b...) //doesn't workfmt.Println("x", *x, "y", *y, "z", *z)fmt.Println("b", b)}