Giter VIP home page Giter VIP logo

Comments (7)

smaye81 avatar smaye81 commented on May 14, 2024

Seeing the same. In fact, anything with int is failing for me. Using number though with parameter annotations works, but not for field annotations. For example, this works fine:

class MyClass {
    constructor(xArg:number, yArg:number) {
        this.x = xArg;
        this.y = yArg;
    }
}
var myClass = new MyClass(1, 2);
console.log(myClass);

var myClass2 = new MyClass("test", "test");  // this will fail in the console (as expected)

But this doesn't:

class MyClass {
    x:number;
    y:number;
}

from atscript-playground.

mlb6 avatar mlb6 commented on May 14, 2024

I solved the problem on my side by adding this to the config.json : "memberVariables":true
I guessed it by looking at this line of the traceur code: https://github.com/google/traceur-compiler/blob/master/src/Options.js#L143

from atscript-playground.

smaye81 avatar smaye81 commented on May 14, 2024

Interesting. I can get it to compile by doing that also. I can also define a class like above with:

export class Point {
    x:int;
}

But if I try to instantiate it like so:

var point = new Point(5);
console.log(point);

I see this in my console:

Point {x: (...)}
x: [Exception: ReferenceError: int is not defined]
__proto__: Point

And this is my config:

{
  "traceur": {
    "modules": "register",
    "moduleName" : true,
    "script": false,
    "types": true,
    "typeAssertions": true,
    "typeAssertionModule": "assert",
    "annotations": true,
    "sourceMaps": false,
    "memberVariables" : true,
    "experimental" : true
  }
}

from atscript-playground.

mlb6 avatar mlb6 commented on May 14, 2024

You get this error because you forgot to write a constructor :

export class Point {
  x:int;
  constructor(x:int){
    this.x = x;
  }
}

from atscript-playground.

smaye81 avatar smaye81 commented on May 14, 2024

Shouldn't the transpiler auto-generate the constructor for me? Take a look at the AtScript primer by @mhevery

https://docs.google.com/document/d/11YUzC-1d0V1-Q3V0fQ7KSit97HnZoKVygDxpWzEYW0U/mobilebasic?viewopt=127

Look at the Field Annotations section, specifically this line:

By specifying the fields in this way, the transpiler can generate the constructor which then pre-creates the fields.

from atscript-playground.

mlb6 avatar mlb6 commented on May 14, 2024

It specifies that the transpiler can generate the constructor. But it does not say if the constructor will assign values. In the example provided by @mhevery field variables are assigned to null.

class MyClass {
  constructor() {
    this.x = null; // auto-generated
    this.y = null; // auto-generated
  }
}

But when I transpile the code into es6 using traceur, there is no constructor :

class Point {
  get x() {
    return this.$__0;
  }
  set x(value) {
    this.$__0 = value;
  }
  get y() {
    return this.$__1;
  }
  set y(value) {
    this.$__1 = value;
  }
}
Object.defineProperty(Object.getOwnPropertyDescriptor(Point.prototype, "x").set, "parameters", {get: function() {
    return [[int]];
  }});
Object.defineProperty(Object.getOwnPropertyDescriptor(Point.prototype, "y").set, "parameters", {get: function() {
    return [[int]];
  }});

So, I agree with you on the fact it does not generate constructor, but anyway according to this document atScript will not auto-assign your fields. So, you'll have to write a constructor.

from atscript-playground.

mhevery avatar mhevery commented on May 14, 2024

work in progress.

On Thu Nov 20 2014 at 1:39:03 PM Martin Le Bas [email protected]
wrote:

It specifies that the transpiler can generate the constructor. But it
does not say if the constructor will assign values. In the example provided
by @mhevery https://github.com/mhevery field variables are assigned to
null.

class MyClass {
constructor() {
this.x = null; // auto-generated
this.y = null; // auto-generated
}
}

But when I transpile the code into es6 using traceur, there is no
constructor :

class Point {
get x() {
return this.$__0;
}
set x(value) {
this.$__0 = value;
}
get y() {
return this.$__1;
}
set y(value) {
this.$__1 = value;
}
}Object.defineProperty(Object.getOwnPropertyDescriptor(Point.prototype, "x").set, "parameters", {get: function() {
return [[int]];
}});Object.defineProperty(Object.getOwnPropertyDescriptor(Point.prototype, "y").set, "parameters", {get: function() {
return [[int]];
}});

So, I agree with you on the fact it does not generate constructor, but
anyway according to this document atScript will not auto-assign your
fields. So, you'll have to write a constructor.


Reply to this email directly or view it on GitHub
#7 (comment)
.

from atscript-playground.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.