doc/named-param.dox
changeset 1722 2acb5f9bfa72
parent 1709 a323456bf7c8
child 1929 84d87d6024af
equal deleted inserted replaced
8:fb25531f608b 9:d13fbfd558f3
    19 exactly in this way. With a crafty trick and with some little
    19 exactly in this way. With a crafty trick and with some little
    20 inconvenience this is possible. We have implemented this little trick
    20 inconvenience this is possible. We have implemented this little trick
    21 as an example below.
    21 as an example below.
    22 
    22 
    23 \code
    23 \code
    24 class named_fn 
    24 class namedFn 
    25 {
    25 {
    26   int _id;
    26   int _id;
    27   double _val;
    27   double _val;
    28   int _dim;
    28   int _dim;
    29   
    29   
    30   public:
    30   public:
    31   named_fn() : _id(0), _val(1), _dim(2) {}
    31   namedFn() : _id(0), _val(1), _dim(2) {}
    32   named_fn& id(int p)     { _id  = p ; return *this; }
    32   namedFn& id(int p)     { _id  = p ; return *this; }
    33   named_fn& val(double p) { _val = p ; return *this; }
    33   namedFn& val(double p) { _val = p ; return *this; }
    34   named_fn& dim(int p)    { _dim = p ; return *this; }
    34   namedFn& dim(int p)    { _dim = p ; return *this; }
    35 
    35 
    36   run() {
    36   run() {
    37     printf("Here is the function itself.");
    37     printf("Here is the function itself.");
    38   }
    38   }
    39 };
    39 };
    40 \endcode
    40 \endcode
    41 
    41 
    42 
    42 
    43 The usage is the following.
    43 The usage is the following.
    44 
    44 
    45 We have to define a class, let's call it \c named_fn.  Let us assume that
    45 We have to define a class, let's call it \c namedFn.  Let us assume that
    46 we would like to use a parameter, called \c X. In the \c named_fn class we
    46 we would like to use a parameter, called \c X. In the \c namedFn class we
    47 have to define an \c _X attribute, and a function \c X. The function
    47 have to define an \c _X attribute, and a function \c X. The function
    48 expects a parameter with the type of \c _X, and sets the value of
    48 expects a parameter with the type of \c _X, and sets the value of
    49 \c _X. After setting the value the function returns the class itself. The
    49 \c _X. After setting the value the function returns the class itself. The
    50 class also have to have a function, called for example <tt>run()</tt>, we have
    50 class also have to have a function, called for example <tt>run()</tt>, we have
    51 to implement here the original function itself. The constructor of the
    51 to implement here the original function itself. The constructor of the
    53 them.
    53 them.
    54 
    54 
    55 If we instantiate this class, the default values will be set for the
    55 If we instantiate this class, the default values will be set for the
    56 attributes (originally the parameters), initially. If we call function
    56 attributes (originally the parameters), initially. If we call function
    57 \c X, we get a class with the modified parameter value of
    57 \c X, we get a class with the modified parameter value of
    58 \c X. Therefore we can modify any parameter-value, independent from the
    58 \c X. Therefore we can modify any parameter-value, independently from the
    59 order. To run the algorithm we have to call the <tt>run()</tt> function at the
    59 order. To run the algorithm we have to call the <tt>run()</tt> function at the
    60 end of the row.
    60 end of the row.
    61 
    61 
    62 Example:
    62 Example:
    63 \code
    63 \code
    64 named_fn().id(3).val(2).run();
    64 namedFn().id(3).val(2).run();
    65 \endcode
    65 \endcode
       
    66 
       
    67 \note Although it is a class, namedFn is used pretty much like as it were
       
    68 a function. That it why it is called namedFn and not \c NamedFn.
       
    69 
       
    70 \note In fact, the final <tt>.run()</tt> could be made unnecessary if the
       
    71 actual function code were put in the destructor instead. This however would make
       
    72 hard to implement functions with return values, and would also make the
       
    73 implementation of \ref named-templ-func-param "named template parameters"
       
    74 very problematic. Therefore, by convention, <tt>.run()</tt> is used
       
    75 to explicitly execute function having named parameters in Lemon. 
       
    76 
    66 
    77 
    67 \section traits-classes Traits Classes
    78 \section traits-classes Traits Classes
    68 
    79 
    69 The procedure above can also be applied when defining classes. In this
    80 The procedure above can also be applied when defining classes. In this
    70 case the type of the attributes can be changed.  Initially we have to
    81 case the type of the attributes can be changed.  Initially we have to