Last active
May 25, 2024 10:18
-
-
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/43930f5c1bd904cf66423127a010221a2fe2222c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.2" | |
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