Giter Club home page Giter Club logo

guardian's Introduction

guardian

travis_ci

Function guard is a very powerful feature of Erlang. But only BIF functions are supported. Guardian allows calling (any local or across module) functions in guards. This allows the code to be very short. Easy to code and maintain.

Usage:

It is achieved using parse transforms. Just add parse_transform as guardian in the source file or specify during compilation.

-compile({parse_transform, guardian}).

Note:

Please be careful while using non BIF functions in guards. There should not be any side effects as the guards are evaluated for each clause in sequence until a match clause is found every time the function is called.

Internal:

  • Only the function calling other functions in guards (non BIF), is replaced with function with case statements in the AST. All other functions are not transformed.
  • It maintains the guard sequence.
  • Line numbers are of the actual function. For example if there is a runtime error in the guard function the error report shown will have the line number of the original function and not the line of the generated case function. Hence there is no change to the developer. The code can be debugged without thinking of the transformed function.

Example:

greetings(Name, AllowedNames) when lists:member(Name, AllowedNames) ->
  {welcome, Name};
greetings(Name, AllowedNames) ->
  {leave, Name}.

The above function is replaced with code similar to the below functions. This is hidden and it does not affect the developer while compiling and debugging.

greetings(Name = _GuardVariable1, AllowedNames = _GuardVariable2) ->
    case lists:member(Name, AllowedNames) of
        true -> {welcome, Name};
        _ ->
            greetings_guardian1(_GuardVariable1, _GuardVariable2)
    end.

greetings_guardian1(UnknownName = _GuardVariable1, _AllowedNames = _GuardVariable2) ->
    {leave, UnknownName}.

Support:

Please report issues/bugs and enhancement requests / suggestions so that it can be improved further.

guardian's People

Contributors

vinod-hg avatar

Stargazers

Conrad Steenberg avatar Serge Aleynikov avatar Amirhossein Aleyasen avatar Danilo Nery avatar Vladimir Potapev avatar Andy Till avatar Andrei Neculau avatar Eksperimental avatar Luis Rascão avatar h920526 avatar  avatar Wondong Shin avatar

Watchers

Wondong Shin avatar James Cloos 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.