Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:27:23+00:00 2026-05-25T23:27:23+00:00

I was trying to learn CoffeeScript, and made this simple class as first try:

  • 0

I was trying to learn CoffeeScript, and made this simple class as first try:

class test    
  fib: (x) -> 
     x if x == 0 || x == 1   
     (this.fib x-1) + (this.fib x-2)

t = new test
alert(t.fib(6));

This code doesn’t work because it gets compiled without a return statement in the if statement. This works:

fib: (x) -> 
  return x if x == 0 || x == 1   
  (this.fib x-1) + (this.fib x-2)

Why do I need the explicit return ? Based on the language description, especially http://jashkenas.github.com/coffee-script/#expressions, I expected the x expression to be converted to a return by the compiler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:27:24+00:00Added an answer on May 25, 2026 at 11:27 pm

    Why would you expect the x expression to be converted to a return? Only the last expression in a method or function is converted to a return.

    In Jeremy’s if/then/else example, there are two possible last expressions, and the coffeescript parser understands to be the case in an if/then/else, which is not what you have here: instead, you have on expression with no lvalue, followed by another perfectly valid expression. (There’s some discussion as to whether or not the if statement itself is an expression; arguably, it should be read as one, but the compiled output of an if/then/else contains a return keyword in both the then clause and the else clause.)

    The compiler can’t read your mind. It doesn’t know what your intent is there with that rvalue x expression. All it knows is that another perfectly valid expression in the same scope follows it.

    To get the effect you want:

    if x == 0 or x == 1
        x
    else 
        (@fib x-1) + (@fib x-2)
    

    Or one-liner:

    if x == 0 or x == 1 then x else (@fib x-1) + (@fib x-2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to learn OOP and I've made this class class boo{ function boo(&another_class,
Trying to learn pointer math better I wrote this code. The intention was to
Trying to learn pointers better I wrote this code. The intention was to print
Trying to learn a bit about PDO and is going through this tutorial .
I'm trying to learn C++ so forgive me if this question demonstrates a lack
Trying to learn HTML5 and downloaded a test project https://github.com/amiklosi/Comicr . There is a
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've
Trying to learn Django, I closed the shell and am getting this problem now
While trying to learn c++, I tried to implement class representing very basic trie.
I'm trying to learn RoR and I keep getting pesky page doesn't exist pages.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.