Suggestions on syntax to express mathematical formula concisely

If you’re going to be writing this for the ab-initio world (which I’m guessing from your MP2 equation) you want to make it very easy and clear to express things as close to the mathematical definition that you can. For one, I wouldn’t have the complicated range function. Have it define a loop, but if … Read more

Scala’s .type and Java’s .class literal

Here is my rationalization: classOf[T] classOf is defined in Predef as a function with this signature: def classOf[T]: Class[T] Although it’s implemented by the compiler, using the function syntax is possible without having to create any special treatment in terms of syntax. So that’s one reason here to consider this option. The alternative like String.class … Read more

What is the rationale for not having static constructor in C++?

Using the static initialization order problem as an excuse to not introducing this feature to the language is and always has been a matter of status quo – it wasn’t introduced because it wasn’t introduced and people keep thinking that initialization order was a reason not to introduce it, even if the order problem has … Read more

Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more

Why are regular expressions greedy by default?

Hysterical Raisens Part of the answer may involve the origins of REs in practical computing. They were originally a theoretical concept from automata theory and formal language theory until Ken Thompson himself wrote a real implementation and used them in qed and ed(1). The original version had only the greedy syntax and so there wasn’t … Read more

How to implement Swift-like enums with associated values in JavaScript?

You can use a discriminated union for this. This example uses Typescript, but the concept would be similar for Javascript just without the type safety. interface UPCA { kind: “UPCA”; numberSystem: number; manufacturer: number; item: number; checkDigit: number; } interface QRCode { kind: “QRCode”; data: string; } interface Other { kind: “Other”; } type Barcode … Read more

Why does !new Boolean(false) equals false in JavaScript?

new Boolean(false) returns an object. All objects (except document.all in browsers) are truthy. As a result, ! of any object will always be false. To prove it to yourself, you can run this in your JavaScript console: (typeof new Boolean(false)) // “object” Also, you can use the strict equality operator === to confirm that new … Read more

How do multimethods solve the namespace issue?

Dynamic dispatch and namespace resolution are two different things. In many object systems classes are also used for namespaces. Also note that often both the class and the namespace are tied to a file. So these object systems conflate at least three things: class definitions with their slots and methods the namespace for identifiers the … Read more

Advantages of Java’s enum over the old “Typesafe Enum” pattern?

“cannot be subclassed” isn’t a restriction. It’s one of the big advantages: It ensures there there is always only ever exactly the set of values defined in the enum and no more! enum correctly handles serialization. You can do that with type-safe enums as well, but it’s often forgotten (or simply not known). This ensures … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)