How to Create an Operator to Implement Error Chaining

Chaining overloaded + operators

Your poly class is missing a copy constructor of the form poly(const poly&). Note that if you already have a poly(poly&) that isn't enough. You need a copy constructor that takes a const poly& as a parameter.

Can't implement simple optional chaining

You can use null coalescing in conjunction with optional chaining:

const updateState = item => ({
userObj.firstName = item?.firstName ?? userObj.firstName ,
userObj.lastName = item?.lastName ?? userObj.lastName ,
userObj.userID = item?.userID ?? userObj.userId ,
});

You could use the spread operator:

const updateState = item => {
userObj = { ...userObj, ...item };
return userObj;
}

Or you can use lodash's defaults() function:

const _ = require('lodash');

const updateState = item => {
userObj = _.defaults(userObj, item)
return userObj;
}

Or... if you really want to mutate the state object, rather than creating a new one and replacing it, roll your own, similar:

const updateState = item => {
for (const key in item ) {
const hasValue = key != null && key != undefined && key != NaN ;

if ( hasValue ) {
userObj[prop] = item[prop];
}

}
}

There is, as they say, more than one way to skin a cat.

[Edited: Add explanation of the spread operator]

The spread operator,

const obj = { ...obj1, ...obj2, . . . , ...objN };

is somewhat akin to calling a function like this:

const obj = mergeObjects( obj1, obj2, . . ., objN );

where mergeObjects() is defined as:

function mergeObjects(...objects) {
const mergedObject = {};

for (const obj of objects ) {
for (const key in obj ) {
mergedObject[key] = item[key];
}
}

return mergedObject;
}

Or perhaps a better explanation might be done using Object.assign(). One could say that an expression like:

const obj = {

prop1: 'a' ,
prop2: 'b' ,

...obj1 ,

prop3: 'c' ,
prop4: 'd' ,

...obj2 ,

prop5: 'e' ,
prop6: 'f' ,

...obj3 ,

}

is the equivalent of this:

const obj = Object.assign( {},
{
prop1: 'a' ,
prop2: 'b' ,
},
obj1 ,
{
prop3: 'c' ,
prop4: 'd' ,
} ,
obj2 ,
{
prop5: 'e' ,
prop6: 'f' ,
} ,
obj3 ,
);

How can I implement a custom error throwing syntax in swift?

You can define a postfix operator which takes a throwing closure as (left) operand. ?? is already defined as an infix operator, therefore you have to choose a different name:

postfix operator <?>

postfix func <?><T>(expression: () throws -> T) -> T? {
do {
return try expression()
} catch {
print(error)
return nil
}
}

Now you can call

let result = throwingFunc<?>

or chain it with

let result = (throwingFunc<?>)?.doStuff()

Previous answer:

?? is already defined as an infix operator. For a postfix operator you have to choose a different name, for example:

postfix operator <?>

extension Result {
static postfix func <?>(value: Result) -> Success? {
switch value {
case .success(let win):
return win
case .failure(let fail):
print(fail.localizedDescription)
return nil
}
}
}

Now you can call

let res = Result(catching: throwingFunc)<?>

or chain it with

let res = (Result(catching: throwingFunc)<?>)?.doStuff()

chaining operator throwing error in express app

You have 2 options

  1. Upgrade your Node version. Only these versions support optional chaining. As you can see, only Node 14.5+ supports optional chaining
  2. If you want to support older versions such as 12, you will need to transpile your code. Take a look at Babel or TypeScript. These programs take your code and transform it into code that is compatible with older Node versions. For example, your code:
if (user.address?.postal_code.length > 0 ) {
// Do stuff
}

Turns into:


var _user$address;

if (((_user$address = user.address) === null || _user$address === void 0 ? void 0 : _user$address.postal_code.length) > 0) {
// Do stuff
}

How to chain and serialize functions by overloading the | operator

First I assume you have some basics that look like this

#include <iostream>
struct vec2 {
double x;
double y;
};
std::ostream& operator<<(std::ostream& stream, vec2 v2) {return stream<<v2.x<<','<<v2.y;}

//real methods
vec2 translate(vec2 in, double a) {return vec2{in.x+a, in.y+a};} //dummy placeholder implementations
vec2 rotate(vec2 in, double a) {return vec2{in.x+1, in.y-1};}
vec2 scale(vec2 in, double a) {return vec2{in.x*a, in.y*a};}

So what you want is a proxy class for operations, where a proxy object is constructed with the function and the "other parameters". (I made the function a template parameter, which prevents the use of function pointers, and helps the optimizer to inline, making this nearly zero overhead.)

#include <type_traits>
//operation proxy class
template<class rhst, //type of the only parameter
vec2(*f)(vec2,rhst)> //the function to call
class vec2_op1 {
std::decay_t<rhst> rhs; //store the parameter until the call
public:
vec2_op1(rhst rhs_) : rhs(std::forward<rhst>(rhs_)) {}
vec2 operator()(vec2 lhs) {return f(lhs, std::forward<rhst>(rhs));}
};

//proxy methods
vec2_op1<double,translate> translate(double a) {return {a};}
vec2_op1<double,rotate> rotate(double a) {return {a};}
vec2_op1<double,scale> scale(double a) {return {a};}

And then you simply make that chainable

//lhs is a vec2, rhs is a vec2_operation to use
template<class rhst, vec2(*f)(vec2,rhst)>
vec2& operator|(vec2& lhs, vec2_op1<rhst, f>&& op) {return lhs=op(lhs);}

Usage is simple:

int main() {
vec2 v2{3,5};
v2 | translate(2.5) | rotate(30) | translate(3) | scale(2);
std::cout << v2;
}

http://coliru.stacked-crooked.com/a/9b58992b36ff12d3

Note: No allocations, no pointers, no copies or moves. This should generate the same code as if you just did v2.translate(2.5); v2.rotate(30); v2.scale(10);
directly.



Related Topics



Leave a reply



Submit