couple of questions

37 views
Skip to first unread message

Rintcius Blok

unread,
Mar 12, 2013, 6:20:49 AM3/12/13
to treehugg...@googlegroups.com
Hi,

I am trying to generate statements like:

  val s = Some("str")
  val n = None

But I cannot find it in the docs. How do I generate this?

I would also like to add a sequence of statements to a block.
Simple case:

    def aSeq(seq: Seq[String]): Seq[Tree] = {
      seq map { str => VAL(str) := LIT(str) }
    }

BLOCK(
  VAL("x") := LIT("x")
  // how to add this?
  // aSeq(Seq("a", "b"))
)

I'd like something like this:

BLOCK(
  VAL("x") := LIT("x"),
  BLOCK( aSeq(Seq("a", "b")))
)

but then generating:

    val x = "x"
    val a = "a"
    val b = "b"

instead of:

  val x = "x"
  {
    val a = "a"
    val b = "b"
  }

Thanks, Rintcius

Rintcius Blok

unread,
Mar 12, 2013, 7:17:02 PM3/12/13
to treehugg...@googlegroups.com
Never mind, I found this:

  val n = VAL("n") := NONE
  val s = VAL("s") := TYPE_REF("Some") APPLY LIT("str")

For the sequence I am now creating all Seq[Tree] entries in the method aSeq (i.e. moved VAL("x") := LIT("x") inside aSeq).

Rintcius

Eugene Yokota

unread,
Mar 13, 2013, 12:50:00 AM3/13/13
to treehugg...@googlegroups.com
Hi Rintcius,

Both Some and None are available as "collection constructors" - http://eed3si9n.com/treehugger/stdcollectionclass.html

    treehugger> console
    [info] Starting scala interpreter...
    [info] 
    import treehugger.forest._
    import definitions._
    import treehuggerDSL._

    scala> VAL("s") := SOME(LIT("str"))
    res1: treehugger.forest.ValDef = ValDef(Modifiers(, , Map()),Ident(s),Apply(Ident(Some),List(Literal(Constant(str)))))

    scala> treeToString(res1)
    res2: String = val s = Some("str")

    scala> VAL("n") := NONE
    res3: treehugger.forest.ValDef = ValDef(Modifiers(, , Map()),Ident(n),Ident(None))

    scala> treeToString(res3)
    res4: String = val n = None

As you might noticed, BLOCK(...) accepts both repeated parameters and Iterable[Tree]:

    scala> List(VAL("x") := LIT("x"), VAL("a") := LIT("a"), VAL("b") := LIT("b"))
    res5: List[treehugger.forest.ValDef] = List(ValDef(Modifiers(, , Map()),Ident(x),Literal(Constant(x))), ValDef(Modifiers(, , Map()),Ident(a),Literal(Constant(a))), ValDef(Modifiers(, , Map()),Ident(b),Literal(Constant(b))))

    scala> BLOCK(res5)
    res6: treehugger.forest.Block = Block(List(ValDef(Modifiers(, , Map()),Ident(x),Literal(Constant(x))), ValDef(Modifiers(, , Map()),Ident(a),Literal(Constant(a)))),ValDef(Modifiers(, , Map()),Ident(b),Literal(Constant(b))))

    scala> treeToString(res6)
    res7: String = 
    {
      val x = "x"
      val a = "a"
      val b = "b"
    }

    scala> BLOCK(res5: _*)
    res8: treehugger.forest.Block = Block(List(ValDef(Modifiers(, , Map()),Ident(x),Literal(Constant(x))), ValDef(Modifiers(, , Map()),Ident(a),Literal(Constant(a)))),ValDef(Modifiers(, , Map()),Ident(b),Literal(Constant(b))))

    scala> treeToString(res8)
    res9: String = 
    {
      val x = "x"
      val a = "a"
      val b = "b"
    }

-eugene

Rintcius Blok

unread,
Mar 13, 2013, 6:09:36 AM3/13/13
to treehugg...@googlegroups.com

Hi Eugene,

Thanks!

Regarding the BLOCK: I noticed indeed and used the Iterable[Tree] as a solution (see 2nd message in this thread) which works fine.

I missed the documentation of None and Some. Perfectly clear now!

Regards, Rintcius
Reply all
Reply to author
Forward
0 new messages