I'm very new to coffeescript. So what does this error actually mean?
this is the class
class Animation
constructor: (t) ->
#startTime: t
I'm trying to set it up so that this class has a member startTime initialized to t during construction. Am I doing it wrong?
Your code is creating an object in the constructor (and not saving the reference anywhere) with a key of #startTime. The error occurs because # isn't a valid character for an object key.
Try this instead:
class Animation
constructor: (#startTime) ->
Here's the generated JavaScript:
var Animation;
Animation = (function() {
function Animation(startTime) {
this.startTime = startTime;
}
return Animation;
})();
Here's where you can see the syntax for what you wanted to do: http://coffeescript.org/#classes
Here's the syntax you were incorrectly and unintentionally using: http://coffeescript.org/#literals (the section titled "Objects and Arrays")
Related
I have the following code in dart, which decodes a string into a JSON object.
import 'dart:convert';
void main(){
var stringValue = "{\"last_supported\": \"2.00\", \"current\": \"2.00\"}";
var newValue = json.decode(stringValue);
print(newValue["last_supported"]);
}
The above code works fine, but when I change print statement to:
print(newValue.last_supported);
It gives me the following exception:
Uncaught TypeError: C.C_JsonCodec.decode$1(...).get$last_supported is not a function
Is it possible to use dot annotation to access properties, and how?
I'm guessing you come from a java-script background.
in dart object keys cannot be accessed through the . dot notation.
rather they are accessed like arrays with ['key_name'].
so that's why this line doesn't work
print(newValue.last_supported)
and this one does
print(newValue["last_supported"]);
dot notation in dart only works on class instances, not Map (similar to JavaScript objects).
look at the following :
class User {
final String name;
final int age;
// other props;
User(this.name, this.age);
}
Now when you create a new user object you can access its public attributes with the dot notation
final user = new User("john doe", 20); // the new keyword is optional since Dart v2
// this works
print(user.name);
print(user.age);
// this doesn't work because user is an instance of User class and not a Map.
print(user['name]);
print(user['age]);
For more about the new keyword you can read the v2 release notes here.
Let me be more specific here: This is used in Unity 2017 so the syntax they are using is this:
class CameraMotionBlurEditor extends Editor
{
var preview : SerializedProperty;
var previewScale : SerializedProperty;
...
function OnInspectorGUI () {
if (preview.boolValue) dosomething()
}
}
What I'm getting errors in is this preview.boolValue reference.. it claims it's ambiguous so therefore whatever this class is extending, must also have a declaration of that variable name. What I don't know is how to specify the local one.
The this keyword is used to refer to the current instance of the class. Retrieving the preview.boolValue from the current instance of the class hence becomes this.preview.boolValue:
function OnInspectorGUI () {
if (this.preview.boolValue) dosomething()
}
Note that UnityScript is slowly becoming deprecated, and the recommended route of action is to instead program Unity scripts in C#.
I'm attempting to create some ui elements in my F# script that is attached to a camera in Unity 3d. I know how to do this in C# but I'm not quite sure how to do it in F#.
Here's a simple C# example:
using UnityEngine;
using UnityEngine.UI;
public class CameraController : MonoBehaviour {
public Text uiText;
void Start ()
{
uiText.text = "Hello world!";
}
void Update ()
{
}
}
Now, here's my F# code. I've added comments that displays the compiler error I get when attempting to create a text element, This type has no accessible constructors.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
member this.uiText = Text // Compiler Error: This type has no accessible constructors
member this.Start() =
let uiText = Text // Compiler Error: This Type has no accessible constructors
()
member this.Update() =
()
It seems like F# implicitly calls a constructor method on Unity's Text object.
So, how can I create a ui text element in an F# script? Thanks for any and all help.
Kurt
In your C# code, the uiText member is getting initialized with null, which is the default for C#. In F#, there is no "default", you have to write the initial value explicitly:
member this.uiText: Text = null
This will compile, but won't give you the desired result. This syntax will create a read-only property that always returns null. In order to create a read/write property with initial value, you have to use member val:
member val uiText: Text = null with get, set
The 'get, set' part in there serves for clarification that you want the property to be readable and writable. Without it (by default), the property will be read-only.
For a complete description of how properties work in F#, see MSDN.
I ended up using a mutable let binding that specifies it is of type Text and its initial value is null. I then added the SerializeField attribute in order to associate UI Text elements in the Unity editor with the script.
Below is a simple example.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
[<SerializeField>]
let mutable uiText : Text = null
member this.Start() =
uiText.text <- "hello world!"
()
member this.Update() =
()
I'm trying to subclass the native JS Error object in CoffeeScript to get specialized error types, but i found that the instanceof does not work correctly if i don't define a constructor in the subclasses:
class SimpleError extends Error
class EmptyConstructorError extends Error
constructor: ->
class SuperConstructorError extends Error
constructor: ->
super
new SimpleError instanceof SimpleError # -> false
new EmptyConstructorError instanceof EmptyConstructorError # -> true
new SuperConstructorError instanceof SuperConstructorError # -> true
The problem seems to be caused by how the generated JS constructor functions are defined. When i don't define a constructor in CoffeeScript:
SimpleError = (function(_super) {
__extends(SimpleError, _super);
function SimpleError() {
return SimpleError.__super__.constructor.apply(this, arguments);
}
return SimpleError;
})(Error);
And when i do define a constructor in CoffeeScript:
SuperConstructorError = (function(_super) {
__extends(SuperConstructorError, _super);
function SuperConstructorError() {
SuperConstructorError.__super__.constructor.apply(this, arguments);
}
return SuperConstructorError;
})(Error);
As you can see, the difference is a simple return in the first case. I don't understand why this makes any difference in the instanceof behavior though, as the super constructor is just being applied to the this object (i.e. the super constructor is not being called with new), but then again i don't understand a whole lot of how JS constructors work =P
And the weird thing is that this behavior seems to only happen when subclassing native JS objects. If i subclass CoffeeScript classes everything works as expected.
Any idea of why this might be happening and how could i avoid writing dummy constructors just for the instanceof operator to work correctly?
Thanks!
Update
So the user matyr answered with a link to the commit where this behavior was introduced, but it doesn't quite explain what is happening here, so i'll try to explain that a little bit in case anyone else wonders why this works this way.
The main problem is this inherited nasty "feature" from JavaScript which let us define a constructor function that returns an object other than the one being constructed:
function Foo() {
return {'LOL': 'You fool!'};
}
new Foo() instanceof Foo // -> false
And there is also the fact that some native constructors, like Error, Array, String and whatnot don't need to be called with new: they will just return a new object of the corresponding type if you happen to forget it.
In the end, add these two ugly things together and the result is that you should remember to write class MyError extends Error then constructor: -> super instead of the more intuitive class MyError extends Error if you want the instanceof operator to work properly with MyError. That's because CoffeeScript's implicit constructor will just return whatever the parent constructor returns, and in this case will do return Error.apply(this, arguments) which will just return a shinny new error object instead of the object you passed as the this argument. Yay!
Update 2 (Feb 25 2013)
This problem was fixed in CoffeeScript 1.5.0! =D
Now extending native objects works as expected:
class MyError extends Error
new MyError instanceof MyError # -> true :)
Update 3 (Mar 04 2013)
Aaand it's gone on 1.6.0 =P
For better or worse, the return was added on 1.3.1 to fix #1966 (and #2111).
I have defined a class as
package telmate.com.audioB.volume {
import flash.display.MovieClip;
public class Volume_Bar extends MovieClip {
public static const BAR_WIDTH = 20;
public function Volume_Bar(op: Number, vol: Number) {
alpha = Util.clamp(op);
volume = vol;
}
private _volume:Number;// do we even need to store this?
public function set volume(v: Number){
_volume = v;
var f:uint = Util.clamp(v * totalFrames, 0, totalFrames - 1) + 1;
gotoAndStop(f);
}
}
}
and I am getting two errors: I am calling the constant BAR_WIDTH and instantiating it with parameters - new Volume_Bar(op, vol) -- and getting
/Users/dave/Documents/Audio/telmate/com/audioB/Audio_Bars.as, Line 152
1136: Incorrect number of arguments. Expected 0.
and
/Users/dave/Documents/Audio/telmate/com/audioB/Audio_Bars.as, Line 156
1119: Access of possibly undefined property BAR_WIDTH through a
reference with static type Class.
Why would this be?
Unfortunately, I don't know how specific I can be here, but...
That first error message indicates that when you're calling a function, you're passing arguments, but the function is not set up to handle arguments. Have you adjusted either the function itself, or the line of code that is calling it? (If you're calling through an event listener, be sure to include an argument in the function to hold the referring event. See documentation.)
What specifically is on Audio_bars.as, Line 152? That's where the error is occurring.
The second error seems to indicate that you haven't declared a function/variable by the name of "BAR_WIDTH". In reading your code, there doesn't appear to be an error in the declaration. Thus, you may be having a weird issue I've had before.
Hope that helps!