Silly question of the day: Is there a way in #scala3 to "unpack" a String at the type level ?
ie: val x: Unpack["abc"] = ('a', 'b', 'c')
-
-
Replying to @skaalf
Yes, you can match on the type parameter's TypeRepr as a ConstantType(StringConstant(str)) and then just work on the string.
1 reply 0 retweets 1 like -
Replying to @propensive
Ah I see. I can give it a try :) Any non-macro solution ?
1 reply 0 retweets 0 likes -
Replying to @skaalf
For some reason I read the word "macro" in your question, but maybe that's what happens when you've been doing Scala 3 macros all day...
2 replies 0 retweets 1 like -
Replying to @propensive @skaalf
I don't know of one, though. But the macro is very easy. Something like this: def unpack[T <: Singleton & String: Type](using Quotes): Expr[List[Char]] = import quotes.reflect._ TypeRepr.of[T] match case ConstantType(StringConstant(str)) => '{ str.toList } (untested) ;)
1 reply 0 retweets 2 likes -
Replying to @propensive @skaalf
Oh, and you'll also need the macro inline def unpack[T <: Singleton & String]: List[Char] = ${unpack[T]}
1 reply 0 retweets 0 likes -
Replying to @propensive
"access to value str from wrong staging level:" ^^
1 reply 0 retweets 0 likes -
Replying to @skaalf
Ah, you need to do `str.value.toList` outside the `'{}` and then put it inside.
2 replies 0 retweets 0 likes
Make that `str.value.get.toList`, probably. ;)
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.