Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check optional module availability

Tags:

module

raku

In a module I'm writing there is just one method which requires an additional module, so I wish to make that module optional by not listing it in the depends part of the META6.json file. The method will return a Failure object if the optional module is not available.
I know I can do something like this:

if (try require Optional::Module) !=== Nil {
  # go on
} else {
  # fail
}

Is there any better way to do it?

like image 819
Fernando Santagata Avatar asked Feb 11 '21 13:02

Fernando Santagata


People also ask

How do I See which modules are installed in Linux?

Each module can be also loaded by referring to its aliases, stored in the /lib/modules/$ (uname -r)/modules. How do I see which kernel modules are installed? To check which kernel is currently running on your system, use the uname command with the “release” or -r switch. This will output the kernel version (release) number.

What are the different methods of the optional class?

The optional class provides around 10 methods, which we can use for creating and using the Optional class and we are going to see how they are used below. The are three creational methods for creating an optional instance. Returns an empty Optional instance. Returns an empty {Optional} instance. No value is present for this Optional.

What is optional in Java?

What it Is Trying to Solve Optional is an attempt to reduce the number of null pointer exceptions in Java systems, by adding the possibility to build more expressive APIs that account for the possibility that sometimes return values are missing.

How do I serialize an object with an optional value?

If you do need to serialize an object that contains an Optional value, the Jackson library provides support for treating Optionals as ordinary objects. What this means is that Jackson treats empty objects as null and objects with a value as fields containing that value. This functionality can be found in the jackson-modules-java8 project.


2 Answers

I want to thank everyone who answered or commented on this question.
I benchmarked my proposed solution and the two ones given in the comments to my question:

my $pre = now;
for ^10000 {
  $*REPO.repo-chain.map(*.?candidates('Available::Module').Slip).grep(*.defined);
  $*REPO.repo-chain.map(*.?candidates('Unavailable::Module').Slip).grep(*.defined);
}
say now - $pre; # 13.223087

$pre = now;
for ^10000 {
  $*REPO.resolve(CompUnit::DependencySpecification.new(:short-name("Available::Module")));
  $*REPO.resolve(CompUnit::DependencySpecification.new(:short-name("Unavailable::Module")));
}
say now - $pre; # 3.105257

$pre = now;
for ^10000 {
  (try require Available::Module) !=== Nil;
  (try require Unavailable::Module) !=== Nil;
}
say now - $pre; # 4.963793

Change the module names to match one that you have available on your system and one that you don't.
The comments show the results in seconds on my computer.

like image 138
Fernando Santagata Avatar answered Oct 09 '22 08:10

Fernando Santagata


Something like this?

my $loaded = False;
CATCH {
    default { $loaded = True }
}
require Optional::Module;
if ( ! $loaded ) {
  # Fail
}
# Go on

In this case it will try and load the module and catch the exception at runtime.

like image 33
Scimon Proctor Avatar answered Oct 09 '22 08:10

Scimon Proctor