This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Creating a DSL in scala

5 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Dear All,I need to create the following construct for scala:
   val richSession = RichSession(session)
   richSession transactionally merge item;


I have coded the following, which doesn't work clearly. 
class RichSession(val session:Session) {  def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){     val tx: Transaction = session.beginTransaction      f(session,target);      tx.commit  }
  def merge() = new { def apply(session:Session, item:AnyRef):Unit = session.merge(item)}

}
 object RichSession {   def apply(session:Session) = new RichSession(session); }
Can you help me understanding where I am wrong?
Thank you very much
Edmondo
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Fwd: Creating a DSL in scala
To continue, I knew how to do it this way:

class RichSession(val session:Session) {  def transactionally() =    new {      private def apply(f:(Session, AnyRef)=>Unit, target:AnyRef) = {         val tx: Transaction = session.beginTransaction        f(session,target);        tx.commit      }      def merge(target:AnyRef) = apply((session,target)=>session.merge(target),target);
    };

}
 object RichSession {   def apply(session:Session) = new RichSession(session); }

But this would imply creating an object at each time the transactionally is called. Is there a better way of doing this?
Best Regards 

---------- Forwarded message ----------
From: Edmondo Porcu <edmondo.porcu@gmail.com>
Date: 2011/12/14
Subject: Creating a DSL in scala
To: scala-user <scala-user@googlegroups.com>


Dear All,I need to create the following construct for scala:
   val richSession = RichSession(session)
   richSession transactionally merge item;


I have coded the following, which doesn't work clearly. 
class RichSession(val session:Session) {  def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){     val tx: Transaction = session.beginTransaction      f(session,target);      tx.commit  }
  def merge() = new { def apply(session:Session, item:AnyRef):Unit = session.merge(item)}

}
 object RichSession {   def apply(session:Session) = new RichSession(session); }
Can you help me understanding where I am wrong?
Thank you very much
Edmondo
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Creating a DSL in scala

the return type of "transactionally" doesn't have a method called "merge"
"merge" is inside richsession

-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 13:37:41 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Creating a DSL in scala

> Dear All,
> I need to create the following construct for scala:
>
> val richSession = RichSession(session)
>
> richSession transactionally merge item;
>
>
>
> I have coded the following, which doesn't work clearly.
>
> class RichSession(val session:Session) {
> def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){
> val tx: Transaction = session.beginTransaction
> f(session,target);
> tx.commit
> }
>
> def merge() = new { def apply(session:Session, item:AnyRef):Unit =
> session.merge(item)}
>
>
> }
>
> object RichSession {
> def apply(session:Session) = new RichSession(session);
> }
>
> Can you help me understanding where I am wrong?
>
> Thank you very much
>
> Edmondo

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Re: Creating a DSL in scala
You are right, but the second working implementation 
class RichSession(val session:Session) {  def transactionally =    new {      private def apply(f:(Session, AnyRef)=>Unit)(target:AnyRef) = {         val tx: Transaction = session.beginTransaction        f(session,target);        tx.commit      }      def merge(target:AnyRef) = apply((session,target)=>session.merge(target))(target);
    };
}
would force me to call richSession.transactionally merge item
Is there anything I can do to avoid this?
Best Regards

2011/12/14 Dennis Haupt <h-star@gmx.de>
the return type of "transactionally" doesn't have a method called "merge"
"merge" is inside richsession

-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 13:37:41 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Creating a DSL in scala

> Dear All,
> I need to create the following construct for scala:
>
>    val richSession = RichSession(session)
>
>    richSession transactionally merge item;
>
>
>
> I have coded the following, which doesn't work clearly.
>
> class RichSession(val session:Session) {
>   def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){
>     val tx: Transaction = session.beginTransaction
>       f(session,target);
>       tx.commit
>   }
>
>   def merge() = new { def apply(session:Session, item:AnyRef):Unit =
> session.merge(item)}
>
>
> }
>
>  object RichSession {
>    def apply(session:Session) = new RichSession(session);
>  }
>
> Can you help me understanding where I am wrong?
>
> Thank you very much
>
> Edmondo

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Creating a DSL in scala

you need some dots or braces for chained expressions

-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 14:04:10 +0100
> Von: Edmondo Porcu
> An: Dennis Haupt
> CC: scala-user@googlegroups.com
> Betreff: Re: [scala-user] Creating a DSL in scala

> You are right, but the second working implementation
>
> class RichSession(val session:Session) {
> def transactionally =
> new {
> private def apply(f:(Session, AnyRef)=>Unit)(target:AnyRef) = {
> val tx: Transaction = session.beginTransaction
> f(session,target);
> tx.commit
> }
> def merge(target:AnyRef) =
> apply((session,target)=>session.merge(target))(target);
>
> };
>
> }
>
> would force me to call richSession.transactionally merge item
>
> Is there anything I can do to avoid this?
>
> Best Regards
>
>
> 2011/12/14 Dennis Haupt
>
> > the return type of "transactionally" doesn't have a method called
> "merge"
> > "merge" is inside richsession
> >
> > -------- Original-Nachricht --------
> > > Datum: Wed, 14 Dec 2011 13:37:41 +0100
> > > Von: Edmondo Porcu
> > > An: scala-user
> > > Betreff: [scala-user] Creating a DSL in scala
> >
> > > Dear All,
> > > I need to create the following construct for scala:
> > >
> > > val richSession = RichSession(session)
> > >
> > > richSession transactionally merge item;
> > >
> > >
> > >
> > > I have coded the following, which doesn't work clearly.
> > >
> > > class RichSession(val session:Session) {
> > > def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){
> > > val tx: Transaction = session.beginTransaction
> > > f(session,target);
> > > tx.commit
> > > }
> > >
> > > def merge() = new { def apply(session:Session, item:AnyRef):Unit =
> > > session.merge(item)}
> > >
> > >
> > > }
> > >
> > > object RichSession {
> > > def apply(session:Session) = new RichSession(session);
> > > }
> > >
> > > Can you help me understanding where I am wrong?
> > >
> > > Thank you very much
> > >
> > > Edmondo
> >

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Creating a DSL in scala
Without any dots or braces, a b c d always means call method d on the result of passing value c to method b of value a; in other words, a.b(c).d. At the point in time that scalac does that translation, it doesn't know what a, b, c, or d actually are.


On Wednesday, December 14, 2011, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
> You are right, but the second working implementation 
> class RichSession(val session:Session) {
>   def transactionally =
>     new {
>       private def apply(f:(Session, AnyRef)=>Unit)(target:AnyRef) = {
>         val tx: Transaction = session.beginTransaction
>         f(session,target);
>         tx.commit
>       }
>       def merge(target:AnyRef) = apply((session,target)=>session.merge(target))(target);
>     };
> }
> would force me to call richSession.transactionally merge item
> Is there anything I can do to avoid this?
> Best Regards
>
> 2011/12/14 Dennis Haupt <h-star@gmx.de>
>>
>> the return type of "transactionally" doesn't have a method called "merge"
>> "merge" is inside richsession
>>
>> -------- Original-Nachricht --------
>> > Datum: Wed, 14 Dec 2011 13:37:41 +0100
>> > Von: Edmondo Porcu <edmondo.porcu@gmail.com>
>> > An: scala-user <scala-user@googlegroups.com>
>> > Betreff: [scala-user] Creating a DSL in scala
>>
>> > Dear All,
>> > I need to create the following construct for scala:
>> >
>> >    val richSession = RichSession(session)
>> >
>> >    richSession transactionally merge item;
>> >
>> >
>> >
>> > I have coded the following, which doesn't work clearly.
>> >
>> > class RichSession(val session:Session) {
>> >   def transactionally(f:(Session, AnyRef)=>Unit)(target:AnyRef){
>> >     val tx: Transaction = session.beginTransaction
>> >       f(session,target);
>> >       tx.commit
>> >   }
>> >
>> >   def merge() = new { def apply(session:Session, item:AnyRef):Unit =
>> > session.merge(item)}
>> >
>> >
>> > }
>> >
>> >  object RichSession {
>> >    def apply(session:Session) = new RichSession(session);
>> >  }
>> >
>> > Can you help me understanding where I am wrong?
>> >
>> > Thank you very much
>> >
>> > Edmondo
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland