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

[coding style] object+apply vs def

2 replies
david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.

May be stupid question

I saw the following code,

class A {
object Foo {
def apply(name: String, params: List[String]): Bar = new Bar {
def xxx = name + ":" + params.mkString
}
}
}

I would like to understand why using it, instead of :

class A {
def Foo(name: String, params: List[String]): Bar = new Bar {
def xxx = name + ":" + params.mkString
}
}

What is the difference ?

/davidB

mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.
Re: [coding style] object+apply vs def


On Sun, Mar 8, 2009 at 6:17 PM, David Bernard <david.bernard.31@gmail.com> wrote:
May be stupid question

I saw the following code,

class A {
 object Foo {
   def apply(name: String, params: List[String]): Bar =  new Bar {
     def xxx = name + ":" + params.mkString
   }
 }
}

scala> a.Foo("abc",List("a", "b"))
res12: Bar = A$Foo$$anon$1@1420fea

scala> b.Foo("abc",List("a", "b"))
res13: Bar = A$Foo$$anon$1@1e91485

Also you can't access xxx (unless it's defined in Bar).

One thing is that you can use object Foo as a type and pass it around


I would like to understand why using it, instead of :

class A {
 def Foo(name: String, params: List[String]): Bar =  new Bar {
   def xxx = name + ":" + params.mkString
 }
}

scala> c.Foo("abc",List("a", "b"))
res16: Bar = A$$anon$1@4adb34

scala> d.Foo("abc",List("a", "b"))
res17: Bar = A$$anon$1@1906773

Not that the type is one level shallowers.
 

What is the difference ?

Good question. I wonder if there is a performance difference.

 My understanding it that in the first case, the first time you call 'Foo.apply' a new Foo object is create, and then the function called. This would mean the first definition of A is bigger and creates a new object. However the object can be passed to other functions.

Thomas

pi song
Joined: 2009-02-20,
User offline. Last seen 42 years 45 weeks ago.
Re: [coding style] object+apply vs def

wait... I think you can declare as function and pass it around as
well. Something like this:-

val succ = (x: Int) => x + 1

I believe both cases in the example are the same internally.

Fix me if I'm wrong please.

Pi Song

On Mon, Mar 9, 2009 at 9:05 AM, Thomas Sant Ana wrote:
>
>
> On Sun, Mar 8, 2009 at 6:17 PM, David Bernard
> wrote:
>>
>> May be stupid question
>>
>> I saw the following code,
>>
>> class A {
>>  object Foo {
>>    def apply(name: String, params: List[String]): Bar =  new Bar {
>>      def xxx = name + ":" + params.mkString
>>    }
>>  }
>> }
>
> scala> a.Foo("abc",List("a", "b"))
> res12: Bar = A$Foo$$anon$1@1420fea
>
> scala> b.Foo("abc",List("a", "b"))
> res13: Bar = A$Foo$$anon$1@1e91485
>
> Also you can't access xxx (unless it's defined in Bar).
>
> One thing is that you can use object Foo as a type and pass it around
>
>>
>> I would like to understand why using it, instead of :
>>
>> class A {
>>  def Foo(name: String, params: List[String]): Bar =  new Bar {
>>    def xxx = name + ":" + params.mkString
>>  }
>> }
>
> scala> c.Foo("abc",List("a", "b"))
> res16: Bar = A$$anon$1@4adb34
>
> scala> d.Foo("abc",List("a", "b"))
> res17: Bar = A$$anon$1@1906773
>
> Not that the type is one level shallowers.
>
>>
>> What is the difference ?
>
> Good question. I wonder if there is a performance difference.
>
>  My understanding it that in the first case, the first time you call
> 'Foo.apply' a new Foo object is create, and then the function called. This
> would mean the first definition of A is bigger and creates a new object.
> However the object can be passed to other functions.
>
> Thomas
>
>

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