Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

old variable generation on inherited instance variables

#17

it seems that the old variable generation does not work on inherited instance variables:

import org.gcontracts.annotations.*

// by http://www.javacoffeebreak.com
class Account 
{
    protected BigDecimal balance

    def Account( BigDecimal amount = 0.0 )
    {
        balance = amount
    }

    void deposit( BigDecimal amount )
    {
        balance += amount
    }

    @Requires({ amount >= 0.0 })
    // @Ensures({ balance == old.balance - amount })
    BigDecimal withdraw( BigDecimal amount )
    {
        if (balance < amount) return 0.0
        
        balance -= amount
        return amount
    }

    BigDecimal getBalance()
    {
        return balance
    }
}  

class BetterAccount extends Account {
    
    @Ensures({ balance == old.balance - (amount * 0.5) })
    BigDecimal withdraw( BigDecimal amount )
    {
        if (balance < amount) return 0.0
        
        balance -= amount * 0.5
        return amount
    }
}

Account account = new BetterAccount()
account.deposit(30)
account.withdraw(10) // could be a program bug

account.balance       // uniform access principle: uniform access to computed or in-memory vals

Reported by me (at andresteingress) · April 11th, 2011 @ 08:57 PM

State: resolved
Milestone: 1.2.4

Activity

  1. me (at andresteingress)
    me (at andresteingress)

    the problem occurs only if the inherited class computes the old variables map, but the predecessor does not.

    April 11th, 2011 @ 09:07 PM

  2. me (at andresteingress)
    me (at andresteingress)
    • Milestone changed from 1.2.2 to 1.2.3
    • Milestone order changed from 10 to 0

    May 12th, 2011 @ 08:09 PM

  3. me (at andresteingress)
    me (at andresteingress)
    • Milestone changed from 1.2.3 to 1.2.4
    • Milestone order changed from 4 to 0

    May 18th, 2011 @ 11:17 AM

  4. me (at andresteingress)
  5. me (at andresteingress)
    me (at andresteingress)
    • State changed from new to resolved

    May 23rd, 2011 @ 09:17 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.