You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@spark.apache.org by tao zhan <zh...@gmail.com> on 2017/07/24 03:16:33 UTC

Question on Spark code

Hello,

I am new to scala and spark.
What does the "this.type" in set function for?


​
https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5e2596da1d600b9d0a/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala#L48

Thanks!

Zhan

Re: Question on Spark code

Posted by tao zhan <zh...@gmail.com>.
Get it, thank you!

On Mon, Jul 24, 2017 at 11:50 AM, Reynold Xin <rx...@databricks.com> wrote:

> This is a standard practice used for chaining, to support
>
> a.setStepSize(..)
>   .set setRegParam(...)
>
>
> On Sun, Jul 23, 2017 at 8:47 PM, tao zhan <zh...@gmail.com> wrote:
>
>> Thank you for replying.
>> But I do not get it completely, why does the "this.type“” necessary?
>> why could not it be like:
>>
>> def setStepSize(step: Double): Unit = {
>>     require(step > 0,
>>       s"Initial step size must be positive but got ${step}")
>>     this.stepSize = step
>> }
>>
>> On Mon, Jul 24, 2017 at 11:29 AM, M. Muvaffak ONUŞ <
>> onus.muvaffak@gmail.com> wrote:
>>
>>> Doesn't it mean the return type will be type of "this" class. So, it
>>> doesn't have to be this instance of the class but it has to be type of this
>>> instance of the class. When you have a stack of inheritance and call that
>>> function, it will return the same type with the level that you called it.
>>>
>>> On Sun, Jul 23, 2017 at 8:20 PM Reynold Xin <rx...@databricks.com> wrote:
>>>
>>>> It means the same object ("this") is returned.
>>>>
>>>> On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I am new to scala and spark.
>>>>> What does the "this.type" in set function for?
>>>>>
>>>>>
>>>>> ​
>>>>> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5
>>>>> e2596da1d600b9d0a/mllib/src/main/scala/org/apache/spark/mlli
>>>>> b/optimization/GradientDescent.scala#L48
>>>>>
>>>>> Thanks!
>>>>>
>>>>> Zhan
>>>>>
>>>>
>>>>
>>
>

Re: Question on Spark code

Posted by Reynold Xin <rx...@databricks.com>.
This is a standard practice used for chaining, to support

a.setStepSize(..)
  .set setRegParam(...)


On Sun, Jul 23, 2017 at 8:47 PM, tao zhan <zh...@gmail.com> wrote:

> Thank you for replying.
> But I do not get it completely, why does the "this.type“” necessary?
> why could not it be like:
>
> def setStepSize(step: Double): Unit = {
>     require(step > 0,
>       s"Initial step size must be positive but got ${step}")
>     this.stepSize = step
> }
>
> On Mon, Jul 24, 2017 at 11:29 AM, M. Muvaffak ONUŞ <
> onus.muvaffak@gmail.com> wrote:
>
>> Doesn't it mean the return type will be type of "this" class. So, it
>> doesn't have to be this instance of the class but it has to be type of this
>> instance of the class. When you have a stack of inheritance and call that
>> function, it will return the same type with the level that you called it.
>>
>> On Sun, Jul 23, 2017 at 8:20 PM Reynold Xin <rx...@databricks.com> wrote:
>>
>>> It means the same object ("this") is returned.
>>>
>>> On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I am new to scala and spark.
>>>> What does the "this.type" in set function for?
>>>>
>>>>
>>>> ​
>>>> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5
>>>> e2596da1d600b9d0a/mllib/src/main/scala/org/apache/spark/
>>>> mllib/optimization/GradientDescent.scala#L48
>>>>
>>>> Thanks!
>>>>
>>>> Zhan
>>>>
>>>
>>>
>

Re: Question on Spark code

Posted by Reynold Xin <rx...@databricks.com>.
This is a standard practice used for chaining, to support

a.setStepSize(..)
  .set setRegParam(...)


On Sun, Jul 23, 2017 at 8:47 PM, tao zhan <zh...@gmail.com> wrote:

