declosurify repo

66 views
Skip to first unread message

Paul Phillips

unread,
Oct 11, 2012, 5:35:36 PM10/11/12
to Eugene Burmako, scala-i...@googlegroups.com
https://github.com/paulp/declosurify

Here is the part I like best, the entire foreach implementation:

  def foreachInfix[T: c0.WeakTypeTag, Coll: c0.WeakTypeTag](c0: CtxColl[T, Coll])(f0: c0.Expr[T => Unit]): c0.Expr[Unit] =
    mapInfix[T, Unit, Coll, Unit](c0)(f0)

The map macro distinguishes map from foreach based on whether the method is supposed to return Unit.  Sample:

    def mkLinear(prefixTree: Tree): c.Expr[That] = {
      val prefix = c.Expr[Lin[T]](prefixTree)
      val call   = mkCall('these dot 'head)

      reify {
        closureDef.splice
        builderVal.splice
        var these = prefix.splice
        while (!these.isEmpty) {
          call.splice
          these = these.tail
        }
        mkResult.splice
      }
    }

So "builderVal.splice" is a call to newBuilder for map, but () for foreach.  And etc.

Jason Zaugg

unread,
Oct 11, 2012, 5:47:18 PM10/11/12
to scala-i...@googlegroups.com
On Thu, Oct 11, 2012 at 11:35 PM, Paul Phillips <pa...@improving.org> wrote:
> Here is the part I like best, the entire foreach implementation:

> So "builderVal.splice" is a call to newBuilder for map, but () for foreach.
> And etc.

A macro implementation can also switch on `c.macroApplication.symbol`
as a stronger check for its intended purpose. Example [1].

-jason

[1] https://github.com/non/spire/commit/682220b

Paul Phillips

unread,
Oct 11, 2012, 5:50:29 PM10/11/12
to scala-i...@googlegroups.com


On Thu, Oct 11, 2012 at 2:47 PM, Jason Zaugg <jza...@gmail.com> wrote:
A macro implementation can also switch on `c.macroApplication.symbol`
as a stronger check for its intended purpose. Example [1].

That's good, I was just reinventing everything as I went.  Also I think last time I checked macroApplication was always null, but that was a while ago.  Glad to see I can work more with symbols.

So it is possible to do what I'm doing without the patch to trunk exposing the METHOD flag? I mean, I could cast my way to the method flag I guess, but pretending I wouldn't.

Jason Zaugg

unread,
Oct 11, 2012, 6:18:15 PM10/11/12
to scala-i...@googlegroups.com
I suppose copy/pasting (1 << 6) is also cheating.

I haven't tried this, but it seems you should be able to create a
symbol-free DefDef with:

scala> u.DefDef.apply()
<console>:9: error: not enough arguments for method apply:
(mods: u.Modifiers, name: u.Name, tparams: List[u.TypeDef],
vparamss: List[List[u.ValDef]], tpt: u.Tree, rhs: u.Tree)u.DefDef
in class DefDefExtractor.
Unspecified value parameters mods, name, tparams, ...
u.DefDef.apply()
^
... in the hope that the type checker would assign a the right sort of symbol.

-jason

Paul Phillips

unread,
Oct 11, 2012, 6:44:50 PM10/11/12
to scala-i...@googlegroups.com


On Thu, Oct 11, 2012 at 3:18 PM, Jason Zaugg <jza...@gmail.com> wrote:
... in the hope that the type checker would assign a the right sort of symbol.

First line of typedDefDef:

    def typedDefDef(ddef: DefDef): DefDef = {
      val meth = ddef.symbol.initialize

If you don't have a symbol already, you are toast.  Is there something other than c.typeCheck I should be calling? Because it's enterSym I need to obtain a symbol.
Reply all
Reply to author
Forward
0 new messages