Hacker News new | ask | show | jobs
by mannykannot 1949 days ago
This language clearly avoids some of the run-time errors that can occur with C, but I would like to learn a little more about the remainder. For example, if you make an out-of-bounds assignment to an array, the array is grown to accommodate it (for +ve offset only, I assume) - but what about out-of-bounds retrieval?
2 comments

You get undef, that's part of the reason we added an undef concept (it's a value that isn't a value, though if you treat it like an int I believe it is zero, like a string and you get "").
I think that is a giant design mistake since you are pushing the discovery of an error to some place else in the program, potentially very far from where the error occurred.
Without providing an example of how to do it better, I'm not sure I see your point. Yeah, I see the problem you point out.

Consider

char *p = 0;

some complicated code that should have set p but didn't...

char c = *p; // SEGV

So do you have a way to push that error all the way back to wherever you think it should be pushed?

The person you replied to asked what the behavior was when you access an offset of an array that doesn't exist and you are giving me an example of dereferencing a null pointer.

The array access should error out on the line that it happens.

The dereference could print a line of the last assignment, but this isn't what was being talked about.

I don't see the array access as any different than a null pointer dereference, in both cases you are asking for something that isn't there.

In Tcl, stuff just gets auto expanded on assignment and returns "" (I think) on dereference. We chose to return undef. I'm not sure what the problem is.

I explained the problem already (the error does not show up where it actually happens).

I also explained a huge difference between the two already (you know how large an array is and can detect an out of bounds access when it happens).

So there's `undef` and `null`? I wonder if there's a popular language that made the same mistake you could have learnt from :-P
There is no null, only undef. It's not a value, there is no value that means undef. We implemented by stealing the high order bit from the reference counter that tcl uses for garbage collection. If it is set, the variable undefined, if it isn't, it's a normal variable.

I thought that was clever given that tcl's reference counter was signed and it only uses positive values. So we made it unsigned and got the bit for free.

What happens if you call a Tcl function with an undef argument? Is this running Tcl code on a slightly different runtime, or do they interop in some other way?
In Tcl, everything is a string. The string rep of undef is "" so Tcl gets "" but doesn't know that it is undef.

So yeah, there is some sloppiness there. Tcl tends to use "" as sort of a null or undef so lots of stuff just works but no promises on that.

This is sloppy simply because the Tcl die hards (are there any left?) refused to see the value of undef, a value for a variable that said there is no value. We used it all over the place, there clearly is value. They didn't see that, Little never got pushed back into the Tcl source base so Tcl never thought about undef.

It is what it is, we live in an imperfect world. I tried.

What would you want a language to do if you try to access an array offset that doesn't exist except for give you a clear error?
You are kind of making my point. In tcl, they'd just give you "", but you can't tell the difference between "past the end of the array" or an element where you said

set foo[i] = ""

In Little you can tell, we'll return undef (your clear "error" though in these languages it is a supported feature, not an error). So we support the auto expanding array but give you that extra bit of info that you are past the end.

Well, it's that line of thought that prompted my question. It is, of course, a well-known source of problems in C that it will uncomplainingly dereference an invalid pointer.
That's actually C avoiding a run time error which is the opposite of what you asked.
You are right, it is avoiding a runtime error, but there have been, and continue to be, many cases where it leads to one. I suppose am asking about things that are undefined behavior in C.