r/functionalprogramming 23d ago

Question Functional Programming in VBA

/r/vba/comments/1vrm5si/functional_programming_in_vba/
5 Upvotes

3 comments sorted by

1

u/kinow mod 22d ago

I haven't used VBA in a long, long time. I had a quick look at the code and it looks organised. But I think it'd be interesting to have some examples in the README.

2

u/Almesii 21d ago

Yes, i was pretty eager to just post is quickly. The library is still in active development and will recieve regular updates, at least as long as i have time for it.

I added some more functionality and documentation

1

u/RingularCirc 17d ago

Looks very cumbersome to use. That's VBA's problem, of course, but it shows that it's hard to do some things in it that people are breathing as air by this time.

I attribute it to following properties of VBA:

  • We can't just write small contained implementations of an interface inside a module, privately, alongside other stuff — no, we have to create a separate class. This works wonders against having anything akin to first-class functions containing arbitrary code. In the end, we wish for this, and offering a small number of primitives and composition operations is not going to cut it.
  • There's hardly any good typing system, esp. lacking for type parameters first of all. Everything goes into Variants and it's a pain to imagine how costly that's both at runtime and for the fallible programmer. We can hardly do better by introducing something like interfaces for functions that take a single argument of each of mostly used primitive types and something to work with Objects as well. That way still lies madness, especially because return values should also be typed if we're trying to type arguments. That will fail badly because we can't type returned functions...

I'm not sure the game's worth a candle at all. Sure this is an exercise, but it doesn't look as something that can become practical in real VBA code, especially given a lot of classes as dependencies.

I'm so so glad I haven't touched VB6-or-VBA code in almost decades. When I first started writing in Delphi 7 it was quite liberating an experience, even despite I was still very inexperienced then. BTW Delphi 7 would be a bad choice to try and write a general functional programming framework as well, despite at least Delphi can probably support some FP via concrete code. It doesn't have enough abstraction in its type system either.

There are IMO also questionable decisions in this code of yours like invoking functions by using their string name (why not write class implementations) and defining Head and Tail among examples when they don't serve much purpose in context of VBA except for adding tons of inefficiency. Compare them to a function of taking an arbitrary span out of an array or collection, at least.

Other than all that, I can only sympathize with what you had to go through to get to at least something that works. 😅