-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Sugestion: A way to declare Non Sparse Arrays #24752
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
Comments
I think the vast, vast majority of arrays are non-sparse; it seems like what you'd actually want is for the default Array behavior to be like this and need to opt in to sparseness. |
@RyanCavanaugh, I agree with you. But I think maybe changing that is a breaking change. Thats why I suggest a way to be more specific. A way to say "this array is non sparse". That change would not bother me. But I do not know in which cases a breaking change is allowed. |
The compiler today does not have any reasoning about array bounds. Its is not clear if there is a way to plug this into the current system. |
Do you want to label this issue to be a good idea for the future? In special if you think that is need to enance array and tupple treatament. For example: var e1 = a[a.length-1];
var e2 = a.pop(); // e1 and e2 points to the same object both points to the same object. Or in a tuple case: function a3(a:[T, T, T], b:T){
a[7] = b;
} the assigment inside the function does not warns the error. After that statement I think than in the future there be the need of analyze all the array and tupples stuffs. Please save this issue for that moment. |
The issue will always be there. we do look at closed issues every now and then. |
I agree with you. Thank you |
I was playing with Perhaps the problem is not being able to specify the result type of the object to which a method has been applied. For example: var server = new Server();
await server.listen();
// ...
await server.close(); The variable Then if we can do that, we can have betters arrays. new Array<T>() => Array<T, 0>
var x:string[] = [] // x is of type Array<string,0>
x.push('hi') // x is of type Array<string,1>
function chop<T, n>(a:Array<T,n>, e:T){
if(a.length && a[0]==e){
return a.shift();
}
}
var line = readline()
chop(x, line); // after chop x is of type Array<string,1> | Array<string,0> May be we can use a symbol chop: <T,n>(arr: Array<T,n> ~> Array<T,n|n-1>, element:T) => T | undefined; // chop modifies arr
listen: () => Listener ~> ServerListening // chop modifies this It is too much? I am sorry |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Search Terms
non sparse array.
(I read and understand (I supouse): #10272 #13778 )
Suggestion
Add a way to declare Non Sparse Arrays to take adventajes of type guards to have these adventajes:
It could be something like:
type a: NonSparse<ElementType[]>
ortype a:NonSparseArrayOf<ElementType>
Use Cases
Knowing and wanting that an array haves non undefined values.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: