Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Macro: is there a way to reify a spliced function?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Léonard Schneider  
View profile  
 More options Nov 14 2012, 7:36 pm
From: Léonard Schneider <leonard.schnei...@gmail.com>
Date: Wed, 14 Nov 2012 16:36:15 -0800 (PST)
Local: Wed, Nov 14 2012 7:36 pm
Subject: Macro: is there a way to reify a spliced function?

Hi,

I need to reify an expression of a spliced function. Unfortunately, reify
does not work as expected when you append a wildcard. Instead of selecting
the function which is being spliced it acts as if the spliced function were
a value.

An example to illustrate the issue.

Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM,
Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.reflect.runtime.{universe=>ru}
import scala.reflect.runtime.{universe=>ru}
scala> def f(x: Int) = 0
f: (x: Int)Int
scala> val g = ru.reify(f _)
g: reflect.runtime.universe.Expr[Int => Int] =
Expr[Int => Int]({
  ((x) => f(x))

})

scala> ru.reify(g.splice _)
res0: reflect.runtime.universe.Expr[() => Int => Int] =
Expr[() => Int => Int]((() => {
  ((x) => f(x))

}))

I would have expected an Expr[Int => Int]. Is this a bug? Is there a
workaround?

Best regards,

Leo


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eugene Burmako  
View profile  
 More options Nov 15 2012, 4:38 am
From: Eugene Burmako <xeno...@gmail.com>
Date: Thu, 15 Nov 2012 01:38:22 -0800 (PST)
Local: Thurs, Nov 15 2012 4:38 am
Subject: Re: Macro: is there a way to reify a spliced function?
reify typechecks its arguments before doing any processing, so it
behaves exactly like normal Scala does:

scala> val g1 = f _
g1: Int => Int = <function1>

scala> val g2 = g1 _
g2: () => Int => Int = <function0>

Why it behaves like that is another question.

On Nov 15, 1:36 am, Léonard Schneider <leonard.schnei...@gmail.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave  
View profile  
 More options Nov 15 2012, 6:08 am
From: Dave <dave.mahabiers...@hotmail.com>
Date: Thu, 15 Nov 2012 03:08:08 -0800 (PST)
Local: Thurs, Nov 15 2012 6:08 am
Subject: Re: Macro: is there a way to reify a spliced function?

Maybe with parentheses to force apply after lifting otherwise it means only
lifting.
Don't know if this is intended.

scala> val g2 = g1 _
g2: () => Int => Int = <function0>

scala> val g2 = g1(_)
g2: Int => Int = <function1>

scala> ru.reify(g.splice _)
res1: reflect.runtime.universe.Expr[() => Int => Int] =
Expr[() => Int => Int]((() => {
  ((x) => f(x))

}))

scala> ru.reify(g.splice(_))
res0: reflect.runtime.universe.Expr[Int => Int] =
Expr[Int => Int](((x$1) => {
  ((x) => f(x))

}.apply(x$1)))

Op donderdag 15 november 2012 10:38:29 UTC+1 schreef Eugene Burmako het
volgende:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave  
View profile  
 More options Nov 15 2012, 6:14 am
From: Dave <dave.mahabiers...@hotmail.com>
Date: Thu, 15 Nov 2012 03:14:01 -0800 (PST)
Local: Thurs, Nov 15 2012 6:14 am
Subject: Re: Macro: is there a way to reify a spliced function?

Or

scala> ru.reify((g.splice _).apply)
res5: reflect.runtime.universe.Expr[Int => Int] =
Expr[Int => Int]((() => {
  ((x) => f(x))

}).apply())

Op donderdag 15 november 2012 12:08:08 UTC+1 schreef Dave het volgende:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Léonard Schneider  
View profile  
 More options Nov 15 2012, 6:46 am
From: Léonard Schneider <leonard.schnei...@gmail.com>
Date: Thu, 15 Nov 2012 03:46:17 -0800 (PST)
Local: Thurs, Nov 15 2012 6:46 am
Subject: Re: Macro: is there a way to reify a spliced function?

Hi Dave,

I've tried that but it failed for my use case. So let me describe the more
general problem I'm trying to solve. Any idea or hint is welcome.

// Challenge: write macro getf such as "def g = getf[A.type]" == "def g =
A.f _" for any A object
// which contains a f method which can have any type signature
// This is just an example of an A object containing an f method
// object A {
//  def f(x: Int, s: String)
// }

def getf[T] = macro getfImpl[T]

def getfImpl[T: c.WeakTypeTag](c: Context) = {
  import c.universe._

  ???


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Léonard Schneider  
View profile  
 More options Nov 15 2012, 12:53 pm
From: Léonard Schneider <leonard.schnei...@gmail.com>
Date: Thu, 15 Nov 2012 09:53:36 -0800 (PST)
Local: Thurs, Nov 15 2012 12:53 pm
Subject: Re: Macro: is there a way to reify a spliced function?

So here is a failed attempt to fix ideas. It shows why I've been trying to
use the splice and wildcard combination in a reify. I think my initial
question was a bad example to that regard.

scala> :pas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Léonard Schneider  
View profile  
 More options Nov 16 2012, 6:51 pm
From: Léonard Schneider <leonard.schnei...@gmail.com>
Date: Fri, 16 Nov 2012 15:51:31 -0800 (PST)
Local: Fri, Nov 16 2012 6:51 pm
Subject: Re: Macro: is there a way to reify a spliced function?

OK. Answering myself. Here is what I came up with. It would still need to
be made more robust by dealing with nullary methods for instance. In a few
words, you have to redefine a function which takes the method values, as a
lambda expression would, to be able to use it later on. I should be able to
use the trick for automatically transform case classes into HList in my
implementation ;)

object A {
 def f(x: Int, s: String) = 0

}

def getf[T]: Any = macro getfImpl[T]
def getfImpl[T: c.WeakTypeTag](c: Context) = {
  import c.universe._
  val fSym = weakTypeOf[T].member(newTermName("f")).asMethod
  val argSyms = fSym.paramss.flatten
  val funTree = Function(argSyms.map(ValDef(_)),
treeBuild.mkMethodCall(fSym, argSyms.map(s =>
    treeBuild.mkAttributedIdent(s)
  )))
  c.Expr(funTree)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave  
View profile  
 More options Nov 17 2012, 6:38 am
From: Dave <dave.mahabiers...@hotmail.com>
Date: Sat, 17 Nov 2012 03:38:53 -0800 (PST)
Local: Sat, Nov 17 2012 6:38 am
Subject: Re: Macro: is there a way to reify a spliced function?

Amazing. Well done!

C:\Users\Dave>scala

Op zaterdag 17 november 2012 00:51:31 UTC+1 schreef Léonard Schneider het
volgende:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »