Andy Coolware | 1 May 08:59 2012
Picon

Re: so what a (_:String).+ really is (as opposed to (_:String).isEmpty)

Hi,

Thanks for all the answers good and not so good :-P.

so we covered "text"+5 where apparently the build in operation (_:String).+( is being employed via StringBuilder.

Now what really happen, purely from Scala language semantics if we write 5+"text".  There is really no (_:Int).+ applicable here. What happens here? I also noticed that if defined implicit conversion String=>Int is not really involved even if it is available in the scope.

Thx,
Andy




Edmondo Porcu | 1 May 10:28 2012
Picon

Override method of self-type in trait

Dear all,
is it possible to have something like the following?

trait MyTrait {
   self:MyClass =>
   override def myMethod() {println(hello) ; super.myMethod()}
}

Best Regards
Edmondo

Alec Zorab | 1 May 10:42 2012

Re: so what a (_:String).+ really is (as opposed to (_:String).isEmpty)

From Predef:

implicit def any2stringadd(x: Any) = new runtime.StringAdd(x)

and then runtime.StringAdd is basically a wrapper for String.valueOf(x)

interestingly enough though, when you then actually look at the
bytecode (I didn't know you could do that on the console, it's
incredibly convenient), you'll see absolutely no trace of the
StringAdd thing anywhere. I'm not sure whether this means that the
StringAdd thing is a red herring, and the whole process is actually
special cased by the compiler, or whether the compiler is smart enough
to elide the unneeded implicit in this case. I suspect the former
though!

On 1 May 2012 07:59, Andy Coolware <andy.coolware@...> wrote:
> Hi,
>
> Thanks for all the answers good and not so good :-P.
>
> so we covered "text"+5 where apparently the build in operation (_:String).+(
> is being employed via StringBuilder.
>
> Now what really happen, purely from Scala language semantics if we write
> 5+"text".  There is really no (_:Int).+ applicable here. What happens here?
> I also noticed that if defined implicit conversion String=>Int is not really
> involved even if it is available in the scope.
>
> Thx,
> Andy
>
>
>
>

Miles Sabin | 1 May 12:08 2012

Re: Override method of self-type in trait

On Tue, May 1, 2012 at 9:28 AM, Edmondo Porcu
<edmondo.porcu@...> wrote:
> Dear all,
> is it possible to have something like the following?
>
> trait MyTrait {
>   self:MyClass =>
>   override def myMethod() {println(hello) ; super.myMethod()}
> }

Yes, if you extend MyClass rather than self typing to it, ie.,

Cheers,

Miles

--

-- 
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@...
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://underscoreconsulting.com
http://www.chuusai.com

Edmondo Porcu | 1 May 12:28 2012
Picon

Re: Override method of self-type in trait

I discovered you can also in this way

 trait MyTrait {
   self:MyClass =>
   override def myMethod() {println(hello) ; self.myMethod()}
 }

Best Regards
Edmondo

2012/5/1 Miles Sabin <miles@...>:
> On Tue, May 1, 2012 at 9:28 AM, Edmondo Porcu
<edmondo.porcu@...> wrote:
>> Dear all,
>> is it possible to have something like the following?
>>
>> trait MyTrait {
>>   self:MyClass =>
>>   override def myMethod() {println(hello) ; super.myMethod()}
>> }
>
> Yes, if you extend MyClass rather than self typing to it, ie.,
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel: +44 7813 944 528
> gtalk: miles@...
> skype: milessabin
> g+: http://www.milessabin.com
> http://twitter.com/milessabin
> http://underscoreconsulting.com
> http://www.chuusai.com

sveri | 1 May 12:32 2012

externalize method from companion objects in trait

Hi everybody,

i read this is the list for noobies and their questions, as i am one, and have a question, i hope this is the right place for me :D

I have two classes with their companion objects like this:

// class task
class Task extends Record[Long, Task] with IdentityGenerator[Long, Task] {     
  def PRIMARY_KEY = id
  def relation = Task
 
  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val name = "name".VARCHAR(255)
  val description = "description".TEXT

}

object Task extends Task with Table[Long, Task] {
  private val taskDbObj = this AS "taskDbObj" 
  def fetchAll = taskDbObj.map(taskDbObj => SELECT(taskDbObj.*).FROM(taskDbObj).ORDER_BY(taskDbObj.name ASC)).list
}

// class user
class User extends Record[Long, User] with IdentityGenerator[Long, User] {
  def PRIMARY_KEY = id
  def relation = User
 
  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val name = "name".VARCHAR(255)
  val prename = "prename".VARCHAR(55)

}

object User extends User with Table[Long, User] {
  private val userDbObj = this AS "userDbObj" 
  def fetchAll = userDbObj.map(userDbObj => SELECT(userDbObj.*).FROM(userDbObj).ORDER_BY(userDbObj.prename ASC)).list
}

(These classes extend the Record class from the circumflex-orm library)

As you can see, the companion objects are almost the same, they only differ in syntax and one logical point:
the order by column is a different one in each.

Now, regarding the DRY principle i'd like to put that method "fetchAll" in a superclass.
I read that the way to go are traits, so something like this should work i think:

trait StandardDatabaseAccessor {
 
  private val dbObj = this AS "dbObj" 
  def fetchAll = dbObj.map(dbObj => SELECT(dbObj.*).FROM(dbObj).ORDER_BY(dbObj.orderColumn ASC)).list
}

But this doesnt work, as 'this' is not known in the context of the trait.

However, i'd like to know if it is possible to share code lik e this in a trait at all, and if so, how i can do this.


Thanks in Advance,
Sven

Miles Sabin | 1 May 12:53 2012

Re: Override method of self-type in trait

On Tue, May 1, 2012 at 11:28 AM, Edmondo Porcu
<edmondo.porcu@...> wrote:
> I discovered you can also in this way
>
>  trait MyTrait {
>    self:MyClass =>
>    override def myMethod() {println(hello) ; self.myMethod()}
>  }

That compiles, but won't do what you want it to do (unless what you
want is unbounded recursion ;-)

Cheers,

Miles

--

-- 
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@...
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://underscoreconsulting.com
http://www.chuusai.com

Edmondo Porcu | 1 May 14:15 2012
Picon

Re: Override method of self-type in trait

Then I am in troubles, because extending MyClass is something I can't do.

Namely MyClass is AbstractSpringTransactionalTestNG something...the
base class you extend in spring when you want to do transactional
tests, and extending it directly messes up initialization order :(

What will happen when I call this.myMethod ?
Best

2012/5/1 Miles Sabin <miles@...>:
> On Tue, May 1, 2012 at 11:28 AM, Edmondo Porcu
<edmondo.porcu@...> wrote:
>> I discovered you can also in this way
>>
>>  trait MyTrait {
>>    self:MyClass =>
>>    override def myMethod() {println(hello) ; self.myMethod()}
>>  }
>
> That compiles, but won't do what you want it to do (unless what you
> want is unbounded recursion ;-)
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel: +44 7813 944 528
> gtalk: miles@...
> skype: milessabin
> g+: http://www.milessabin.com
> http://twitter.com/milessabin
> http://underscoreconsulting.com
> http://www.chuusai.com

Miles Sabin | 1 May 14:57 2012

Re: Override method of self-type in trait

On Tue, May 1, 2012 at 1:15 PM, Edmondo Porcu
<edmondo.porcu@...> wrote:
> Then I am in troubles, because extending MyClass is something I can't do.
>
> Namely MyClass is AbstractSpringTransactionalTestNG something...the
> base class you extend in spring when you want to do transactional
> tests, and extending it directly messes up initialization order :(

Remember that this is a trait extending that class. Initialization
order will be determined at the point at which the trait is mixed into
a concrete class.

> What will happen when I call this.myMethod ?

Try it: Boom!

Cheers,

Miles

--

-- 
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@...
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://underscoreconsulting.com
http://www.chuusai.com

Edmondo Porcu | 1 May 16:01 2012
Picon

Re: Override method of self-type in trait

Everything works fine, no boom ....

2012/5/1 Miles Sabin <miles@...>:
> On Tue, May 1, 2012 at 1:15 PM, Edmondo Porcu
<edmondo.porcu@...> wrote:
>> Then I am in troubles, because extending MyClass is something I can't do.
>>
>> Namely MyClass is AbstractSpringTransactionalTestNG something...the
>> base class you extend in spring when you want to do transactional
>> tests, and extending it directly messes up initialization order :(
>
> Remember that this is a trait extending that class. Initialization
> order will be determined at the point at which the trait is mixed into
> a concrete class.
>
>> What will happen when I call this.myMethod ?
>
> Try it: Boom!
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel: +44 7813 944 528
> gtalk: miles@...
> skype: milessabin
> g+: http://www.milessabin.com
> http://twitter.com/milessabin
> http://underscoreconsulting.com
> http://www.chuusai.com


Gmane