Skip to content

Instantly share code, notes, and snippets.

@SteveTrewick
Created January 6, 2025 21:02
Show Gist options
  • Select an option

  • Save SteveTrewick/0c3d0fb0eab403e965c74ebb9cf5f1a5 to your computer and use it in GitHub Desktop.

Select an option

Save SteveTrewick/0c3d0fb0eab403e965c74ebb9cf5f1a5 to your computer and use it in GitHub Desktop.
stored a metatype and need to get the MemoryLayout, etc? Yeah, me too.
// if we have a type param, can we get the bits we need out of it,
// like length?
let xtype = UInt16.self
// MemoryLayout<xtype> etc, no no no, so ...
func sizeof<T>(_ value: T.Type) -> Int {
MemoryLayout<T>.size
}
let sz = sizeof(xtype)
print(sz) // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment