Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Pat3ickI opened this issue Feb 6, 2023 · 1 comment
Closed

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

Pat3ickI opened this issue Feb 6, 2023 · 1 comment

Comments

@Pat3ickI
Copy link

Pat3ickI commented Feb 6, 2023

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)
@gopherbot gopherbot added this to the Proposal milestone Feb 6, 2023
@ianlancetaylor
Copy link
Member

Thanks, but closing as a dup of #19412, #54685, #57644. If we change the type system like this it should be in the language, not in comments.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2023
@golang golang locked and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants