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

where (as in let)

10 replies
Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.

hi,

i think i wish Scala syntax supported the placement of nested
functions at the end somehow, like 'where' in haskell. any tricks for
that? like, sure, i could probably whip up my own thing on top of
scala to hack it, but that seems extreme. i wish the core syntax
supported it, but i figure the core syntax doesn't, since the last
line of a method is the return value.

hm.

jibal
Joined: 2010-12-01,
User offline. Last seen 1 year 45 weeks ago.
Re: where (as in let)
On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke <raould@gmail.com> wrote:
hi,

i think i wish Scala syntax supported the placement of nested
functions at the end somehow, like 'where' in haskell. any tricks for
that? like, sure, i could probably whip up my own thing on top of
scala to hack it, but that seems extreme. i wish the core syntax
supported it, but i figure the core syntax doesn't, since the last
line of a method is the return value.

hm.

It would be nice if Scala treated the last non-definition in a block as the value of the block; I see no value in the current behavior of implicitly inserting "; ()" after a final definition:

scala> val x = { f; def f = 1}       
x: Unit = ()

-- Jim


Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: where (as in let)
I wanted to have it for a long time now. I'm still wondering if it is syntaxically possible ... Maybe with some implicit  and structural types, but I couldn't get it done yet ...

2011/1/22 Jim Balter <Jim@balter.name>
On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke <raould@gmail.com> wrote:
hi,

i think i wish Scala syntax supported the placement of nested
functions at the end somehow, like 'where' in haskell. any tricks for
that? like, sure, i could probably whip up my own thing on top of
scala to hack it, but that seems extreme. i wish the core syntax
supported it, but i figure the core syntax doesn't, since the last
line of a method is the return value.

hm.

It would be nice if Scala treated the last non-definition in a block as the value of the block; I see no value in the current behavior of implicitly inserting "; ()" after a final definition:

scala> val x = { f; def f = 1}       
x: Unit = ()

-- Jim



Ruediger Keller 2
Joined: 2010-04-30,
User offline. Last seen 42 years 45 weeks ago.
Re: where (as in let)

I'm not too familiar with the where syntax from Haskell. What can you
do with it, that is not possible with inner methods? Is it just that
the syntax for inner methods is more verbose and that they cannot be
the last statement in the method? Or is there more to it?

Regardless, I really like Jim's suggestion that the last
non-definition should be the return value of a block.

Regards,
Ruediger

2011/1/23 Alex Repain :
> I wanted to have it for a long time now. I'm still wondering if it is
> syntaxically possible ... Maybe with some implicit  and structural types,
> but I couldn't get it done yet ...
>
> 2011/1/22 Jim Balter
>>
>> On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke wrote:
>>>
>>> hi,
>>>
>>> i think i wish Scala syntax supported the placement of nested
>>> functions at the end somehow, like 'where' in haskell. any tricks for
>>> that? like, sure, i could probably whip up my own thing on top of
>>> scala to hack it, but that seems extreme. i wish the core syntax
>>> supported it, but i figure the core syntax doesn't, since the last
>>> line of a method is the return value.
>>>
>>> hm.
>>
>> It would be nice if Scala treated the last non-definition in a block as
>> the value of the block; I see no value in the current behavior of implicitly
>> inserting "; ()" after a final definition:
>>
>> scala> val x = { f; def f = 1}
>> x: Unit = ()
>>

Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: where (as in let)


2011/1/23 Ruediger Keller <ruediger.keller@rk42.de>
I'm not too familiar with the where syntax from Haskell. What can you
do with it, that is not possible with inner methods? Is it just that
the syntax for inner methods is more verbose and that they cannot be
the last statement in the method? Or is there more to it?

I don't think the 'where' clause can do more than declaring your vars/vals inside the block and then working with them, and it's not a lot more concise. But it's functional style, and some coders are used to this. Besides, it adds some clarity. A 'let {...} in {...}' or '{...} where {...}' block is certainlly more readable than using anonymous functions directly, and can help separating an algorithm and its data implementation. At least that's where I would use it the most.

Cheers,
Alex

Regardless, I really like Jim's suggestion that the last
non-definition should be the return value of a block.

Regards,
Ruediger


