Rama
Rama

Reputation: 4767

How to initialize a local variable in its declaration in Eiffel?

I tried to do this:

local
    condition: BOOLEAN
    condition := true
do

And something like this:

local
    condition: BOOLEAN := true
do

Obviously neither work, but I need to initialize a variable inside the declaration because I don't want the variable to re-initialize if a retry instruction is executed. I looked in Eiffel's oficial documentation and in tutorial but they always intitialize variables in the body of the operation.

Upvotes: 4

Views: 1565

Answers (3)

Jocelyn
Jocelyn

Reputation: 703

local
   condition: BOOLEAN
do
   condition := True
   ...

Upvotes: 0

lucidbrot
lucidbrot

Reputation: 6156

I think you can use

local
condition: BOOLEAN = true
do

Upvotes: 0

Berend de Boer
Berend de Boer

Reputation: 2112

Every variable is initialised in Eiffel, so in local they all get their default value, which is false for BOOLEAN.

Note that for a retry the variables are not initialised to their default again, so you can use that with:

test
  local
    retrying: BOOLEAN
  do
    if retrying then
      do_something_else
    else
      retrying := true
      first_try
    end;
  rescue
    handle_error
    retry
  end

Upvotes: 8

Related Questions