Reputation: 3226
When writing JavaScript/TypeScript, I sometimes implement a pattern where a function returns either a response object (as shown below) or a response "tuple" (just meaning array with two items), rather than the raw value. Something like this:
example.js
function getName() {
if (userPressedOk) {
return {status: "OK", name: getName()}
else {
return {status: "FAIL", name: ""}
}
}
example.ts
function getName(): { status: string; jobName: string } {
if (userPressedOk) {
return {status: "OK", name: getName()}
else {
return {status: "FAIL", name: ""}
}
}
This is a slightly contrived example, but that's the basic idea. I am trying to imitate I style I have seen in functional programming languages. Does this pattern have a name?
Upvotes: 1
Views: 89
Reputation: 664484
I've seen them called Result Objects.
In the functional languages that you say you are borrowing from, the respecitive type is often called Result
or Either
(although it usually provides an error message in the fail case, not a default value).
Upvotes: 3