It is trivial to write a macro to convert bytes"hello"
into
"hello".getBytes
or Array('h'.getByte, 'o'.getByte, 'l'.getByte, 'l'.getByte, 'o'.getByte)
The problem here is we do not get the constant literal, i.e
"hello" eq "hello"
does not hold for the values constructed by the macros: bytes"hello" ne bytes"hello"
. There is also a performance drawback as the byte array is needlessly constructed many times.
What actually bytes"hello"
has to be converted is a global scope object:
object `bytes"hello"` {
val value = Array('h'.getByte, 'o'.getByte, 'l'.getByte, 'l'.getByte, 'o'.getByte)
}
But it seems that in its current state, Scala Macros does not allow creation of global objects. Am I wrong and this is doable in the current Scala ?