Giter Site home page Giter Site logo

p6-assertions's Introduction

NAME

Assertions - Runtime assertions for Perl 6

SYNOPSIS

use Assertions

my $x = 42;

assert $x;                      # Assert $x is defined and evaluates to something truthy

assert x %% 5;                  # Assert a simple expression. `assertion failed.`

assert $x² < 1729,              # Include a user-friendly error message
      'Ramanujan was slightly off the mark';

assert { ($x+1).is-prime };     # Assert a block

assert {
    ((1/$x¹, 1/$x², 1/$x³ … { $_ < 1/($x**$x) }).elems) == $x;
  },
  "No magic found with $x";

assert { $frodo.has-ring },
       { Logger.log('Oops, he did it again!'); }

Non-fatal assertions

In line with assert available in languages like C, C++, Python, etc assert throws an exception if the test expression fails. To make assertions non-fatal and log failure messages to STDERR instead, use the :non-fatal flag.

my $x = (-20,-15 ... 30).pick;
assert $x.sqrt !~~ NaN, :non-fatal;       # $x might be a negative number
with $x { do something(); }               # control resumes here
… 

In fact, :non-fatal may be needed when the test expression or block itself may throw an (inadvertent) exception.

assert { die  "See you later, I'm out …" },  # :non-fatal handles exceptions in this block
       { note "But wait, I live again …" },  # so that this block is called
       :non-fatal

DESCRIPTION

Assertions provides an assertion mechanism for Perl 6 in the style of assert.h (NOTE: Unlike assert from assert.h, these are subroutines and are not available at compilation time).

Note

Perl6's type system, sub/method signatures and return types, exceptions, etc somewhat make assertions an unneeded feature of the language and you should defer to the features of the language over the use of assert from this module for performance and stability (amongst other) reasons.

TODO

  • Make use of DEBUG and NDEBUG to control assertions.
  • Enable tracing to track triggered assertions.
  • Consider if Smart Comments may be of use.

SEE ALSO

p6-assertions's People

Contributors

shalomb avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.