2011/1/23 Alex Repain <alex.repain@gmail.com>:
> I wanted to have it for a long time now. I'm still wondering if it is
> syntaxically possible ... Maybe with some implicit  and structural types,
> but I couldn't get it done yet ...
>
> 2011/1/22 Jim Balter <Jim@balter.name>
>>
>> On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke <raould@gmail.com> wrote:
>>>
>>> hi,
>>>
>>> i think i wish Scala syntax supported the placement of nested
>>> functions at the end somehow, like 'where' in haskell. any tricks for
>>> that? like, sure, i could probably whip up my own thing on top of
>>> scala to hack it, but that seems extreme. i wish the core syntax
>>> supported it, but i figure the core syntax doesn't, since the last
>>> line of a method is the return value.
>>>
>>> hm.
>>
>> It would be nice if Scala treated the last non-definition in a block as
>> the value of the block; I see no value in the current behavior of implicitly
>> inserting "; ()" after a final definition:
>>
>> scala> val x = { f; def f = 1}
>> x: Unit = ()
>>
>> -- Jim
>>
>>
>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: where (as in let)
if it's just the readability, why not write code like this:
val x = {
   var a = ...
   var b = ...
   ... code that uses a and b and returns the result ...
}

now it's clear where a and b are used

Am 23.01.2011 11:13, schrieb Alex Repain:
UYVA1wZ+2xGvjsLJG1 [at] mail [dot] gmail [dot] com" type="cite">

2011/1/23 Ruediger Keller <ruediger [dot] keller [at] rk42 [dot] de" rel="nofollow">ruediger.keller@rk42.de>
I'm not too familiar with the where syntax from Haskell. What can you
do with it, that is not possible with inner methods? Is it just that
the syntax for inner methods is more verbose and that they cannot be
the last statement in the method? Or is there more to it?

I don't think the 'where' clause can do more than declaring your vars/vals inside the block and then working with them, and it's not a lot more concise. But it's functional style, and some coders are used to this. Besides, it adds some clarity. A 'let {...} in {...}' or '{...} where {...}' block is certainlly more readable than using anonymous functions directly, and can help separating an algorithm and its data implementation. At least that's where I would use it the most.

Cheers,
Alex

Regardless, I really like Jim's suggestion that the last
non-definition should be the return value of a block.

Regards,
Ruediger


2011/1/23 Alex Repain <alex [dot] repain [at] gmail [dot] com" rel="nofollow">alex.repain@gmail.com>:
> I wanted to have it for a long time now. I'm still wondering if it is
> syntaxically possible ... Maybe with some implicit  and structural types,
> but I couldn't get it done yet ...
>
> 2011/1/22 Jim Balter <Jim [at] balter [dot] name" rel="nofollow">Jim@balter.name>
>>
>> On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke <raould [at] gmail [dot] com" rel="nofollow">raould@gmail.com> wrote:
>>>
>>> hi,
>>>
>>> i think i wish Scala syntax supported the placement of nested
>>> functions at the end somehow, like 'where' in haskell. any tricks for
>>> that? like, sure, i could probably whip up my own thing on top of
>>> scala to hack it, but that seems extreme. i wish the core syntax
>>> supported it, but i figure the core syntax doesn't, since the last
>>> line of a method is the return value.
>>>
>>> hm.
>>
>> It would be nice if Scala treated the last non-definition in a block as
>> the value of the block; I see no value in the current behavior of implicitly
>> inserting "; ()" after a final definition:
>>
>> scala> val x = { f; def f = 1}
>> x: Unit = ()
>>
>> -- Jim
>>
>>
>
>


Ruediger Keller 2
Joined: 2010-04-30,
User offline. Last seen 42 years 45 weeks ago.
Re: where (as in let)

Just to clarify, I was talking about inner methods, not anonymous
functions. Are we talking about different things?

I meant something like:

def length[T](list: List[T]) = {
// Here comes the inner method
def len(l: List[T], acc: Int): Int = {
if(l.isEmpty) acc else len(l.tail, acc + 1)
}

len(list, 0)
}

Regards,
Ruediger

2011/1/23 Alex Repain :
>
>
> 2011/1/23 Ruediger Keller
>>
>> I'm not too familiar with the where syntax from Haskell. What can you
>> do with it, that is not possible with inner methods? Is it just that
>> the syntax for inner methods is more verbose and that they cannot be
>> the last statement in the method? Or is there more to it?
>>
> I don't think the 'where' clause can do more than declaring your vars/vals
> inside the block and then working with them, and it's not a lot more
> concise. But it's functional style, and some coders are used to this.
> Besides, it adds some clarity. A 'let {...} in {...}' or '{...} where {...}'
> block is certainlly more readable than using anonymous functions directly,
> and can help separating an algorithm and its data implementation. At least
> that's where I would use it the most.
>
> Cheers,
> Alex
>
>> Regardless, I really like Jim's suggestion that the last
>> non-definition should be the return value of a block.
>>
>> Regards,
>> Ruediger
>>
>>
>> 2011/1/23 Alex Repain :
>> > I wanted to have it for a long time now. I'm still wondering if it is
>> > syntaxically possible ... Maybe with some implicit  and structural
>> > types,
>> > but I couldn't get it done yet ...
>> >
>> > 2011/1/22 Jim Balter
>> >>
>> >> On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke wrote:
>> >>>
>> >>> hi,
>> >>>
>> >>> i think i wish Scala syntax supported the placement of nested
>> >>> functions at the end somehow, like 'where' in haskell. any tricks for
>> >>> that? like, sure, i could probably whip up my own thing on top of
>> >>> scala to hack it, but that seems extreme. i wish the core syntax
>> >>> supported it, but i figure the core syntax doesn't, since the last
>> >>> line of a method is the return value.
>> >>>
>> >>> hm.
>> >>
>> >> It would be nice if Scala treated the last non-definition in a block as
>> >> the value of the block; I see no value in the current behavior of
>> >> implicitly
>> >> inserting "; ()" after a final definition:
>> >>
>> >> scala> val x = { f; def f = 1}
>> >> x: Unit = ()
>> >>
>> >> -- Jim
>> >>
>> >>
>> >
>> >
>
>

Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: where (as in let)


