Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 20, 2024 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/1b76c44def82ea3c390c83155e089166 to your computer and use it in GitHub Desktop.
Save dacr/1b76c44def82ea3c390c83155e089166 to your computer and use it in GitHub Desktop.
scala3 feature examples - macros - inline sizeOf / published by https://github.com/dacr/code-examples-manager #ec61ceb3-1593-4112-9aa9-6ba76fac74cf/c4cfabd54486af8dfc21303e78010db7614f623a
// summary : scala3 feature examples - macros - inline sizeOf
// keywords : scala3, tutorial, macros, inline, meta-programming, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : ec61ceb3-1593-4112-9aa9-6ba76fac74cf
// created-on : 2021-06-20T12:33:40+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// Inspired from https://www.youtube.com/watch?v=OPBuCQRgyV4&t=1283s from Josh Suereth
//> using scala "3.4.0"
import scala.compiletime.erasedValue
inline def sizeOf[T]: Int =
inline erasedValue[T] match
case _:Byte => 1
case _:Short => 2
case _:Int => 4
case _:Long => 8
case _:Float => 4
case _:Double => 8
@main def go() =
println(s"sizeOf[Int] = ${sizeOf[Int]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment