monadix
unread,Aug 20, 2025, 2:55:34 PMAug 20Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
As far as I know all the implementations of go do monomorphization of generic parameters at least by the memory layout of used types. This means that while not everything is known about the type at compile-time, at least it's size surely is. And we easily can create new types containing generic fields like that:
```go
type Foo[T any] struct {
foo T
}
```
which means Foo size in bytes depends on the size of generic parameter T.
Also since some builtin functions are usable in constant context, we can actually create static arrays of size of some particular type resolved in compile-time like that:
```go
type Bar struct {
bar [unsafe.Sizeof(uint64(0))]bytes
}
```
But we cannot define types, which memory layout depends on the generic type parameter in more complex ways (but still using only constant functions) like that:
```go
type Baz[T any] struct {
baz [unsafe.Sizeof(*new(T))]byte
}
```
because:
```
array length unsafe.Sizeof(*new(T)) (value of type uintptr) must be constant
```
But isn't it already? Sure it depends on the particular T passed to the type, but it will be monomorphized by size anyway and (if compiled) would have exactly the same memory layout as Foo[T].
Why exactly isn't it possible in the current (I use go 1.24.5) compiler? Is it simply unimplemented, impossible for some unknown to me reason (would like to read the actual explaination) or undesirable and forbidden (also, why?)?