Re: request for change of boolean
- Posted by CChris <christian.cuvier at agric?lture.gouv.fr> Dec 06, 2007
- 847 views
Kat wrote: > > > Is it possible to change how boolean expressions are evaluated such that only > values greater than 0 pass this test? : > > }}} <eucode> > readfile = open(flagfilename,"r") > if readfile then > -- it's there, process it > end if > </eucode> {{{ > > Currently, if readfile = -1 , it evaluates to TRUE, when in fact the readfile > was not opened. > > Kat 1/ I routinely use things like
if readfile+1 then -- readfile is a valid handle end if
Granted, the "+1" is excess typing, but if only it were the only problem with Eu... 2/ If the nil logical value was in the language, then open() could return nil on failure and your test would work. Much cleaner, I wish it were here. But it means changing two state logic to three state. Some code is expected to break. This has been discussed already a few months ago. 3/ Another approach, more object oriened (donning firefighter gear fast), and much easier to use as a result is: - in the standard library: <eiffel> class FILE -- whatever should go in here feature {NONE} -- they are private last_open_file: INTEGER -- last handle retrieved open(file_name,file_mode:STRING) is local success: BOOLEAN -- some internal flag do -- whatever it takes if success then last_open_succeeded = TRUE last_open_file = the_handle else last_open_succeeded = FALSE end ensure success = last_open_succeeded end feature -- public last_open_succeeded: BOOLEAN -- set by open on success get_last_open_file(): INTEGER is require last_open_succeeded do Result = last_open_file end </eiffel> - in your code: <eiffel> open(file,mode) if last_open_succeeded then my_file = get_last_open_file() process(my_file) else file_not_opened() -- must do something about it end </eiffel> If you try to access a file that couldn't be opened without cheking, you get an exception. Again, while the above two scenarios would make coding safer and clearer, they just don't fit in what Eu is. If I ever can get a corret type system to work in Æ - not for the near future -, then the nil solution will be used. CChris