liberty-eiffel
[Top][All Lists]
Advanced

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

default or foo


From: Paolo Redælli
Subject: default or foo
Date: Thu, 30 Jun 2022 08:14:08 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0

I propose to add a feature to ANY.

infix "or" (another: like Current): like Current

    do

        if another=Void then

            Result:=Current

        else

            Result:=another

        end

    ensure Result/=Void

    end


to allow writing things like

    print("a default answer" or something_that_can_be_void)

instead of

    local temp: STRING

    do

        temp := something_that_can_be_void

        if temp=Void then

            print("a default answer")

        else

            print(temp)

        end

    end


Inspired by the coalescing operators. See https://en.wikipedia.org/wiki/Null_coalescing_operator

Initially I thought of proposing to allow to write "foo or default" like in many other languages but this would require to add to the language a special case to avoid an exception for a Void call.

Using a pattern "default or foo" just avoid this.

I think it will help writing expressive yet concise code. Think of it as an Eiffel-style coalescing operator.

An altertive naming would be to use a free operator but I couldn't find one that I like.





reply via email to

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