- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[coding style] object+apply vs def
Sun, 2009-03-08, 22:17
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
Mon, 2009-03-09, 02:17
#2
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
>
>
On Sun, Mar 8, 2009 at 6:17 PM, David Bernard <david.bernard.31@gmail.com> wrote:
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
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.
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