ArbitraryType and its existence

// ArbitraryType is here for the purposes of documentation only and is not actually
// part of the unsafe package. It represents the type of an arbitrary Go expression.
type ArbitraryType int

I see there is a type declaration for ArbitraryType in the unsafe package, yet the comment reads as …and is not actually part of the unsafe package…

And we also have a type Pointer which seem to be a type alias of ArbitraryType which itself looks like a type alias of int, so what does ArbitraryType actually represent, its purpose and meaning? Thanks.

It’s a placeholder. unsafe.Pointer's definition is:

type Pointer *ArbitraryType

Because it’s a pointer to an arbitrary type, not an arbitrary type itself. ArbitraryType could have been defined as T and then Pointer as *T, but then people would have asked “what’s ‘T’?”

They wanted to show something there to show that the unsafe.Pointer type is a pointer type and not some opaque type like string, a slice, map, etc…

Thanks, so when someone does a conversion like Pointer(&objectOfSomeType) will it get casted to int* / *int?

As I see…

type Pointer which seem to be a type alias of ArbitraryType which itself looks like a type alias of int

Another question, why not just type Pointer *int?

No. ArbitraryType, like the documentation says, is just there for documentation purposes. In the documentation, they could have said:

type ArbitraryType *byte

or

type ArbitraryType *bool

etc. unsafe.Pointer is only defined as type unsafe.Pointer *ArbitraryType in the documentation. It’s dereferenced type isn’t actually ArbitraryType.

The thing with every pointer type except for unsafe.Pointer, is that they have an underlying type. If you have a *string, you can dereference it and get a string, same with *int -> int. unsafe.Pointer isn’t like that. It’s “unsafe.” You cannot dereference it because type type system has no idea what’s on the other side. Maybe it’s a pointer to a string, maybe to an int, maybe a [0]byte, etc…

It’s similar to interface{} in that the type system doesn’t “know” what’s in it. The difference is an interface{} includes runtime information so that at runtime, you can deduce what value is contained in the interface{} with a type assertion. unsafe.Pointer is just a pointer to *something* in memory and does not include that runtime type information, so you can’t type assert unsafe.Pointers.