       Re: Set::setps error? Twitter.m's OOP-like approach?

• To: mathgroup at smc.vnet.net
• Subject: [mg104555] Re: Set::setps error? Twitter.m's OOP-like approach?
• From: ADL <alberto.dilullo at tiscali.it>
• Date: Wed, 4 Nov 2009 01:31:44 -0500 (EST)
• References: <hconr9\$1lj\$1@smc.vnet.net>

```You might try with this "overloading" approach:

{NAME, BALANCE} = Range;
ClearAll[Account, create, getName, getBalance, deposit];
Account /: (f_[p___, Account[t_, n_], m___] /;
MemberQ[{Plus, Subtract, Times, Divide, Power}, f]) :=
Account[t, f[p, n, m]];
create[name_, balance_] := Account[name, balance];
getName[account_Account] := account[[NAME]];
getBalance[account_Account] := account[[BALANCE]];
deposit[account_Account, amount_] := account = account + amount;

This is the resulting behavior:

In:= a=create["test",Random[Integer,{0,200}]]
Out= Account(test,176)

In:= getBalance[a]
Out= 176

In:= b=a+10
Out= Account(test,186)

In:= b=a 10
Out= Account(test,1760)

In:= b=10a
Out= Account(test,1760)

In:= b=a/10
Out= Account(test,88/5)

In:= b=10/a
Out= Account(test,5/88)

In:= b=a^2
Out= Account(test,30976)

In:= deposit[a,25]
Out= Account(test,201)

In:= deposit[b,25]
Out= Account(test,31001)

In:= a
Out= Account(test,201)

In:= b
Out= Account(test,31001)

On Nov 3, 8:58 am, Erik Max Francis <m... at alcyone.com> wrote:
> I was experimenting with vaguely OOP-like wrappers around data similar
> to how Twitter.m does things:
>
tica/
>
> I was experimenting with "mutable objects" and I'm running into a
> scoping problem of some type that I don't understand.  Here's some
> sample code creating a wrapper around an "account balance":
>
> In:= {NAME, BALANCE} = Range
>
> Out= {1, 2}
>
> In:= create[name_, balance_] := Account[name, balance]
>
> In:= getName[account_Account] := account[[NAME]]
>
> In:= getBalance[account_Account] := account[[BALANCE]]
>
> In:= deposit[account_Account, amount_] :=
>   account[[BALANCE]] += amount
>
> The deposit function doesn't work, though:
>
> In:= a = create["test", 100]
>
> Out= Account["test", 100]
>
> In:= getBalance[a]
>
> Out= 100
>
> In:= deposit[a, 25]
>
> During evaluation of In:= Set::setps: Account[test,100] in the part
> assignment is not a symbol. >>
>
> Out= 125
>
> In:= getBalance[a]
>
> Out= 100
>
> But here's what I'm confused; if I do what getBalance does manually, it
> works fine:
>
> In:= a[[BALANCE]] += 25
>
> Out= 125
>
> In:= a
>
> Out= Account["test", 125]
>
> The Set::setps error is about Blocks so I suppose I'm running into some
> scoping issue but I don't follow what it is.  What am I missing here?
> What's the right way to write getBalance?
>
> Do Mathematica users find Twitter.m's approach to wrapping around
> objects palatable in the first place?  (My eventual application is for =
a
> wrapper around tensor objects where multiple things have to be carted
> around and the normal display of such objects would be far too ungainly
> to be acceptable anyway.)
>
> Thanks.
>
> --
> Erik Max Francis && m... at alcyone.com &&http://www.alcyone.com/max/
>   San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
>    Wyrd has swept all my kin / all the brave chiefs away! / Now I mus=
t