10
$\begingroup$

Doesn't Association provide us rudimentary object oriented programming via constructs like

TEST = Association["value" -> 2, "method" -> Function[{x}, x^2]]
<|"value" -> 2, "method" -> Function[{x}, x^2]|>
TEST["value"] = TEST["method"][TEST["value"]];
TEST
<|"value" -> 4, "method" -> Function[{x}, x^2]|>

Do any of you familiar with Mathematica syntax and details see ways to extend this theme to include inheritance and other features of OO?

Is anyone attending the Wolfram conference next week who would like to discuss?

Regards Tom Gladd

$\endgroup$
2
  • 2
    $\begingroup$ I have discussed this topic a bit in my post on the use of Associations, and also here (not in the context of Associations). I you search this site, and also Wolfram Mathematica tag on Stack Overflow, you will find many more OO-related discussions. $\endgroup$ Oct 17, 2014 at 14:09
  • 1
    $\begingroup$ OO-programming was one of the things the very first books on Mathematica tackled, and then it became forgotten somehow. The Mathematica Programmer (Maeder, 1994) devotes a chapter to OO-programming in Mathematica, and so does Mastering Mathematica (Gray, 1996). Association on the other hand is new since of this summer, it is approximately twenty years younger than those books... $\endgroup$
    – C. E.
    Jan 16, 2015 at 1:25

2 Answers 2

2
$\begingroup$

You could already do this with DownValues

TEST["value"] = 2;
TEST["method"] = Function[{x}, x^2];
Definition[TEST]

TEST[method] = Function[{x}, x^2]

TEST[value] = 2

TEST["value"] = TEST["method"][TEST["value"]];
Definition[TEST]

TEST[method] = Function[{x}, x^2]

TEST[value] = 4

This method might be easier and more straightforward.

$\endgroup$
0
$\begingroup$

Associations (also knows as dictionaries) are essentially all that is needed for a crude class system (real men use structs, of course), and this is quite explicit in Python. However, using JUST dictionaries (or associations) without any syntactic sugar will produce extremely ugly (not to mention slow, but sugar won't help the latter problem) code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.