Code Monkey home page Code Monkey logo

bmr's Introduction

bmr (bad record match) parse transform for match expressions with records in left side like
#record{ field_1 = value_1 } = something_that_return_record() that will wrap
this match_expr with special code that will work in case of badmatch exception
and will indicate which exact fields were not matched.


Initial code (from bmr_example.erl):
return_record() ->
  #rec{
    a = 1,
    b = 2,
    c = #rec2{
      x = 11,
      y = #rec3{
        s = 111,
        t = {112, a},
        q = b,
        w = a
      },
      z = 12
    },
    d = 3,
    e = a,
    f = b,
    g = a,
    k = k1,
    l = l1
  }.

Vb = bbb,
Vq = b,
#rec{
  a = 11, % wrong value
  b = Vb, % wrong value with variable
  c = #rec2{
    x = X,
    y = R3 = #rec3{ % nested records could be putted into var
      % s = 111,
      t = {112, b}, % wrong value inside nested records
      q = Vq,
      w = Vdfw % detect same new variable wrong values (inside nested records)
    },
    z = 12
  },
  d = Vdfw, % detect same new variable wrong values
  e = Veg, % working with new variables 1+
  f = Vdfw, % detect same new variable wrong values
  g = Veg, % working with new variables 1+
  k = Vkl,
  l = Vkl
} = return_record()

running this code without bmr_pr parse transform:
{badmatch, {rec,1,2,{rec2,11,{rec3,111,{112,a},b,a},12},3,a,b,a,k1,l1}}
not very informative..

but running this code with bmr_pr parse transform:
>>>>>>>>>> bmr called ({"bmr_example.erl",179}) <<<<<<<<<<
In record 'rec' following fields should have the same value:
 for var 'Vdfw':
   "field 'f'": b
   "field 'd'": 3
   "field 'c' (record type 'rec2') field 'y' (record type 'rec3') field 'w'": a

 for var 'Vkl':
   "field 'l'": l1
   "field 'k'": k1

In record 'rec' following fields does not match on left and right:
  "field 'a'": left = 11 vs right = 1
  "field 'b'": left = bbb vs right = 2
  "field 'c' (record type 'rec2') field 'y' (record type 'rec3') field 't'": left = {112, b} vs right = {112, a}
<<<<<<<<<<  bmr done  >>>>>>>>>>


example of initial code and result of parse_transform:
A = 5,
#record{
  a = A,
  b = B,
  c = C,
  d = 7
} = return_record_fun()
--->
begin
  {R, [B, C]} = try
    _tmp_R = return_record_fun(),
    #record{
      a = A,
      b = _tmp_B,
      c = _tmp_C,
      d = 7
    } = _tmp_R,
    {_tmp_R, [_tmp_B, _tmp_C]}
  catch
    error:{badmatch, _tmp_R} ->
      bmr:bmr(...), all needed information about record
      erlang:error({badmatch, _tmp_R})
  end,
  R
end

bmr's People

Contributors

irpab avatar

Watchers

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