gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 155 forks source link

CoffeeScript compatibility problem #288

Open jorgebg opened 11 years ago

jorgebg commented 11 years ago

The following code works in CoffeeScript:

Express = require 'express'
class MyApp extends Express
myapp = new MyApp
myapp.listen 3000

According to CoffeeScript to LiveScript Conversion Guide, the following code should work in LiveScript, but it throws the following error:

TypeError: Object #<MyApp> has no method 'listen'
    at Object.<anonymous> (/tmp/myapp.coffee:11:7)
 7|   }
 8|   return MyApp;
 9| }(Express));
10| myapp = new MyApp;
11+ myapp.listen(3000);
12| function extend$(sub, sup){
13|   function fun(){} fun.prototype = (sub.superclass = sup).prototype;
14|   (sub.prototype = new fun).constructor = sub;
15|   if (typeof sup.extended == 'function') sup.extended(sub);
    at Module._compile (module.js:449:26)
    at Object.module.exports.LiveScript.run (/usr/local/lib/node_modules/LiveScript/lib/node.js:21:19)
    at compileScript (/usr/local/lib/node_modules/LiveScript/lib/command.js:181:29)
    at /usr/local/lib/node_modules/LiveScript/lib/command.js:110:9
    at /usr/local/lib/node_modules/LiveScript/lib/command.js:100:5
    at fs.readFile (fs.js:176:14)
    at Object.oncomplete (fs.js:297:15)

Why? And how can it be solved? Thanks!

PD: In case you doubt about what express is, it is the well-known web application framework for node :) and here is the link: http://expressjs.com/

vendethiel commented 11 years ago

EDIT : I remembered this not working but apparently it does

jorgebg commented 11 years ago

Yes it works in CoffeeScript :(

gkz commented 11 years ago

Can you log the myapp object and check out what's up with it - what its prototype is, etc.?

vendethiel commented 11 years ago

Isn't it because we don't actually return the parent constructor ? Other typed thingies

jorgebg commented 11 years ago

Yes it is, If I return the parent constructor it works:

Express = require 'express'
class MyApp extends Express
  -> return super ...
myapp = new MyApp
myapp.listen 3000

But when I try to extend it without overriding the constructor in order to return the parent constructor, the TypeError appears again:

class MyApp2 extends MyApp
myapp2 = new MyApp2
myapp2.listen 3000
TypeError: Object #<MyApp2> has no method 'listen'
    at Object.<anonymous> (/tmp/myapp2.ls:19:8)
...

Some logging:

Express = require 'express'
class MyApp extends Express
console.log MyApp # Too long, pasted here http://pastebin.com/fwM33jXP 
myapp = new MyApp
cosole.log myapp # It returns an empty object {}