Home
Categories
Dictionnary
Download
Project Details
Changes Log

Constants


    1  Global constants definition
    2  Usage
    3  Local constants
    4  Notes

It is possible to use global constants in expressions.

Global constants definition

The ConstantsDefinitions singleton class manage a global constants repository shared by all expresssions.

To define a global constant, you just have to add it to the shared global constants repository:

      ConstantsDefinitions globals = ConstantsDefinitions.getInstance();  
      globals.addConstant("myConstant", new Constant(1));        

By default, the type and structure of a constant is set depending on its value[1] . However, it is possible to specify the type and structure of the constant. For example:

      ConstantsDefinitions globals = ConstantsDefinitions.getInstance();  
      globals.addConstant("myConstant", new Constant(1, ExpressionType.TYPE_FLOAT));        

Note that as for variables, global constants are shared by all expressions.

Usage

When encountering the constant name, an expression will use the value of the constant. For example:
      ConstantsDefinitions globals = ConstantsDefinitions.getInstance();  
      globals.addConstant("myConstant", new Constant(1));     
        
      Equation equation = ExpressionJ.parse("myConstant+2");   
      Objet value =  equation.eval();   

The result of value will be 3.

Local constants

Main Article: Local constants

It is possible to define constants local to a code block. For example:

      Equation equation = ExpressionJ.parse("const a = 2; return a + 1");  
      Object result = equation.eval();   
      // the result is 3       

Notes

  1. ^ For example, in the example above, the type of the constant would be ExpressionType.TYPE_INTEGER and its structure would be ExpressionType.STRUCT_SCALAR

See Also


Categories: concepts

Copyright 2018 Herve Girod. All Rights Reserved