question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not sure what to call this, but for now I’ll refer to it as opcall()

Right now, pcall is really hard to use well. To demonstrate this, let’s describe a simple function that might error:

function foo(x: number) {
	if (x > 0) {
		return math.sqrt(x);
	} else {
		throw "x must be more than 0!";
	}
}

If we use pcall normally, it would look like this:

const [success, valueOrError] = pcall(foo, 1);
if (success) {
	valueOrError; // string | number
} else {
	valueOrError; // string | number
}

This isn’t good because valueOrError doesn’t have good types.

You could also try not destructuring the pcall return immediately:

const result = pcall(foo, 1);
if (result[0]) {
	const value = result[1]; // number
} else {
	const errorMsg = result[1]; // string
}

That’s better, but it’s not very ergonomic to use if (result[0]) {. It’s also not really clear when reading the code what you’re doing there.

I propose a new version of pcall that would have the following signature:

declare function opcall<T, U extends Array<any>>(
	cb: (...args: U) => T,
	...args: U
): { success: true; value: T } | { success: false; error: string };

And it’s usage would look like:

const result = opcall(foo, 1);
if (result.success) {
	result.value; // number
} else {
	result.error; // string
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
osyrisrblxcommented, Apr 27, 2019

inspired by PHP: real_pcall

1reaction
Validarkcommented, Apr 27, 2019

opcall - object pcall epcall - explicit pcall vpcall - verbose pcall dpcall - dictionary pcall tpcall - table pcall or toilet paper call

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a conference call from your iPhone
Dial the first person and wait for the call to connect. · Tap add call plus button . · Dial the second person,...
Read more >
How to make a conference call on your Android phone
Step 1: Call up the first person you want to include in your conference. · Step 2: Once the call connects, tap the...
Read more >
3-Way Calling - Conference Call FAQs - Verizon
Call the first phone number and wait for the person to answer. · Tap Add call. · Call the second person. Note: The...
Read more >
How to Set Up a Conference Call on iPhone
This guide explains how to set up a conference call on an iPhone for up to ... Tap the “add call” button that...
Read more >
Create call campaigns - Google Ads Help
With these campaigns, you can use CPC bidding based on the value of a call to your business. You can also add your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found