Menu

Passing arguments to the ' init ' message-handler - using make-instance ?

Faraz
2021-09-25
2021-09-25
  • Faraz

    Faraz - 2021-09-25

    Hey there,
    considering the fact that "init" is also a message handler, is there a way to pass arguments to it (using make-instance or initialize-instance ? .
    I've got :

    (defclass MAIN::TESTCLASS       (is-a USER)
        (role concrete)
        (slot id))
    

    and :

    (defmessage-handler MAIN::TESTCLASS init after (?printStatus) "May print sth at the time of creation of instance or re-initialazation"
        (if (eq ?printStatus yes)
            then
                (printout t  "Just created  " (instance-name ?self))
        )       
    ) 
    

    and then :

    (make-instance of TESTCLASS (id 1)) : Message-handler 'init' after in class 'TESTCLASS' expected exactly 1 argument.

    Obvious, right ?
    So how do I pass the argument to it ?
    I have come to know (through trial & error) that the following wont work :

    (make-instance of TESTCLASS yes) :
    [PRNTUTIL2] Syntax Error:  Check appropriate syntax for slot-override.
    

    I believe there is a way to use the message-handler thing like a c# constructor.

    Thanks !!!

     
    • Gary Riley

      Gary Riley - 2021-09-25

      The init message generated for make-instance and initialize-instance doesn't provide any mechanism for passing additional arguments. If you want to set up some debugging behavior for a class, I'd suggest using a shared slot instead of trying to pass additional parameters:

               CLIPS (6.4 2/9/21)
      CLIPS> 
      (defclass TESTCLASS
         (is-a USER)
         (slot id)
         (slot printStatus (storage shared) (default FALSE)))
      CLIPS> 
      (defmessage-handler TESTCLASS init after ()
         (if ?self:printStatus
            then
            (printout t  "Just created  " (instance-name ?self) crlf)
            (bind ?self:printStatus FALSE)))
      CLIPS> (make-instance of TESTCLASS (id 1))
      [gen1]
      CLIPS> (make-instance of TESTCLASS (id 2) (printStatus TRUE))
      Just created  [gen2]
      [gen2]
      CLIPS> (make-instance of TESTCLASS (id 3))
      [gen3]
      CLIPS> 
      
       
  • Faraz

    Faraz - 2021-09-25

    Thanks there!!

     

Log in to post a comment.