> Thank you for replying.
> But I do not get it completely, why does the "this.type“” necessary?
> why could not it be like:
>
> def setStepSize(step: Double): Unit = {
>     require(step > 0,
>       s"Initial step size must be positive but got ${step}")
>     this.stepSize = step
> }
>
> On Mon, Jul 24, 2017 at 11:29 AM, M. Muvaffak ONUŞ <
> onus.muvaffak@gmail.com> wrote:
>
>> Doesn't it mean the return type will be type of "this" class. So, it
>> doesn't have to be this instance of the class but it has to be type of this
>> instance of the class. When you have a stack of inheritance and call that
>> function, it will return the same type with the level that you called it.
>>
>> On Sun, Jul 23, 2017 at 8:20 PM Reynold Xin <rx...@databricks.com> wrote:
>>
>>> It means the same object ("this") is returned.
>>>
>>> On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I am new to scala and spark.
>>>> What does the "this.type" in set function for?
>>>>
>>>>
>>>> ​
>>>> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5
>>>> e2596da1d600b9d0a/mllib/src/main/scala/org/apache/spark/
>>>> mllib/optimization/GradientDescent.scala#L48
>>>>
>>>> Thanks!
>>>>
>>>> Zhan
>>>>
>>>
>>>
>

Re: Question on Spark code

Posted by tao zhan <zh...@gmail.com>.
Thank you for replying.
But I do not get it completely, why does the "this.type“” necessary?
why could not it be like:

def setStepSize(step: Double): Unit = {
    require(step > 0,
      s"Initial step size must be positive but got ${step}")
    this.stepSize = step
}

On Mon, Jul 24, 2017 at 11:29 AM, M. Muvaffak ONUŞ <on...@gmail.com>
wrote:

> Doesn't it mean the return type will be type of "this" class. So, it
> doesn't have to be this instance of the class but it has to be type of this
> instance of the class. When you have a stack of inheritance and call that
> function, it will return the same type with the level that you called it.
>
> On Sun, Jul 23, 2017 at 8:20 PM Reynold Xin <rx...@databricks.com> wrote:
>
>> It means the same object ("this") is returned.
>>
>> On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> I am new to scala and spark.
>>> What does the "this.type" in set function for?
>>>
>>>
>>> ​
>>> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5e2596da
>>> 1d600b9d0a/mllib/src/main/scala/org/apache/spark/mllib/
>>> optimization/GradientDescent.scala#L48
>>>
>>> Thanks!
>>>
>>> Zhan
>>>
>>
>>

Re: Question on Spark code

Posted by "M. Muvaffak ONUŞ" <on...@gmail.com>.
Doesn't it mean the return type will be type of "this" class. So, it
doesn't have to be this instance of the class but it has to be type of this
instance of the class. When you have a stack of inheritance and call that
function, it will return the same type with the level that you called it.

On Sun, Jul 23, 2017 at 8:20 PM Reynold Xin <rx...@databricks.com> wrote:

> It means the same object ("this") is returned.
>
> On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:
>
>> Hello,
>>
>> I am new to scala and spark.
>> What does the "this.type" in set function for?
>>
>>
>> ​
>>
>> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5e2596da1d600b9d0a/mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala#L48
>>
>> Thanks!
>>
>> Zhan
>>
>
>

Re: Question on Spark code

Posted by Reynold Xin <rx...@databricks.com>.
It means the same object ("this") is returned.

On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:

> Hello,
>
> I am new to scala and spark.
> What does the "this.type" in set function for?
>
>
> ​
> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5e2596da
> 1d600b9d0a/mllib/src/main/scala/org/apache/spark/mllib/
> optimization/GradientDescent.scala#L48
>
> Thanks!
>
> Zhan
>

Re: Question on Spark code

Posted by Reynold Xin <rx...@databricks.com>.
It means the same object ("this") is returned.

On Sun, Jul 23, 2017 at 8:16 PM, tao zhan <zh...@gmail.com> wrote:

> Hello,
>
> I am new to scala and spark.
> What does the "this.type" in set function for?
>
>
> ​
> https://github.com/apache/spark/blob/481f0792944d9a77f0fe8b5e2596da
> 1d600b9d0a/mllib/src/main/scala/org/apache/spark/mllib/
> optimization/GradientDescent.scala#L48
>
> Thanks!
>
> Zhan
>