type_alias.mx raw

   1  // `type A = B` is an alias: A and B are the same type object, so a value of
   2  // one assigns to the other and *A matches *B. stage4 built a new Named whose
   3  // underlying was B, which made them distinct and rejected `var c B = a`.
   4  // Aliases to another package's Named type keep the Named shape (the
   5  // export/import path rebuilds those as a clone carrying the target's methods),
   6  // so this covers the same-package cases.
   7  package main
   8  
   9  type B struct{ n int32 }
  10  type A = B
  11  type AliasSlice = []int32
  12  type AliasInt = int32
  13  type AliasPtr = *B
  14  
  15  func takesB(b *B) (r int32) { return b.n }
  16  
  17  func main() {
  18  	a := A{n: 3}
  19  	var c B = a
  20  	p := &c
  21  	var q *A = p
  22  	if takesB(q) != 3 {
  23  		panic("alias pointer did not match its target")
  24  	}
  25  
  26  	var s AliasSlice = []int32{:2}
  27  	s[0] = 7
  28  	if s[0] != 7 {
  29  		panic("alias slice element wrong")
  30  	}
  31  
  32  	var i AliasInt = 9
  33  	var j int32 = i
  34  	if j+c.n != 12 {
  35  		panic("alias int arithmetic wrong")
  36  	}
  37  	println(j + c.n)
  38  }
  39