-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|622|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Misc


 
Pixel_Outlaw
Created : 18 June 2025
Edited : 18 June 2025

[BMAX] - Generics, Function References, Boxing, Type Extension



You will need BlitzMax NG for this.

This shows how to extend a Type in Blitzmax and add additional interfaces using function references.
I would have papered over the superclass' methods but BlitzMax wasn't having the method signature change...

It also shows off how to use class generics.
Finally, I turn what would be 2 for loops into a single chained command using the new more functional interfaces.

I've been sick and tired trying to hunt down some of these features on the missing documentation so I've done so here in 1 example.



 

Comments


Friday, 20 June 2025, 01:30
realtime
This seems like a boxing and unboxing technique to add functionality to list.

I am not a blitzmax user, but it looks nice.

In java and javascript we already have this builtin.
Friday, 20 June 2025, 01:46
Pixel_Outlaw
You're exactly correct.
I implement a box type which can handle primitives or objects.
Then use the usual object list to store these.

One of the big problems with BlitzMax is that there's a dichotomy of objects and primitives such that many of the algorithms and containers won't work with simple data floats doubles etc. All types share Object as their ancestors so they can be used anywhere an Object is. But as numbers are not an object and do not inherit from them they may not be used in those cases and that's why I had to set up a boxing type which can accept both thanks to the new Generics.

Some languages solve this in an elegant way imposing objects all the way down you see this is Smalltalk for example where even numbers are objects and therefore can even have methods. The price is speed.
Friday, 20 June 2025, 03:36
realtime
That's the problem I see with basic language. It slowly fade away. We are strugle with adding new feature to the language.

Even for education, people prefer to introduce python and javascript rather than basic.

We are lucky there are still strong communities that want to maintain this language.
Friday, 20 June 2025, 03:48
Jayenkai
Everything's just 1's and 0's at the end of the day. Code whatever way is best for you.
In my case, I actually struggle with Object Orientated stuff quite a bit, but that's mostly because I've really not found a good reason to do most of it, and .. if I'm honest.. It's been "Bad" examples that have caused most of my issues.

One example I clearly remember involved having a list of enemy invaders in a very basic (they don't move out of the grid) Space Invaders clone, where everything moved based on a "simple" Enemies.Move(speedX,speedY) command.
Sure.. OK..
But then when it came to colliding the bullet with the enemies, the code had to go through this long and tangled mess of checking each bullet against each enemy.

But if they'd have made it an array of enemies, they could've just plucked off the bullet-to-grid with a teensy bit of maths and destroyed "the enemy at grid x,y", without needing to iterate through every single possibility.

That was, to me, a really bad example, and I knew when I read it that it was bad, but ever since then when I've seen things like you've posted, all I can think is "but it still has to run through the whole list like an array, so why not just make it an array..?"

IMHO it's more about "cleaner code" than it is "more optimised code", and personally I'd rather optimise than write neater.
I'm a very messy writer.
Friday, 20 June 2025, 11:37
AndyH
Yea. That doesn't sound like an oop example. Arrays and lists and other data types and structures still exist regardless of oop.

But it's not surprising, like most new stuff in tech, there was a cult-like phase when it began to get popular and there was plenty of bad behaviour going on, and big books written about it.

I'm out of the loop on this, what is blitz max NG?
Friday, 20 June 2025, 13:10
Pixel_Outlaw
Just for the record, the real meat of the demo was passing functions by reference.
I wanted to show how a lot of languages bypass loops altogether when working with collections.

The other stuff was a huge amount of scaffolding to create the mechanisms in BlitzMax NG.
But once that scaffolding is in place, it can be used again and again (even twice below as shown) making code tighter with each usage.

On thing that BASIC traditionally has not done well is work with collections. (This can be fixed, if the language designer wants to by supporting either function references or first class functions)
If I had more time and was in the mood I'd implement a huge amount of handy interfaces over BlitzMax' collections.
You're always looping and it's not necessary in many cases.

You want a general way to say "do <function>" to each member in the collection.
That's what the demo is truly about. Below we can simply /tell/ what we want done to the collection and we can chain them together.

If nothing else, take away that you may pass functions as functions into parameters.
This allows the programmer to write only a basic form of something and the next programmer provides his own function to build the rest of it from OUTSIDE.

As an example if you look at the methods I've added I write a general way to loop over something and apply a function to each member then and return it.
I have no idea what you might want to do to your collection so I leave that open, leaving you a parameter to apply to the entire thing when I'm gone.

This inverts control, I capture the structure or motion, and YOU finish my function by passing whatever function you like in as a parameter.

For example if you had an Enemy type (or number or whatever) in that collection above and wanted to update all of em:

It would turn into something like:


Where is the loop addressing each member?
It's been done implicitly in RBMap and I left it open for your update function.
This is a generalized way to work with containers.

But even beyond containers, when I can specify how something works and leave the final piece up to you as a programmer it's a very powerful thing.
I write the general form of something, and your function reference specializes it such that you get benefit without me tweaking my code.
It's more powerful when the language is dynamically typed rather than statically typed. But even in statically typed languages we can do a subset of it like above.
Friday, 20 June 2025, 14:01
Jayenkai
BMax NG is a community update to BMax, which may or may not function as you'd expect.
IIRC, it only has Strict mode? Or was it that it defaults to Strict mode?
I can't remember.
Friday, 20 June 2025, 16:43
Pixel_Outlaw
I was able to successfully recompile a game from something like 15 years ago.
Some of the math modules have been moved around but I think the brl stuff is still intact.

I would say it has a very high degree of backwards compatibility as far as the language goes if not perfect backwards compatibility. I'm not counting external modules unless as part of the language.

I have always used SuperStrict so I don't know for sure...
I'm not advocating necessarily for Blitz Max NG. But in the realm of Blitz products it's the only one that's going to let you do the example above.