Safer Arrays

56 views
Skip to first unread message

Hitmark7

unread,
Feb 3, 2016, 7:29:16 AM2/3/16
to Haxe
Hello everybody.

Haxe newbie questions here.

This morning I've found myself implementing a wrapper for the haxe.ds.Vector class, to get rid of all the "if x, the result is unespecified" entries on the API docs. Throwing an exception feels far better than this for me. It's the same for Array, Map, and other similar classes.

Am I overlooking something important? Why have it been implemented like this in the first place?

Thanks!

Simon Krajewski

unread,
Feb 3, 2016, 7:49:17 AM2/3/16
to haxe...@googlegroups.com
Because

* throwing exceptions requires branching,
* catching exceptions (especially specific types) is expensive and
* Vector is supposed to be fast.

If you don't care about performance then Array might be a better choice.

Simon

Hitmark7

unread,
Feb 3, 2016, 10:50:56 AM2/3/16
to Haxe
Hi again, Simon. :)

I see your point. I suppose it's important to get as fast as possible on some scenarios, but I believe that a consistent cross-target behavior it's very important, too. It would be fine if exists a library with some wrappers. It just seemed me so weird reading "if x, the result is unespecified" so many times.

David Holaň

unread,
Feb 3, 2016, 11:16:21 AM2/3/16
to haxe...@googlegroups.com
Hello Simon,

* throwing exceptions requires branching,
True, but
* catching exceptions (especially specific types) is expensive and
I think these exceptions would be uncaught, caught by crash reporter or caught by debugger.

* Vector is supposed to be fast.
Could be a -debug only feature. Maybe it already is? It fits "unspecified" behavior :]

-David Holan

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Juraj Kirchheim

unread,
Feb 3, 2016, 12:25:21 PM2/3/16
to haxe...@googlegroups.com
On Wed, Feb 3, 2016 at 4:50 PM, Hitmark7 <hitm...@gmail.com> wrote:
Hi again, Simon. :)

I see your point. I suppose it's important to get as fast as possible on some scenarios, but I believe that a consistent cross-target behavior it's very important, too. It would be fine if exists a library with some wrappers. It just seemed me so weird reading "if x, the result is unespecified" so many times.

The whole point of Vector is to give away some consistency to get native behavior (which tends to result in better performance). You should use Vector if and only if that is what you need and Array otherwise, as Simn pointed out.

As David proposed, for debugging purposes it is no doubt useful to have a drop-in replacement that does bound-checks to make it easier to track errors caused by out-of-bound access. But you should simply not write code that *depends* on Vector throwing exceptions.

Best,
Juraj
Reply all
Reply to author
Forward
0 new messages