excaliburjs / Excalibur

๐ŸŽฎ Your friendly TypeScript 2D game engine for the web ๐Ÿ—ก๏ธ

Home Page:https://excaliburjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tag BoudingBox.getPoints() as a readonly array return

eonarheim opened this issue ยท comments

Discussed in #3007

Originally posted by ikudrickiy April 6, 2024
The question may seem stupid. But I'm not afraid of those ๐Ÿ˜‰

This public function returns the same array referrence for multiple function calls. I can change the array in one place and it will be changed for all the calls. At the same time, if we assume cloning I don't understand the need for a caching mechanism. It looks like a burden in this case. And if there's no caching, cloning seems like a burden.

We can solve this problem on a typescript level turning the function return into readonly array (see this).

there are plenty of places in the codebase that do this, no? would we want to do this everywhere...? i feel like that's going to be difficult to stay consistent on.

(i personally don't see this as much of a problem... adding readonly will help the end user but i think this is just something you need to be aware of when using javascript - the ramifications of mutating received arrays)

@mattjennings there is nothing wrong with gradually adding a readonly tag in all the necessary places. While we don't have such a functionality in javascript, it is present in typescript.