Open arthurfiorette opened 2 months ago
I recently made a video about it, and as far as I can tell, people love the new operator and handle errors as values instead of using try/catch. Will share this in the comments: https://www.youtube.com/watch?v=eh5RYeprXDY
I saw your video @ipenywis!! it was awesome! Keep up the good work.
I wasn't expecting to gain so much attention by creating this proposal. Now I have to handle it :)
Ty @arthurfiorette and great work! I'd be happy to help if this proposal moves forward and requires more hands on deck 🫡
So I've seen a couple of articles and the aforementioned video on this, however I think this isn't a really necessary feature. One can trivially implement it in JS in multiple ways. (The one below only requires 27 lines of JS):
class Result {
static ERR = 0;
static OK = 1;
type;
data;
constructor(data, type = Result.ERR) {
this.data = data;
this.type = type === Result.OK ? Result.OK : Result.ERR;
}
get error() {
return this.type === Result.ERR ? this.data : undefined;
}
get result() {
return this.type === Result.OK ? this.data : undefined;
}
}
function Optional(fn) {
return async function(...args) {
try {
const data = await fn.apply(fn, args);
return new Result(data, Result.OK);
} catch(error) {
return new Result(error, Result.ERR)
}
}
}
function testOk(str) {
return 'Everything is OK' + str;
}
function testErr(str) {
throw Error('Error yeet!' + str)
}
const optionalOk = Optional(testOk);
const optionalErr = Optional(testErr);
const { result, error } = await optionalOk('Test1');
const { result: result2, error: error2 } = await optionalErr('Test1');
const okResult = await optionalOk('Test2');
const errResult = await optionalErr('Test2');
switch(okResult.type) {
case Result.OK:
console.log('Success', okResult.data);
break;
case Result.ERR:
default:
console.error('Failure', okResult.data);
}
switch(errResult.type) {
case Result.OK:
console.log('Success', okResult.data);
break;
case Result.ERR:
default:
console.error('Failure', okResult.data);
}
Ok, based on the discussion that I had some hours after the scheduled time (almost no one showed up in time 😭). I'll be opening a new branch to rewrite this proposal following the most loved ideas proposed in issues here.
Probably will start doing that on 1 sep 2024.
Thanks for the help of everyone.
This proposal has garnered significant attention from the community, sparking some exciting new ideas and raising important questions. To continue this momentum, I’m organizing a small Discord call where everyone can share their thoughts and help us make decisions together.
The meeting will be in English. Don’t worry if your English isn't perfect—mine isn’t either! Everyone is welcome to join the conversation.
Discord: https://discord.gg/YGEEgVeY?event=1275801482427367465
Google calendar: https://calendar.google.com/calendar/u/0/r/eventedit?dates=20240825T130000/20240825T130000&details=https://discord.com/invite/YGEEgVeY?event%3D1275801482427367465%0A%0Ahttps://github.com/arthurfiorette/proposal-safe-assignment-operator&location=Discord:+servidor+Kita,+meetings&text=Let%27s+talk+about+the+safe+assignment+operator