R/setPragma.R

Defines functions setPragma

Documented in setPragma

# -------------------------------------------------------------------------
# setPragma:
#   Creates a keyword that can then be used to implement simple 
#   functionality with a clean syntax
#
#   R is (mostly) a functional language.  Any action taken is made through
#   function calls. This requires all of the calls to use parens even if 
#   no arguments are necessary.  Very often a function has no arguments
#   and are used to set some functionality of the program.  These are 
#   sometimes called pragmas or directives and specify some behaviours of
#   the program -- usually how to compile the program.  Of course, R is
#   not compiled.  It is completely non-essential, but having the ability
#   to have PRAGMAS is a nice feature and makes the code more readable.
#    
#    > AUTOHELP     # instead of autohelp()
# 
#   There should be some guidance on syntaz such as keywords 
#   should be in ALL CAPS.  
#
#   setPragma( function )
#    - Defines an anonymous class
#    - Defines a keyword an object of this class
#    - Defines a show method.
#
#   TODO:
#    - sealed=TRUE : seal the PRAGMA class?
#
# -------------------------------------------------------------------------

# library(formula.tools) 

setPragma <- function( name, fun ) {

  # name <- as.character( lhs( lat.call()  ) )
   #name <- paste( "PRAGMA", name, sep="." ) 
  # TEST IF ALREADY PRESENT

  # SET CLASS
  setClass( name, "NULL", where=globalenv() ) 


  # CONSTRUCT FUNCTION FOR SHOW METHOD
  x <- function(object) {} 
  body(x) <- body(fun) 

  # DEFINE THE ACTION FOR THE KEYWORD 
  setMethod( "show", name, x, where=globalenv() )  

  # We have to define evaluate
  cmd <- paste( name, "<- new('", name, "')", sep ="" )
  call <- parse( text=cmd ) 
  
  eval( call, globalenv()  )  

  # RETURN THE KEYWORD 
  #    KEYWORD <- setPragma( fun ) syntax 
  # return( new( name ) ) 
  return(invisible())

}
 

# FOO <- setPragma( function() cat( "FOO WORKS\n" )  ) 
# export(xdx)
# FOO 

Try the pragma package in your browser

Any scripts or data that you put into this service are public.

pragma documentation built on May 29, 2017, 11:01 a.m.