MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: fyi, small note on using Mathematica for object based programming

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126878] Re: fyi, small note on using Mathematica for object based programming
  • From: Kurt TeKolste <tekolste at fastmail.net>
  • Date: Fri, 15 Jun 2012 03:40:42 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

Another technique for defining object attributes and methods that seems
to work at first glance is to use upvalues.

I have only played with this at an elementary level and do not know if
there are any pitfalls with more complex usages.

Has anyone tried this?  

Kurt

Example

In[91]:= 

newAnimal[n_, s_] := 
Module[{uniqueID = Unique["animal"]},
  ID[a_Animal] ^= uniqueID;
  name[a_Animal] ^= n;
  sound[a_Animal] ^= s;
  makeSound[a_Animal] ^:= For[i = 0, i < 5, i++, Print[sound[a]]];
  uniqueID]

In[92]:= myanimal = Animal[id = newAnimal[lion, roar]]

Out[92]= Animal[animal7]

In[93]:= name[myanimal]

Out[93]= lion

In[94]:= makeSound[myanimal]

roar

roar

roar

roar

roar



  • Prev by Date: Bug in NIntegrate[]?
  • Next by Date: Re: modulo solving lacking domain?
  • Previous by thread: Re: Bug in NIntegrate[]?
  • Next by thread: is Head[] part of the expression?