[RFE] Methods as expressions

classic Classic list List threaded Threaded
31 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

[RFE] Methods as expressions

Cédric Champeau
Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?

Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

David Dawson
Yes, I would love this kind of thing

On 20 March 2018 at 10:41, Cédric Champeau <[hidden email]> wrote:
Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

Andres Almiray
In reply to this post by Cédric Champeau
FYI you can also write it like

@Controller("/") class HelloController {
@Get("/hello/{name}")
String hello(String name) { "Hello $name" }
}
So you're only saving 2 characters (space and closing brace) by following Kotlin/Scala syntax.

Cheers,
Andres


-------------------------------------------
Java Champion; Groovy Enthusiast
JCP EC Associate Seat
http://andresalmiray.com
http://www.linkedin.com/in/aalmiray
--
What goes up, must come down. Ask any system administrator.
There are 10 types of people in the world: Those who understand binary, and those who don't.
To understand recursion, we must first understand recursion.

On Tue, Mar 20, 2018 at 11:41 AM, Cédric Champeau <[hidden email]> wrote:
Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

Cédric Champeau
I agree, that's the syntax I use for now. But I like the expression syntax `=` because of the explicitness of the expression (it's a function which value is ...). It's less about saving characters than it is about expressing the intent.

2018-03-20 12:08 GMT+01:00 Andres Almiray <[hidden email]>:
FYI you can also write it like

@Controller("/") class HelloController {
@Get("/hello/{name}")
String hello(String name) { "Hello $name" }
}
So you're only saving 2 characters (space and closing brace) by following Kotlin/Scala syntax.

Cheers,
Andres


-------------------------------------------
Java Champion; Groovy Enthusiast
JCP EC Associate Seat
http://andresalmiray.com
http://www.linkedin.com/in/aalmiray
--
What goes up, must come down. Ask any system administrator.
There are 10 types of people in the world: Those who understand binary, and those who don't.
To understand recursion, we must first understand recursion.

On Tue, Mar 20, 2018 at 11:41 AM, Cédric Champeau <[hidden email]> wrote:
Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?



MG
Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

MG
In reply to this post by Cédric Champeau
Am having a migraine right now so hard to concentrate / think straight but it seems all that syntax does is getting rid of a single character ?

Integer truth() { 42 }

could then be written as

Integer truth() = 42


or

String hello(String name) { "Hello $name" }

String hello(String name) = Hello $name"

(why did you use a return keyword in your sample ?)

I dont see an improvement in readability here - the main "advantage" is that curly braces are annoying to input on non-US keyboard layouts ;-)

mg



-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?

MG
Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

MG
In reply to this post by Cédric Champeau
PS: I am also worried that unecessarily deviating from Java syntax would make it harder for Java devs to pick up Groovy...


-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?

Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

Cédric Champeau
In reply to this post by Cédric Champeau
Again that's a declaration of intent (if you put apart the fact that you can have style rules that force you to put the brackets on new lines).

When I write:

double surface(double x, double y) = x * y

It's very clear what the intent of this is. It's an expression, a _function_. On the other hand, { ... } declares a block, that could represent an expression, or a statement, or a list of statements, one of them returning an expression. I like the declaration of intent.

2018-03-20 12:20 GMT+01:00 mg <[hidden email]>:
Am having a migraine right now so hard to concentrate / think straight but it seems all that syntax does is getting rid of a single character ?

Integer truth() { 42 }

could then be written as

Integer truth() = 42


or

String hello(String name) { "Hello $name" }

String hello(String name) = Hello $name"

(why did you use a return keyword in your sample ?)

I dont see an improvement in readability here - the main "advantage" is that curly braces are annoying to input on non-US keyboard layouts ;-)

mg



-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

Paolo Di Tommaso
In reply to this post by Cédric Champeau
I tend to agree. Also in Kotlin it tends a bit more readable because the function return type is postponed. 

IMO little is added by the proposed syntax extension. 


p

On Tue, Mar 20, 2018 at 12:20 PM, mg <[hidden email]> wrote:
Am having a migraine right now so hard to concentrate / think straight but it seems all that syntax does is getting rid of a single character ?

Integer truth() { 42 }

could then be written as

Integer truth() = 42


or

String hello(String name) { "Hello $name" }

String hello(String name) = Hello $name"

(why did you use a return keyword in your sample ?)

I dont see an improvement in readability here - the main "advantage" is that curly braces are annoying to input on non-US keyboard layouts ;-)

mg



-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


MG
Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

MG
In reply to this post by Cédric Champeau
on the other hand assignment normally means "immediate evaluation" whereas block means "delayed evaluation", so you are sort of breaking with that...


-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 12:23 (GMT+01:00)
Betreff: Re: [RFE] Methods as expressions

Again that's a declaration of intent (if you put apart the fact that you can have style rules that force you to put the brackets on new lines).

When I write:

double surface(double x, double y) = x * y

It's very clear what the intent of this is. It's an expression, a _function_. On the other hand, { ... } declares a block, that could represent an expression, or a statement, or a list of statements, one of them returning an expression. I like the declaration of intent.

2018-03-20 12:20 GMT+01:00 mg <[hidden email]>:
Am having a migraine right now so hard to concentrate / think straight but it seems all that syntax does is getting rid of a single character ?

Integer truth() { 42 }

could then be written as

Integer truth() = 42


or

String hello(String name) { "Hello $name" }

String hello(String name) = Hello $name"

(why did you use a return keyword in your sample ?)

I dont see an improvement in readability here - the main "advantage" is that curly braces are annoying to input on non-US keyboard layouts ;-)

mg



-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


MG
Reply | Threaded
Open this post in threaded view
|

Re: [RFE] Methods as expressions

MG
In reply to this post by Cédric Champeau
@style rules: Then change your style rules to allow single line single statement blocks, do not change the language, no ?-)

-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 12:23 (GMT+01:00)
Betreff: Re: [RFE] Methods as expressions

Again that's a declaration of intent (if you put apart the fact that you can have style rules that force you to put the brackets on new lines).

When I write:

double surface(double x, double y) = x * y

It's very clear what the intent of this is. It's an expression, a _function_. On the other hand, { ... } declares a block, that could represent an expression, or a statement, or a list of statements, one of them returning an expression. I like the declaration of intent.

2018-03-20 12:20 GMT+01:00 mg <[hidden email]>:
Am having a migraine right now so hard to concentrate / think straight but it seems all that syntax does is getting rid of a single character ?

Integer truth() { 42 }

could then be written as

Integer truth() = 42


or

String hello(String name) { "Hello $name" }

String hello(String name) = Hello $name"

(why did you use a return keyword in your sample ?)

I dont see an improvement in readability here - the main "advantage" is that curly braces are annoying to input on non-US keyboard layouts ;-)

mg



-------- Ursprüngliche Nachricht --------
Von: Cédric Champeau <[hidden email]>
Datum: 20.03.18 11:41 (GMT+01:00)
Betreff: [RFE] Methods as expressions

Hi,

One of the Kotlin features I really like is the short-hand notation for simple expression methods:

class Foo {
    fun truth(): Integer = 42
}

For example, in Groovy, you write:

@Controller("/") class HelloController {

@Get("/hello/{name}")
String hello(String name) {
return "Hello $name"
}
}

but we could write:

@Controller("/") 
class HelloController {
@Get("/hello/{name}")
String hello(String name) = "Hello $name"
}

It's more concise and makes the "functional style" more readable. Is this something Groovy users would appreciate?


1234