Re: Set::setps error? Twitter.m's OOP-like approach?
- To: mathgroup at smc.vnet.net
- Subject: [mg104575] Re: Set::setps error? Twitter.m's OOP-like approach?
- From: dh <dh at metrohm.com>
- Date: Wed, 4 Nov 2009 01:35:34 -0500 (EST)
- References: <hconr9$1lj$1@smc.vnet.net>
Hi Erik, what you want to do is a bit tricky. By: deposit[a, 25] you want to assigne to the variable "a" an expression like: Account[..,..] By default all arguments to a function are evaluated prior to calling the function. Therefore your variable "account" does not contain "a" but the expression "Account["test", 100]". To obtain the variable name you must prevent the evaluation by e.g. specifying; SetAttributes[deposit,HoldFirst]. Inside the function you must again take precautiion against evaluation of the variable by specifying e.g.: Unevaluated[account] = As you have not defined an addition rule for "Account" we need to create the full expression on the right side of the assignment: Unevaluated[account] = Account[account[[1]], account[[2]] + amount] Here "account" contains the evaluated "a", namely: "Account["test", 100]" Therefore, deposit would be declared e.g. like: SetAttributes[deposit, HoldFirst]; deposit[account_, amount_] := Unevaluated[account] = Account[account[[1]], account[[2]] + amount] Daniel Erik Max Francis wrote: > I was experimenting with vaguely OOP-like wrappers around data similar > to how Twitter.m does things: > > http://blog.wolfram.com/2009/04/30/twittering-with-mathematica/ > > 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[1]:= {NAME, BALANCE} = Range[2] > > Out[1]= {1, 2} > > In[2]:= create[name_, balance_] := Account[name, balance] > > In[3]:= getName[account_Account] := account[[NAME]] > > In[4]:= getBalance[account_Account] := account[[BALANCE]] > > In[5]:= deposit[account_Account, amount_] := > account[[BALANCE]] += amount > > The deposit function doesn't work, though: > > In[6]:= a = create["test", 100] > > Out[6]= Account["test", 100] > > In[7]:= getBalance[a] > > Out[7]= 100 > > In[8]:= deposit[a, 25] > > During evaluation of In[8]:= Set::setps: Account[test,100] in the part > assignment is not a symbol. >> > > Out[8]= 125 > > In[9]:= getBalance[a] > > Out[9]= 100 > > But here's what I'm confused; if I do what getBalance does manually, it > works fine: > > In[10]:= a[[BALANCE]] += 25 > > Out[10]= 125 > > In[11]:= a > > Out[11]= 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. >