Can't construct for local variables.

by Phung Dinh Vu (modified: 2009 Nov 04)

I use this code, but when run it has error like this: Syntax error at line 22 in class EXCEPTIONHAN -- Client deposit money into his account. local account_number : INTEGER is 10 -----------------------------------------------^ i : INTEGER See my code: feature Client_Deposit is -- Client deposit money into his account. local account_number : INTEGER is 10 i : INTEGER amount : REAL do rescue end -- client Deposit. It's meant we can't construct for local variable in eiffel. In java, it's very easy. void Client_Deposit(){ int account_number = 10; ..... }

  • Phung Dinh Vu (14 years ago 4/11/2009)

    I use this way, but it's not comfortable.

    feature {NONE} client_deposit is -- Client deposit is private method. -- Client deposit money into his account. local account_number : INTEGER mark : BOOLEAN do -- Try block. if not mark then -- Construction. account_number := 1200 --Not sure. end -- Assert bank to deposit money by call bank's deposit method. deposit(account_number) mark := true rescue mark := true account_number := account_number + 1 -- Try another account number retry end -- client Deposit.

  • Peter Gummer (14 years ago 4/11/2009)

    That is a constant, not a local

    I'm not sure what you mean by "constructing" a local variable. I think you mean you want to initialise the local variable.

    This is how you initialise the local integer variable to 10:

    local account_number: INTEGER do account_number := 10

    The syntax that you used is wrong for two reasons. First of all, it is declaring a constant, and constants cannot be declared within a routine. Secondly, the is keyword is obsolete; for declaring a constant, use =.

    • Manu (14 years ago 5/11/2009)

      Please do not use the blog section to submit question. Instead go to

    • Phung Dinh Vu (14 years ago 5/11/2009)

      it's not comfortable.

      Deal Peter Gummer ! Thanks for your guiding. But when use this way to initialise a local variable: local account_number: INTEGER do account_number := 10 when post-violation is occured and excute the retry instruction, it's initialise this variable again ( your way). it's not proper, i mean. When the retry is excute, the variables should have their current value and not be initialised again.

      I know that when we declare : local account_number: INTEGER do variable account_number is initialised the value 0. And when retry is excuted, account_number isn't initialised value 0 again. it's comfortable. But we always have to use this defaut initialization, don't we? If it were true, it would be uncomfortable.

      In many case We wanna initialise proper local value. And when retry instruction is excuted, the local values have their current value.

      In C, I can do that by use "static" keyword in front of the local variables. It's comfortable in many case.

      • Colin Adams (14 years ago 5/11/2009)

        You can use a local to control extent of retry

        You have full control over whether local variables are re-intialized or not. The usual technique is to declare a local boolean variable and set it to true in the rescue clause:

        e.g. local l_retried: BOOLEAN l_account_number: INTEGER do if not l_retried then l_account_number := 10 ... end ... rescue l_retried := True retry end Colin Adams

        • Phung Dinh Vu (14 years ago 5/11/2009)

          I know this way to initialise local value. But if use this way, we have to use another boolean value to mark.

          Firstly, it's waste of storage.

          Secondly, When rely instruction is excuted, the most above condition instruction is always excuted. And the the instruction that set value to true is also. It's waste of time. if not l_retried then -- This condition is always excuted. I meant, Why eiffel doesn't supply the way to initialise local value ?