How can I get Scala Named and Default arguments to work with macros

eirirlar

Given scala version 2.11.7 and this macro definition:

import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
package object macrotest {
  def namedMacro(c: Context)(a: c.Expr[Int]): c.Expr[Int] = {
    println("the int expr was " + a)
    a
  }
  def named(a: Int = 1) = macro namedMacro
}

And this invocation:

object NamedMacroTest {
  def main(args: Array[String]) {
    named()
    //named(a = 5) // or this
  }
}

Why do I see this error?

Error:(5, 10) macro applications do not support named and/or default arguments

And what can I do to not get the error, while still being able to call the macro with named and default arguments?

som-snytt

It was supposed to go back in, according to the 2.11 docs.

The original issue was addressed in this PR which details the challenges.

It was shot down finally at this attempt for technical reasons. They didn't like doing the desugaring and then losing what the original application looked like; and they didn't want to perform transforms and then have to undo it.

One idea is to let adaptation take over when the arg is omitted:

scala> :pa
// Entering paste mode (ctrl-D to finish)

def m[A: c.WeakTypeTag](c: Context)(i: c.Expr[A]): c.Expr[Int] = {
  import c.universe._
  if (i.tree.tpe <:< typeOf[Int]) i.asInstanceOf[c.Expr[Int]]
  else if (i.tree.tpe <:< typeOf[Unit]) c.Expr[Int](q"42")
  else c.abort(null,  "Nope") }

// Exiting paste mode, now interpreting.

m: [A](c: scala.reflect.macros.whitebox.Context)(i: c.Expr[A])(implicit evidence$1: c.WeakTypeTag[A])c.Expr[Int]

scala> def f[A](i: A): Int = macro m[A]
defined term macro f: [A](i: A)Int

scala> f(1)
res9: Int = 1

scala> f()
warning: there was one deprecation warning; re-run with -deprecation for details
res10: Int = 42

scala> f("")  // I don't remember where they keep NoPosition
java.lang.NullPointerException

It's easier just to do something different, like indirect through a method that takes an empty param list. Compare https://stackoverflow.com/a/25219644/1296806 and comments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the default values for named arguments for a method?

From Dev

How can I get scala to work in the command line?

From Dev

How can I set a default for star arguments?

From Dev

How can I set a default for star arguments?

From Dev

How can I get named parameters into a Hash?

From Dev

How can I reuse code between Javascript macros and minimize work done within the macros?

From Dev

how default arguments work in python

From Dev

how default arguments work in python

From Dev

scala macros how to get tree of specific class

From Dev

scala macros how to get tree of specific class

From Dev

Scala Named Arguments

From Dev

How can I get the default value for a type?

From Dev

How can I get the default maximumTrackTintColor for a UISlider

From Dev

How can I get the project path in Scala?

From Dev

How can I make Weld to distinguish [@Default@Any@Named] from [@Default@Any]?

From Java

How can I get the named parameters from a URL using Flask?

From Dev

Structuremap - how can I get a named instance and pass parameter at runtime?

From Dev

Can this be done with Scala macros?

From Dev

How can I splice in a type and a default value in Scala quasiquotes?

From Dev

How can I splice in a type and a default value in Scala quasiquotes?

From Dev

Scala Default Values: How do I get a default value from type Any in Scala while creating a LinkedList?

From Dev

How can I get SweetFX to work with RadeonPro?

From Dev

How can I get jshint to work?

From Dev

How can I get 'autodoc' work?

From Dev

How can I get join to work with conditions

From Dev

How can I get FacetFilter work properly?

From Dev

How can I get at to work on OSX?

From Dev

How can I get .htaccess to work?

From Dev

How can I get ddclient to work with freedns?

Related Related

  1. 1

    How can I get the default values for named arguments for a method?

  2. 2

    How can I get scala to work in the command line?

  3. 3

    How can I set a default for star arguments?

  4. 4

    How can I set a default for star arguments?

  5. 5

    How can I get named parameters into a Hash?

  6. 6

    How can I reuse code between Javascript macros and minimize work done within the macros?

  7. 7

    how default arguments work in python

  8. 8

    how default arguments work in python

  9. 9

    scala macros how to get tree of specific class

  10. 10

    scala macros how to get tree of specific class

  11. 11

    Scala Named Arguments

  12. 12

    How can I get the default value for a type?

  13. 13

    How can I get the default maximumTrackTintColor for a UISlider

  14. 14

    How can I get the project path in Scala?

  15. 15

    How can I make Weld to distinguish [@Default@Any@Named] from [@Default@Any]?

  16. 16

    How can I get the named parameters from a URL using Flask?

  17. 17

    Structuremap - how can I get a named instance and pass parameter at runtime?

  18. 18

    Can this be done with Scala macros?

  19. 19

    How can I splice in a type and a default value in Scala quasiquotes?

  20. 20

    How can I splice in a type and a default value in Scala quasiquotes?

  21. 21

    Scala Default Values: How do I get a default value from type Any in Scala while creating a LinkedList?

  22. 22

    How can I get SweetFX to work with RadeonPro?

  23. 23

    How can I get jshint to work?

  24. 24

    How can I get 'autodoc' work?

  25. 25

    How can I get join to work with conditions

  26. 26

    How can I get FacetFilter work properly?

  27. 27

    How can I get at to work on OSX?

  28. 28

    How can I get .htaccess to work?

  29. 29

    How can I get ddclient to work with freedns?

HotTag

Archive