liberty-eiffel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Coalescing operator


From: Paolo Redaelli
Subject: Coalescing operator
Date: Fri, 1 Jul 2022 10:14:30 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0

Il 30/06/22 18:41, Hans Zwakenberg ha scritto:
What would happen if neither of the two arguments in your example is void?  
Which of the two would be printed?

None, it would trigger a call to void exception if the first argument is Void.

Forcing a particular behaviour would require support from the compiler or a change to the language itself.

What I was proposing is a way to mimick the coalescing operator found in many other languages


So running

class TEST
create {ANY} make
feature
      make
      do
         print ("default%N" or Void)
         print ("default%N" or foo)
         print ( (42 or 12).out+" is the default number%N")
         print (bar(1) or bar(2))
         -- print ((Void or Void).out) crashes the compile
      end

      foo: STRING
         do
            Result := "something new%N"
         end
      bar(i: INTEGER): STRING
         do
            if i/=42 then Result:=Void
            else Result:="the meaning of life%N"
            end
         end
end

will result in

default
something new
12 is the default number
Line : 9 column 17 in /home/paolo/tmp/ordefa/test.e.
*** Error at Run Time ***: Call on a Void target.


PS: it can't be implemented with a direct comparison to Void but

   infix "or" (another: like Current): like Current
   do
      if default=another then
         Result:=Current
      else
         Result:=another
      end
   end

is also valid for expanded objects.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]