-
Notifications
You must be signed in to change notification settings - Fork 2
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
Defaults for base types #23
Comments
For parameters, variable declarations, and destructuring that makes sense. Because: function doSomething(s, n, b) {
if (!s) { /* s wasn't passed*/ }
if (!n) { /* n wasn't passed */ }
if (!b) { /* b either wasn't passed, or is false */ }
}
doSomething(); // equal to doSomething(undefined, undefined, undefined)
// with defaults
function doSomething2(s = "", n = 0, b = false) {
if (!s) { /* s wasn't passed*/ }
if (!n) { /* n wasn't passed */ }
if (!b) { /* b either wasn't passed, or is false */ }
}
doSomething2(); // equal to doSomething2(undefined, undefined, undefined) Basically, However, your second example is incorrect (and might be invalid sytnax?): var obj = {
s string,
n number,
b boolean
};
// desugars to:
var obj = {
s: s,
n: n,
b: b
}; I'm not sure what to do about that. One possibility is: // desugared:
var obj = {
s: (s !== void 0 ? s : ""),
n: (n !== void 0 ? n : 0),
b: (b !== void 0 ? b : false),
}; I'm on the fence regarding this, we'll have to see what other people have to say about it. |
Oh I forgot about this new ES6 feature: var s;
var n;
var b;
var obj = {
s,
n,
b
};
// goes to
var obj = {
s: s,
n: n,
b: b
}; Without that it wouldn't be a problem... I wonder if providing a type-hint should be different than the above. Because this would not make any sense: var s boolean;
var n number;
var b string;
var obj = {
s string,
n number,
b boolean
}; The types wouldn't match. So providing a type-hint should probably be a shorthand for: var obj = {
s string: "",
n number: 0,
b boolean: false
}; It's kind of an odd situation. But I think that makes sense to me. |
I think I disagree. I think the TypeHints should always be on the LHS of an var obj Object = {
s, n , b
}; |
Not sure what your If we're on the same page with that: So are you saying both of these should be invalid? var obj = {
s string,
n number,
b boolean
}; var obj = {
s string: "",
n number: 0,
b boolean: false
}; If so, here's the issue: There are a lot of parallels between an a JavaScript object and a class: class Foo {
doThis() {
}
} var foo = {
// short-hand method is valid in an object too!
doThis() {
}
}; And if a class ends up using a class Foo {
s string: ""
n number: 0
b boolean: false
} Also, people still use JavaScript objects for class-like things: var Foo = Object.freeze({
// add some type hints without rewriting code:
s: "", // -> s string,
n: 0, // -> n number,
b: false, // -> b false,
init: function() {
return Object.create(this);
},
doThis: function() {}
});
var foo = Foo.init(); So being able to add type-hints to existing code would be very helpful. Flow assumes that |
I see what you're saying. I'm just really on the fence about it.
At the moment, I'm thinking they're both syntactically valid. For consistency with classes, it makes a lot of sense. var x Hint = 1; // makes sense
var x Hint = {}; // makes sense
class A {
x Hint: 1; // Makes sense
};
var O Hint = { // makes sense, consistent with other vars
x Hint: 1, // makes sense, has code smell, but same as class (aside from semicolon vs comma)
};
var O Hint = {
X Hint, // This throws up warning bells for me, depending on the semantics.
}; I have a problem with the default expansion: var x bool = 1;
var o = { x string };
// is shorthand for
var o = {x string: x };
// NOT
var o = {x string: "" }; In that case, I think the
Good point, except it should be made for There's also this case we should consider: let o = { A: undefined };
let { A string } = o; // What is the value of A?
// similarly:
function f(arg string = undefined) { }
f(); // what is arg? |
Another inconsistency with this, is usage with non-base types: class A {}
let x A; // what's the default value here? |
See, I think there is a big difference between this: var obj = {s}; And this: var obj = {s string}; The first one is a short-hand of: var obj = {s}; Which is assuming you have a The second one, var obj = {s:""}; I think adding a type-hint should negate that shortcut, especially since you won't be able to do that in a class. Also, this is invalid: var s number; // default;
var obj = {s string}; There is a type conflict there. That's why If we decided that you can't put type hints on var obj = {
getValue() string {
return "";
}
}; A method on an object, which looks just like a method on class, can have a TypeHint on it. Why not a property at this point? Another way to resolve the LHS var obj = {
s string = "" // require = with a typehint
}; The only thing I don't like about that is it's no longer JSON. But then again, JSON is very different than js objects:
We're just hinting & providing defaults. So yep, no error throwing :)
The default of any class (non-base type) is |
Okay, let's run with it as is. I have a feeling that people will complain about the I'll try to get some of the static semantics done when I have some time. |
What would the default type for |
It would be undefined. That's the default when function arguments are omitted. |
I'm not sure this can be done for object literals. I'm reading through the syntax for the object initializers, and it assumes: let o = { Identifier }; // It assume "Identifier" is the value, not the key
// desugars to:
let o = {}
o[%StringValue(Identifier)] = Identifier; Making it desugar to modify the RHS of the assignment seems like a much bigger than we should be doing... |
Isn't However, something like this: let o = { Identifier string }; // "Identifier" is key, not the value
// desugars to:
let o = {
Identifier: ""
}
// not sure what goes here...:) |
My example is what the spec currently does: http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer-static-semantics-propname What you specify in the initializer is what the value is, not what the key is. |
Right, what I'm saying is: var Identifier = "foo";
// ES6 code:
var o = {Identifier};
// Desugars to ES5 code:
var o = {Identifier: Identifier};
// Which can be thought of as:
var o = {Identifier any: Identifier};
// logs as:
console.log(o); // {Identifier: "foo"} Is different than: var Identifier = "foo";
// ES6 code:
var o = {Identifier string};
// Desugars to ES5 code:
var o = {Identifier: ""};
// Which can be though of as:
var o = {Identifier string: ""};
// logs as:
console.log(o); // {Identifier: ""}
// Which means if you want to use "var Identifier" you need to do (explicitly):
var o = {Identifier string: Identifier} Which would be consistent with class properties. I suppose |
Yeah, I think I can get it to work by changing the semantic part of the
|
@lukescott, Just made an attempt at allowing default values (#44).. and it's really messy. I definitely need help from someone who actually knows the spec more intimately than me. |
We need to put somewhere in this spec that base values provide defaults if not specified.
For example, the following code:
Should desugar to:
That way you don't end up with undesired behavior with
undefined
.The text was updated successfully, but these errors were encountered: