You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this is rather subtle. looking at it i would not guess, and if i did it by mistake i would not be able to see why it is working any differently.
For a while contextual type did not go through parenthesized expressions, and ppl tripped over it consistently.
We did at one point consider the ([x, y]) syntax for tuples, but as Mohamed Hegazy (@mhegazy) says, we thought it too subtle. Someone looking at ([x, y]) would have absolutely no intuition that it produces a different type than [x, y].
FWIW I kind of like Aleksey-Bykov proposal.
I would have the intuition that ([x, y]) is different from [x, y] as it looks similar to an arrow function with a concise body returning an object literal:
var f = x => {}; // returns void var f = x => ({}); // returns {}
With the proposed ([x, y]) syntax:
var f = x => ['hey', 1]; // returns (string|number)[] type var f = x => (['hey', 1]); // returns [string, number] type
I don't like the analogy with the arrow function returning object literal syntax because that syntax itself is kind of a hack to get around an issue with how arrow functions are specified in ECMAScript.
That said, it's a useful hack but it has no impact on the type of the object literal, it has an impact on the type and value of the function, and no one writes them in parentheses except to return them from expression bodied arrow functions.
of which, better than nothing means there is no better syntax for object literal results in lambdas (aside from the statements), and this is something no one can change anymore, despite being ugly, it works for a lot of people
we've seen it, we hated it but we ate it, and it worked out
Aleksey-Bykov I think where the analogy breaks down is that { value: x } and ({ value: x }) have the same type, whereas [x, y] and ([x, y]) would have different types with what you're proposing.
Anders Hejlsberg (@ahejlsberg), I admit, the syntax is not ideal, and what you said is definitely a weak point of the idea, at the same time it has a number of strong points, as far as i can tell, that were also mentioned
to my defence: the fact that ([]) and [] currently mean the same is a waste of valuable syntax, because you don't usually see ([]) in the wild, all I am saying is that it might get a better use
Aleksey-Bykov I was objecting to the analogy for the exact reasons that Anders Hejlsberg (@ahejlsberg) mentioned. I was not objecting to the work around syntax for returning object literals from expression bodied arrow functions.
I think it's a shame that expression bodied arrow functions require a kludge to work with object literals but the syntax could be worse.
After re-reading my comment I realize I could have been clearer. I was indeed saying that it's not great to follow the precedent of a syntax that is a hack in the first place.
I was also trying to get across that the use of parentheses is completely different in that context because it alters the function returning the value, not the value itself.
I know about "not playing at the expression level syntax" rule...
but just as a fantasy:
x=<>[1,'a',true]// or [ 1, 'a', true ] as <> - type strongifierx= #[1,'a',true]// - AFAIK # now is invalid symbol. treat #[ - as a single delimiterx=[: 1,'a',true :]// symmetric smiley brackets
Activity
blendsdk commentedon Jun 16, 2016
would this help?
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
this is stated as a problem that is sought to be solved
meaning that you example only works thank to explicit type declaration which i wish could be avoided
blendsdk commentedon Jun 16, 2016
Ah..... I misread the question. I am inclined to say
type tuple = Array<any>but then it is not a tuple anymore.mhegazy commentedon Jun 16, 2016
we have talked about this before, we could not come up with a proposal that works and looks right.. some of the options considered
var x = [| 1, 2 |]var y = tuple(1,2)var z: [...] = [1,2]nothing seemed palatable to be frank.
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
why would not this work:
(['hey', 1])?x => ({value: x})mhegazy commentedon Jun 16, 2016
this is rather subtle. looking at it i would not guess, and if i did it by mistake i would not be able to see why it is working any differently.
For a while contextual type did not go through parenthesized expressions, and ppl tripped over it consistently.
ahejlsberg commentedon Jun 16, 2016
We did at one point consider the
([x, y])syntax for tuples, but as Mohamed Hegazy (@mhegazy) says, we thought it too subtle. Someone looking at([x, y])would have absolutely no intuition that it produces a different type than[x, y].zpdDG4gta8XKpMCd commentedon Jun 16, 2016
my few centes, speaking for myself:
([])rather than[]looks strange to me, so it's a red flag anywaydsbl41 commentedon Jun 16, 2016
FWIW I kind of like Aleksey-Bykov proposal.
I would have the intuition that
([x, y])is different from[x, y]as it looks similar to an arrow function with a concise body returning an object literal:var f = x => {}; // returns voidvar f = x => ({}); // returns {}With the proposed
([x, y])syntax:var f = x => ['hey', 1]; // returns (string|number)[] typevar f = x => (['hey', 1]); // returns [string, number] typentrrgc commentedon Jun 16, 2016
In my projects I use something like this:
The usage is very simple and explicit:
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
Alicia Boya García (@ntrrgc) this is my current workaround: #9216 (comment)
aluanhaddad commentedon Jun 16, 2016
I don't like the analogy with the arrow function returning object literal syntax because that syntax itself is kind of a hack to get around an issue with how arrow functions are specified in ECMAScript.
That said, it's a useful hack but it has no impact on the type of the object literal, it has an impact on the type and value of the function, and no one writes them in parentheses except to return them from expression bodied arrow functions.
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
like it or not:
so it's easier to get along with it than deny
mhegazy commentedon Jun 16, 2016
i disagree with the "better than nothing". i still find this fairly confusing, let a side undiscovered.
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
Mohamed Hegazy (@mhegazy) i was talking about
x => ({ value: x })syntax that is used for returting object-literals in lambdas that Aluan Haddad (@aluanhaddad) happened not to likeof which, better than nothing means there is no better syntax for object literal results in lambdas (aside from the statements), and this is something no one can change anymore, despite being ugly, it works for a lot of people
we've seen it, we hated it but we ate it, and it worked out
so there is a precedent
ahejlsberg commentedon Jun 16, 2016
Aleksey-Bykov I think where the analogy breaks down is that
{ value: x }and({ value: x })have the same type, whereas[x, y]and([x, y])would have different types with what you're proposing.zpdDG4gta8XKpMCd commentedon Jun 16, 2016
Anders Hejlsberg (@ahejlsberg), I admit, the syntax is not ideal, and what you said is definitely a weak point of the idea, at the same time it has a number of strong points, as far as i can tell, that were also mentioned
to my defence: the fact that ([]) and [] currently mean the same is a waste of valuable syntax, because you don't usually see ([]) in the wild, all I am saying is that it might get a better use
aluanhaddad commentedon Jun 17, 2016
Aleksey-Bykov I was objecting to the analogy for the exact reasons that Anders Hejlsberg (@ahejlsberg) mentioned. I was not objecting to the work around syntax for returning object literals from expression bodied arrow functions.
I think it's a shame that expression bodied arrow functions require a kludge to work with object literals but the syntax could be worse.
I'm saying the analogy is flawed.
aluanhaddad commentedon Jun 17, 2016
After re-reading my comment I realize I could have been clearer. I was indeed saying that it's not great to follow the precedent of a syntax that is a hack in the first place.
I was also trying to get across that the use of parentheses is completely different in that context because it alters the function returning the value, not the value itself.
Artazor commentedon Jun 23, 2016
I know about "not playing at the expression level syntax" rule...
but just as a fantasy:
zpdDG4gta8XKpMCd commentedon Aug 7, 2016
Closing in favor of #10195