Did you know that ... End/RuntimeError Override

Blitz3D Forums/Blitz3D Programming/Did you know that ... End/RuntimeError Override

Bobysait(Posted 2013) [#1]


I would have thought thoose "function" would be protected from overriding.
Actually it might become a bit unsafe (but Im' maybe the only "stupid" guy who think about this kind of trick ... I assume ^.^)


Captain Wicker (crazy hillbilly)(Posted 2013) [#2]
You can "override" any BlitzBasic command using this method..
In BlitzMax, there are modules for that.


Bobysait(Posted 2013) [#3]
I Know that, but I precised :
I would have thought thoose "function" would be protected from overriding.


Yasha(Posted 2013) [#4]
RuntimeError isn't a surprise, but I never realised End was a function at all. I always thought it was a keyword (like Next or EndIf), not a valid name! I guess there's no reason why it needed to be one though, since it can only appear in call-position.

RuntimeError is more obviously just a function though.

As for why they aren't protected... Why should they be? It's your program, do with it as you will. Not hurting anyone else by doing so (note that since we don't have the BlitzMax module system in B3D, you can't secretly insert these into a project or anything: someone has to explicitly Include every definition).


Bobysait(Posted 2013) [#5]
sure, it's just that like you say, I thought "End" was a protected keyword.
for runtimeerror, it's probably the fact I don't know how low-level this function is, I thought it was really deep incorporated to the blitz3d language (not just a function) Like "Stop" or "Exit" maybe.

Then, according to the fact we are supposed to explicitely include the definitions, we are not supposed to "accept" such modifications, but ... if someone send you a massive library (with multiple includes) you would probably miss it.

-> it's like when blitz-users post some come, for help most of the time, and go for fullscreen with a memory leak or infinite loop that almost crash your PC while you were trying to help. You don't always think about it.

Hopefully, I have not such friends that hide destructive stuff in their programs :)
(Sadly, My friends can't say the same ...)


_PJ_(Posted 2013) [#6]
Overwriting the WaitKey() in the above example will not accomplish much unless it is set to loop or call itself, which would cause a stack overflow in an executable.

Interesting that the Blitz commands can be overloaded though.


Bobysait(Posted 2013) [#7]
"Who would even think about replacing waitkey with an infinite loop ?!
...
No ... mmm .... No ! Don't even think about it."


Well, I already replaced majors blitz-function years ago, for some tricks, but I thought every body knew it here.

Actually, it's very usefull for debugging.
for ex : the writeLine command
You have some stuff to process on a file, but it's a big file, and you want to be sure what will end in the file before modifying it
-> Overwrite the WriteLine/WriteByte/WriteWhateverYouUse functions with
Function WriteLine(Stream,line_$)
Debuglog line_
End Function

It's faster than commenting/adding a DebugLog on each line that process writing to the output.
When you come to something working, just remove the overwriten function and that's finished.

By the way, you can also overwrite the PointEntity function which is reeeally a slow function (or the Trim function which is a bit buged)

we just have to take care of (obviously) not needing the original function inside the overwriten function


virtlands(Posted 2013) [#8]
BobySait is brilliant.

I would never have thought of controlling that aspect of B3D before.

Therefore, you can also override the STOP command; "Debug enabled" would be rendered useless.



Most of the String functions can be replaced too, except that Str$(a) won't allow itself to be replaced.

The following math functions can be replaced too, (there might be more):
Rnd (start#,end#)
Rand ([low value],high value)
SeedRnd()
RndSeed()


Yasha(Posted 2013) [#9]
This is a documented feature: you can replace any function in the B3D library with your own definition.

The part of the topic that was surprising was the news that End and RunTimeError are functions, not special syntax forms like If or EndIf, and that therefore the replacement rule applies to them as well.

Str, Abs, Int, Float, Object, Handle, and several others are not functions, but operators (i.e. the compiler considers them symbols that coincidentally happen to be made out of letters, not valid names), and therefore the rewrite rule won't apply - not because what they do is special, but because it's simply impossible for a function to be given one of those keywords as a name (any more than you could name a function "function": it's a special symbol). An interesting consequence is that they therefore follow the operator rules instead of function rules, and you can use them without parentheses (e.g. 'Str(X)' is the same as '(Str X)' and 'Str X', because 'Str' works like '-').

In general, this happens because B3D doesn't permit overloading, so operations like Handle or Str that need to accept arguments of multiple types can't be defined as functions, because they have no valid type signature: the compiler has to generate special code for each usage site.