Tilde: a project for syntatic sugar( Feature #2 postfix function )


#1

I am pleased to announce a library, tilde. It provides syntatic sugar for Rust, using the disused ~ operator.

The first feature it provides is postfix macro.

use tilde::tilde;

macro_rules! inc {
    ($e:expr) => { $e +1 }
}

tilde! {
    fn test_inc( input: i32 ) -> i32 {
        (   input
          + input.~inc!()
          + input.~inc!().~inc!()
          + input.~inc!().~inc!().~inc!()
        ).~inc!()
    }
}

macro_rules! add {
    ( $lhs:expr, $rhs:expr ) => { $lhs + $rhs }
}

tilde! {
    fn test_add( input: i32 ) -> i32 {
        (   input
          + input.~add!( input )
          + input.~add!( input.~add!( input ))
          + input.~add!( input.~add!( input.~add!( input )))
        ).~add!( input )
    }
}

assert_eq!( test_inc(3), 19 );
assert_eq!( test_add(1), 11 );

Project link: tilde


#2

tilde-0.0.3 : in compliance with RFC 2442. The first argument of the macro will be evaluated excactly once.


#3

tilde-0.0.4 supports postfix functions.

The syntax is first_arg.~the_fn(rest_args), which will
be desugared as the_fn( first_arg, rest_args ).

Example: Postfix function

fn inc( i: i32 ) -> i32 { i + 1 }

Suppose i: i32, The library user could write: i.~inc(),
i.clone().~inc() etc, which is a sugar as inc( i ) and
inc( i.clone() ).