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 156 forks source link

[Question] Possible to expand/loop literal text? #971

Closed Industrial closed 7 years ago

Industrial commented 7 years ago

Hi.

I am using a library Objection.js which is an ORM. It has a class Model which I'd like to extend. The problem is that it exports ES6 classes, and I can't seem to use LiveScript classes to extend that.

Is it possible to create a fuction that exports class #{class-name} extends {#parent-class-name} { etc }

?

rhendric commented 7 years ago

It's a little hacky, but here's a pattern that might work for you:

es6-class = ({superclass, ::}:ls-class) ->
    ``class extends superclass {}`` <<< ls-class then ..:: <<< ::

YourClass = es6-class class extends ParentClass
    # Standard LiveScript class code here...
    # except you can't have a custom constructor, sorry!
    # The base class's constructor will always be used.

If your classes really need custom initialization code, you can probably find a way to hack that into es6-class, but it'll be tricky.

vendethiel commented 7 years ago

there are many things we allow in LS classes that aren't allowed in ES classes, but it's true it'd be helpful being able to extends Array, Date, etc...

Industrial commented 7 years ago

Thanks for the help

I decided to evade the problem entirely by using Knex.js inside my GraphQL resolvers and not Objection.js.