1

I have this simple code, I'd like to access an element in an ARRAYED_SET what is in a HASH_TABLE, but I get an error:

Error: target of the Object_call might be void.
What to do: ensure target of the call is attached.

here's my code:

class
APPLICATION

inherit
    ARGUMENTS

create
    make

feature {NONE}
make
local

    elem: ARRAYED_SET[INTEGER]
    table: HASH_TABLE[ARRAYED_SET[INTEGER], INTEGER]
do
    create elem.make (3)
    create table.make (1)

    elem.put (4)
    table.put (elem, 1)

    table.at (1).go_i_th (1)
end
end
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45

1 Answers1

2

When you access an item in a HASH_TABLE it's possible that the item is not present. Therefore the signature of this feature is

item (k: K): detachable G

and it returns Void (or a default value for an expanded type) if the item is not found. So, when you try to use an item from HASH_TABLE, it should be checked whether it is attached or not. This can be achieved by replacing:

table.at (1).go_i_th (1)

with:

if attached table.at (1) as la_element then
  la_element.go_i_th (1)
end
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
Louis M
  • 576
  • 2
  • 4
  • it's OK, but as you seed I just added the item **elem** with key **1** so it must present – Ferenc Dajka Jan 05 '14 at 11:09
  • or you mean, the langueage forces me to check wheter it's void or not every time I use it? – Ferenc Dajka Jan 05 '14 at 11:21
  • 1
    Yes, with Void safety enabled, every detachable objects must be check, even if you just set it. It look like it does not make sense, but in some case, it can be important to do so. For exemple, when you use multi-thread. Also, Void Safety can be disable if you don't like it. Of course, I don't recommand to do so. – Louis M Jan 05 '14 at 14:18
  • thanks! it's absolutely makes sense, when someone write small codes like these maybe no, but if you write bigger ones you could get confused, it's good that you don't need to check this kind of error. I wrote an other question here http://stackoverflow.com/questions/20935201/detachable-element-in-ensure-eiffel . Could you answer that one too? And could you recommend a site where I can learn about Eiffel, I'm googleing for these things, but find nothing. – Ferenc Dajka Jan 05 '14 at 15:13
  • Well, Eiffel is a wonderfull language, but it really lack some easy tutorial. I learn Eiffel by writing Eiffel code and asking question (as you do now). But I do have some reference to give you. First, some docs here: http://docs.eiffel.com/book . Also, the Eiffel users mailing list at http://eiffel.641255.n2.nabble.com/Eiffel-Software-Users-f641256.html . There's Eiffel Room also at http://www.eiffelroom.org/ but there is noting really recent there. – Louis M Jan 05 '14 at 15:51
  • I have an other question, can you please answer that one too? :P http://stackoverflow.com/questions/20938164/immutable-class-in-eiffel – Ferenc Dajka Jan 05 '14 at 19:38