2011/1/23 Ruediger Keller <ruediger.keller@rk42.de>
Just to clarify, I was talking about inner methods, not anonymous
functions. Are we talking about different things?

Not really in terms of expressivity, and yes in terms of syntax ... But the matter is quite the same. The difference between your code and the following :

def length[T](list: List[T]) = {
    println(startMessage)
    len(list, 0)
} where {
    val startMessage = "Now entering length method."
    def len(l: List[T], acc: Int): Int = if(l.isEmpty) acc else len(l.tail, acc + 1)
}

is that in this second version, you separate syntaxically your length method in two code block. In this particular case, it's not that relevant, I guess. But my point was that you can will to break your code in two sections, one for an algorithmic view, the other for implementation details. Notice that in many languages, say the ML family, the where method is not only to move the definition of functions/inner methods, but also of inner values. There surely are other reasons why you can find Haskell-like 'where' useful.
One good article about 'let' VS 'where' in Haskell : http://www.haskell.org/haskellwiki/Let_vs._Where

Alex


I meant something like:

 def length[T](list: List[T]) = {
   // Here comes the inner method
   def len(l: List[T], acc: Int): Int = {
     if(l.isEmpty) acc else len(l.tail, acc + 1)
   }

   len(list, 0)
 }

Regards,
Ruediger


2011/1/23 Alex Repain <alex.repain@gmail.com>:
>
>
> 2011/1/23 Ruediger Keller <ruediger.keller@rk42.de>
>>
>> I'm not too familiar with the where syntax from Haskell. What can you
>> do with it, that is not possible with inner methods? Is it just that
>> the syntax for inner methods is more verbose and that they cannot be
>> the last statement in the method? Or is there more to it?
>>
> I don't think the 'where' clause can do more than declaring your vars/vals
> inside the block and then working with them, and it's not a lot more
> concise. But it's functional style, and some coders are used to this.
> Besides, it adds some clarity. A 'let {...} in {...}' or '{...} where {...}'
> block is certainlly more readable than using anonymous functions directly,
> and can help separating an algorithm and its data implementation. At least
> that's where I would use it the most.
>
> Cheers,
> Alex
>
>> Regardless, I really like Jim's suggestion that the last
>> non-definition should be the return value of a block.
>>
>> Regards,
>> Ruediger
>>
>>
>> 2011/1/23 Alex Repain <alex.repain@gmail.com>:
>> > I wanted to have it for a long time now. I'm still wondering if it is
>> > syntaxically possible ... Maybe with some implicit  and structural
>> > types,
>> > but I couldn't get it done yet ...
>> >
>> > 2011/1/22 Jim Balter <Jim@balter.name>
>> >>
>> >> On Sat, Jan 22, 2011 at 6:20 AM, Raoul Duke <raould@gmail.com> wrote:
>> >>>
>> >>> hi,
>> >>>
>> >>> i think i wish Scala syntax supported the placement of nested
>> >>> functions at the end somehow, like 'where' in haskell. any tricks for
>> >>> that? like, sure, i could probably whip up my own thing on top of
>> >>> scala to hack it, but that seems extreme. i wish the core syntax
>> >>> supported it, but i figure the core syntax doesn't, since the last
>> >>> line of a method is the return value.
>> >>>
>> >>> hm.
>> >>
>> >> It would be nice if Scala treated the last non-definition in a block as
>> >> the value of the block; I see no value in the current behavior of
>> >> implicitly
>> >> inserting "; ()" after a final definition:
>> >>
>> >> scala> val x = { f; def f = 1}
>> >> x: Unit = ()
>> >>
>> >> -- Jim
>> >>
>> >>
>> >
>> >
>
>

Aydjen
Joined: 2009-08-21,
User offline. Last seen 1 year 28 weeks ago.
Re: where (as in let)

Hi,

the idea is to write top-down, adding more details as you go. That way, you always find the basic ideas of an algorithm at the top of a class/method without having to search through all the details first. I think this idea is very closely related to how methods should be ordered inside a class as presented in Robert Martin's book "clean code".

