Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add return identifier or default value in the function declaration. #3583

Closed
xyrius opened this issue Aug 12, 2014 · 2 comments
Closed

Comments

@xyrius
Copy link

xyrius commented Aug 12, 2014

I would love to see the default return value/identifier in the function statement or a undefined return
as we have in Javascript

I very often see myself adding an extra line to force a undefined or other return value.

This would also eliminate the need for ending a function with @ or this (taking up a whole line for a single @) to chain functions

Any return statement inside the function should override the default return value

for example:

    class Person
        constructor: ( @name ) ->

        setNickName: @ ( nickName ) -> @nickName= nickName

        setAge: @ ( age ) -> @age= age

        addRoomMate: true ( somePerson ) ->
            return false if @roomIsFull()
            @roomMates.push( somePerson )
            @doSomeMoreInitialization()

    john= new Person 'john'
    john.setNickName('jt').setAge(24)
    letsParty() if john.addRoomMate( harry )


instead of:

    class Person

        constructor: ( @name ) ->

        setNickName: ( nickName ) ->
            @nickName= nickName
            @

        setAge: ( age ) ->
            @age= age
            @

        addRoomMate: ( somePerson ) ->
            return false if @roomIsFull()
            @roomMates.push( somePerson )
            @doSomeMoreInitialization()
            true
@DylanPiercey
Copy link
Contributor

Or simply use cascades from dart.

class Person
    constructor: ( @name ) ->

    setNickName: ( nickName ) -> @nickName= nickName

    setAge:  ( age ) -> @age= age

    addRoomMate: ( somePerson ) -> #Not sure about this one
        return false if @roomIsFull()
        @roomMates.push( somePerson )
        @doSomeMoreInitialization()

and then:

john= new Person 'john'
..setNickName('jt')
..setAge(24)

or:

john= new Person'john'
john..setNickName('jt')..setAge(24)

@GeoffreyBooth
Copy link
Collaborator

setNickName: @ ( nickName ) -> @nickName= nickName already compiles, so you would need to come up with a new syntax. I’m not sure there is a syntax like what you’re asking for that wouldn’t be a breaking change.

Removing implicit returns has been discussed before, and isn’t currently on the roadmap. If you want a function to return undefined like in JavaScript, end your function with return by itself on a line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants