程序如何在基元静态类型化语言的运行时知道类型

问题描述:

Both Go and Scala provide ways of checking types at runtime:

Scala:

class A
class B extends A

val a: A = new A // static type: A dynamic type: A
val ab: A = new B // static type: A dynamic type: B
val b: B = new B // static type: B dynamic type: B

def thing(x: Any): String = x match {
    case t: Int => "Int"
    case t: A => "A"
    case t: B => "B"
    case t: String => "String"
    case _ => "unknown type"
}

Go:

package main

import (
    "fmt"
    "reflect"
)

struct A {
    field1 int
    field2 string
}

func printTypeOf(t interface{}) {
    fmt.Println(reflect.TypeOf(t))
}

func main() {
    i := 234234 // int
    s := "hello world" // string
    p := &A{3234234, "stuff"} // *A

    fmt.Print("The type of i is: ")
    printTypeOf(i)
    fmt.Print("The type of s is: ")
    printTypeOf(s)
    fmt.Print("The type of p is: ") // pass a pointer
    printTypeOf(p)
    fmt.Print("The type of the reference of p is: ") // pass a value
    printTypeOf(*p)
}

How exactly does this work internally? I presume for structs and classes, the type of the object is stored in a hidden field (so the structure in golang is really struct { field1 int field2 string type type }. But how on earth can function be given 11010110 and know whether it's a pointer to memory address at 214, the integer 214 or the character Ö? Are all values secretly passed with a byte which represents its type?

In Scala, each object has a pointer to its class. When you call thing with a primitive argument (Int, Char, etc.), it's automatically boxed to an object (java.lang.Integer, java.lang.Character, etc.). The match against Int in your code is actually translated to a match against Integer.

In Go, the type isn't stored in structs, but in interface values:

Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data. Assigning b to an interface value of type Stringer sets both words of the interface value.

So when you call printTypeOf(whatever), whatever is converted to interface {} and its type is stored in this new value along with whatever itself.