It's just about how to present the structure of something. Please bear with me and compare the following two pseudo-representations of an "algorithm":

{
to write an email you
open your mail client
open a blank email template
fill in address, topic and text
send it

where "open your mail client" means: move your mouse pointer to ...
press the left mouse button
...
"open a blank email template" means: move your mouse pointer to the "new email" button ...
press the left mouse button
"fill in address, topic and text" means: ...
...
...
"send it" means: ...
...
}

as opposed to

{
"open your mail client" means: move your mouse pointer to ...
press the left mouse button
...
"open a blank email template" means: move your mouse pointer to the "new email" button ...
press the left mouse button
"fill in address, topic and text" means: ...
...
...
"send it" means: ...
...

open your mail client
open a blank email template
fill in address, topic and text
send it
}

I would like to write stuff in the former style. I can do that partially already by changing the order of methods inside a class, but once I wish to nest methods/functions, I'm screwed.

Just my 2c.

Kind regards
Andreas

HamsterofDeath wrote:

> if it's just the readability, why not write code like this:
> val x = {
> var a = ...
> var b = ...
> ... code that uses a and b and returns the result ...
> }

Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: where (as in let)
(useless post)

Hi Andreas,

funny thing but the w

2011/1/23 Andreas Flierl <andreas@flierl.eu>
Hi,

the idea is to write top-down, adding more details as you go. That way, you always find the basic ideas of an algorithm at the top of a class/method without having to search through all the details first. I think this idea is very closely related to how methods should be ordered inside a class as presented in Robert Martin's book "clean code".

It's just about how to present the structure of something. Please bear with me and compare the following two pseudo-representations of an "algorithm":

{
to write an email you
 open your mail client
 open a blank email template
 fill in address, topic and text
 send it

 where "open your mail client" means: move your mouse pointer to ...
                                      press the left mouse button
                                      ...
       "open a blank email template" means: move your mouse pointer to the "new email" button ...
                                            press the left mouse button
       "fill in address, topic and text" means: ...
                                                ...
                                                ...
       "send it" means: ...
                        ...
}


as opposed to


{
"open your mail client" means: move your mouse pointer to ...
                              press the left mouse button
                              ...
"open a blank email template" means: move your mouse pointer to the "new email" button ...
                                    press the left mouse button
"fill in address, topic and text" means: ...
                                        ...
                                        ...
"send it" means: ...
                ...

open your mail client
open a blank email template
fill in address, topic and text
send it
}


I would like to write stuff in the former style. I can do that partially already by changing the order of methods inside a class, but once I wish to nest methods/functions, I'm screwed.

Just my 2c.

Kind regards
Andreas

HamsterofDeath wrote:

> if it's just the readability, why not write code like this:
> val x = {
>    var a = ...
>    var b = ...
>    ... code that uses a and b and returns the result ...
> }


Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: where (as in let)
Sorry about that.

The way you presented it was really close to how we generally present books :

First, the list of chapters
THEN, the content of each chapter.

I doubt you could be more obvious than this about the utility of 'where'.

2011/1/23 Alex Repain <alex.repain@gmail.com>
(useless post)

Hi Andreas,

funny thing but the way

2011/1/23 Andreas Flierl <andreas@flierl.eu>
Hi,

the idea is to write top-down, adding more details as you go. That way, you always find the basic ideas of an algorithm at the top of a class/method without having to search through all the details first. I think this idea is very closely related to how methods should be ordered inside a class as presented in Robert Martin's book "clean code".

It's just about how to present the structure of something. Please bear with me and compare the following two pseudo-representations of an "algorithm":

{
to write an email you
 open your mail client
 open a blank email template
 fill in address, topic and text
 send it

 where "open your mail client" means: move your mouse pointer to ...
                                      press the left mouse button
                                      ...
       "open a blank email template" means: move your mouse pointer to the "new email" button ...
                                            press the left mouse button
       "fill in address, topic and text" means: ...
                                                ...
                                                ...
       "send it" means: ...
                        ...
}


as opposed to


{
"open your mail client" means: move your mouse pointer to ...
                              press the left mouse button
                              ...
"open a blank email template" means: move your mouse pointer to the "new email" button ...
                                    press the left mouse button
"fill in address, topic and text" means: ...
                                        ...
                                        ...
"send it" means: ...
                ...

open your mail client
open a blank email template
fill in address, topic and text
send it
}


I would like to write stuff in the former style. I can do that partially already by changing the order of methods inside a class, but once I wish to nest methods/functions, I'm screwed.

Just my 2c.

Kind regards
Andreas

HamsterofDeath wrote:

> if it's just the readability, why not write code like this:
> val x = {
>    var a = ...
>    var b = ...
>    ... code that uses a and b and returns the result ...
> }



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