class BaseFunction {
static #allowInstantiation = false;
constructor(...args) {
if (!BaseFunction.#allowInstantiation) {
throw new Error(
"Why are you trying to use 'new'? Classes are so 2015! Use our fancy 'run' method instead!"
);
}
for (const [name, validator] of this.parameters()) {
this[name] = validator(args.shift());
}
}
parameters() {
return [];
}
body() {
return undefined;
}
static run(...args) {
BaseFunction.#allowInstantiation = true;
const instance = new this(...args);
BaseFunction.#allowInstantiation = false;
return instance.body();
}
}
class Add extends BaseFunction {
parameters() {
return [
["a", (x) => Number(x)],
["b", (x) => Number(x)],
];
}
body() {
return this.a + this.b;
}
}
console.log(Add.run(5, 3)); // 8
Post funny things about programming here! (Or just rant about your favourite programming language.)
Oh sweet caffeine, race conditions in the constructor
validators
is a shitty name for something that actually does type conversion.I’m pretty sure this post is designed to kill the soul. I am made slightly worse for witnessing this abortion of an implementation and I will never be quite the same again.
A true FP programmer would make it
apply
instead ofrun
…Ahem, map…
And, of course, everything is a lazy list even if the functions can’t handle more than one element in each list.
OP, what’s your address? I have a “present” for you
It’s just Java
Yep, some code examples from the official documentation. This:
…is syntactic sugar for this:
…which is syntactic sugar for this:
The
printPersons
function looks like this:Basically, if you accept a parameter that implements an interface with only one method (
CheckPerson
), then your caller can provide you an object like that by using the lambda syntax from the first example.They had to retrofit lambdas into the language, and they sure chose the one hammer that the language has.
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
That’s not quite right. In bytecode, lambdas are significantly more efficient than anonymous class instances. So while the lambda implementation is semantically equivalent, characterizing it like you have is reductive and a bit misleading.
Golang also does this, but it’s not classes.
How so?
Golang uses modules, not classes. Each of which may have its own main function.
Huh? Main file? Do you mean main package? A module can contain an arbitrary number of main packages but I don’t see how that has anything to do with this post. Also are you saying modules are equivalent to classes? That may be the strangest take I’ve ever heard about Go.
I meant main function. Oops
Hence, Clojure. It’s not just functions that implement IFn… as the string of “cannot cast to clojure.lang.IFn” errors that I get because I couldn’t be bothered to validate my data’s shape is eager to inform me.
This should be programmer horror
JS disgusts me
Dont look at C++ with std:: function
I think that’s called a functor.
Amazing, lol
What theme are you using, i like it!
Looks like Catppuccin Mocha
Actually now that check it again its not quite right for mocha. But it’s close!
You’re right, the background is too dark. Probably crust instead of base. Maybe it was customised or created improperly.
But I’m fairly confident that the palette is Catppuccin, probably Mocha.
I can confirm it’s Catppuccin Mocha. I am not currently aware of the background color issue, but I’ll look into the matter soon. Thanks for letting me know. Also how dare you. But thanks.
The background is most likely a color that is in the Mocha palette, just one that is intended for dark accents, not regular background.
Thanks!
That’ll be fun in a multi threaded setting!