Skip to content

proposal: all: Strict Type of Interface and unsafe Pointer #58346

@Pat3ickI

Description

@Pat3ickI

interface{} and unsafe.Pointer give some freedom to type system in Go but sometimes, some may want to limit the number of types interface{} or unsafe.Pointer can be declared or converted to at compile time, with //go: unsafe_typestrict and //go: interface_typestrict can solve it. This pragma can work on variables and function parameters. example:
//go: interface_typestrict <variable / parameter name> <accepted types>

type IntT int 

//go: interface_typestrict T int, int32, uint8, string
var T any = 1
var  T any =  IntT(1) // error
var T any =T typesvar T any = []byte(“ T types”) // error

unsafe: //go:unsafe_typestrict < var / param name> <accepted types>

type usp = unsafe.Pointer

var ut []uint8 = []uint8{97, 32, 84, 32, 116, 121, 112,   101}
var sut uint8 = ut[3]

//go:unsafe_typestrict T []uint8, uint8
var T usp = usp(&sut)
var T usp = usp(&ut)

these directives can also work on function parameters

type Another_Byte uint16

//go:interface_typestrict fn func(string, string) any, func(usp, any) unsafe.Pointer
//go:interface_typestrict args Another_Byte, uint8
func T_func( fn, args  any)  {} 

These can also allow underhand types like how it is when using Generics

//go:interface_typestrict <name> ~<type A>, ~<type B>

var T_var G = 200
type G int32

//go:unsafe_typestrict T ~int, ~int32, ~uint8
var T unsafe.Pointer
T = usp(&T_var)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions