Scala Metaprogramming

588 Members
4 Servers

Load older messages

25 Sep 2022
@_discord_925474730230812743:t2bot.iomasonedmison#5794 * where MyShow is
trait MyShow[T]:
  def show(value: T): String
object MyShow:
  def apply[T](using ev: MyShow[T]) = ev

  given MyShow[String] with
    def show(value: String): String =
  // <elided>

with instances provided for primitive types and a simple Main application of
object Main extends App {
  val showRes = new Macro().anyShow("hello", true)

produces a compile time error of
no implicit values were found that match type MyShow[?][("hello" : String)]
@_discord_766348036560912444:t2bot.ioMike (Performant Data)#4533 joined the room.19:20:44
@_discord_766348036560912444:t2bot.ioMike (Performant Data)#4533 I'm trying to use Shapeless to generate, for each ADT, an implementation of a typeclass trait that has a method that behaves like the default toString that you'd find on the ADT. So for a case class instance Foo("bar") I'd expect the method to return "Foo(bar)".

I have code that returns the "(bar)" part. I've been through the Type Astronaut’s Guide and seen how FieldType is used, but I'm puzzled at how to get at the string name of the class. Some of these case classes may be value types, i.e., extends AnyVal.

I'm currently attempting to use TypeTags, thinking that may solve it. In progress, re-learning about it still.
26 Sep 2022
@_discord_530372354426011651:t2bot.iothecoda#7033 I'm really struggling here. I'm in a macro, I have a product type T extends AnyVal which means I can't use a product mirror 08:17:58
@_discord_530372354426011651:t2bot.iothecoda#7033 I can get symbols for the case class, for its companion, for the apply method on the companion. I have the Type of the parameter to that method 08:19:38
@_discord_530372354426011651:t2bot.iothecoda#7033 What I can't figure is how to generated the quoted code to actually invoke the method 08:20:17
@_discord_228527523976314881:t2bot.ioarainko#6079 thecoda Maybe this will come in handy: https://github.com/lampepfl/dotty-macro-examples/tree/main/accessMembersByName 09:00:10
@_discord_530372354426011651:t2bot.iothecoda#7033 Doesn't help, I want to invoke a method and pass an argument 09:18:54
@_discord_228527523976314881:t2bot.ioarainko#6079 Then you can do something like Select.unique(methodName, objectToInvokeOn).appliedTo(<argsAsTerms here>) 09:44:41
@_discord_228527523976314881:t2bot.ioarainko#6079 Or appliedToArgs, I don't remember the exact method name 09:46:47
@_discord_530372354426011651:t2bot.iothecoda#7033 Tried that 10:07:48
def myMethod(x: Underlying): P =
    val arg = '{x}.asTerm
      Select.unique(Ident(companionSym.termRef), "apply"),
      arg :: Nil
@_discord_530372354426011651:t2bot.iothecoda#7033 where P <: AnyVal and Underlying is the wrapped type. companionSym is the companion module to P 10:09:33
@_discord_530372354426011651:t2bot.iothecoda#7033 Gives me: 10:09:53
[error] 281 |                            val arg = '{x}.asTerm
[error]     |                                        ^
[error]     |                        access to $anon.this from wrong staging level:
[error]     |                         - the definition is at level 1,
[error]     |                         - but the access is at level 0.
[error] one error found
@_discord_530372354426011651:t2bot.iothecoda#7033 The problem is that every way I can see to do this wants a Term, but I can't get one from the argument without error 10:14:29
@_discord_192322936219238400:t2bot.ioAly I believe what you're supposed to do is make myMethod an inline def, and maybe inline x: Underlying? But I've not worked extensively with S3 macros, so I could be entirely wrong 10:15:57
@_discord_530372354426011651:t2bot.iothecoda#7033 Oh, this is subtle 10:16:47
@_discord_530372354426011651:t2bot.iothecoda#7033 The problem was x: Underlying 10:17:02
@_discord_530372354426011651:t2bot.iothecoda#7033 Where type Underlying = ... was also defined in the code emitted by the macro 10:17:39
@_discord_530372354426011651:t2bot.iothecoda#7033 It's not the value that was at the wrong level, it was the value's type 10:17:57
@_discord_192322936219238400:t2bot.ioAly Hmm, I wonder if the error saying $anon.this instead of x would be the clue there 10:18:41
@_discord_192322936219238400:t2bot.ioAly What's that ...? 10:18:52
@_discord_530372354426011651:t2bot.iothecoda#7033 The clue is obvious when you know to look for it 10:30:15
@_discord_840225175391698974:t2bot.ioJasper-M#0182 joined the room.15:02:49
@_discord_840225175391698974:t2bot.ioJasper-M#0182 Why is the following illegal? You get the error Type splicing with $ in quotes not supported anymore.
val x = someTypeTree.tpe.asType
'{ new Foo { type Bar = $x } }

While you can get exactly the same effect with this more convoluted code
someTypeTree.tpe.asType match
  case '[x] => '{ new Foo { type Bar = x } }
@_discord_512254403080617985:t2bot.ioshardulc joined the room.15:20:21
27 Sep 2022
@_discord_766348036560912444:t2bot.ioMike (Performant Data)#4533 I posted the current state of affairs to https://github.com/performantdata/lib-sanitize if anyone's interested in taking a look. 04:48:43
@_discord_547365865969025029:t2bot.iomaster_as joined the room.12:11:18
28 Sep 2022
@_discord_488387072378273812:t2bot.iojorge0998 joined the room.03:43:56

There are no newer messages yet.

Back to Room List