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

Feature: Classes (for TS at least) #68

Open
pitust opened this issue May 13, 2020 · 8 comments
Open

Feature: Classes (for TS at least) #68

pitust opened this issue May 13, 2020 · 8 comments

Comments

@pitust
Copy link

pitust commented May 13, 2020

Classes shouldn't be too difficult to get working, especially in typescript, and should be even easier than prototypes (i think).

@ceremcem
Copy link

ceremcem commented May 13, 2020

FYI: LiveScript classes look like the following:

class A
  -> console.log \hello

  x: (y) -> 2 * y

  t: ~> 2 * t

...and compiles to the following Javascript:

var A;
A = (function(){
  A.displayName = 'A';
  var prototype = A.prototype, constructor = A;
  function A(){
    this.t = bind$(this, 't', prototype);
    console.log('hello');
  }
  A.prototype.x = function(y){
    return 2 * y;
  };
  A.prototype.t = function(){
    return 2 * t;
  };
  return A;
}());
function bind$(obj, key, target){
  return function(){ return (target || obj)[key].apply(obj, arguments) };
}

@andrei-markeev
Copy link
Owner

if we get prototype working, then we can use TS->ES3 transpilation to have classes as well.
I don't want to make a separate implementation for classes for now.
Let's cover ES3 first, this will open many doors :)

@pitust
Copy link
Author

pitust commented Jun 1, 2020

What if we made Functions be Objects?

@pitust
Copy link
Author

pitust commented Jun 1, 2020

(that would let us have the first thing @eclipticccc suggested)

@pitust
Copy link
Author

pitust commented Jun 1, 2020

Here is how i see it: make functions have an internal field, lets say __internal_fnptr_call, and when we call them, use that field instead.

@andrei-markeev
Copy link
Owner

@pitust it already kind of works like that

there are actually 3 things:

  • reference to the function
  • function closure
  • function object fields

@pitust
Copy link
Author

pitust commented Jun 1, 2020

Maybe, but code like:

function f() {}
f.a = 3;

doesn't work.
However, this does work:

function __real_f() {}
var f = {};
f.__internal_fnptr_call = __real_f;
f.a = 3;

@andrei-markeev
Copy link
Owner

ah I see. there's a difference between this (i.e. instance of a function) and statically adding fields to the function object. yes that is still not implemented.

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