Skip to main content
added 76 characters in body
Source Link
Simon
  • 10.2k
  • 5
  • 57
  • 73

So the naive way to set up a data structure like struct is, as the OP suggested, to simply used DownValues and/or SubValues. In the below, I use SubValues.

Copying the Wikipedia C language struct example

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

struct account s; // Create new account labelled s
s.account_number // access the account number

In Mathematica, we can talk about an "instance" of account as

account["s"]

set and access its properties using SubValues

account["s"]["account_number"] = 12345

account["s"]["account_number"]
(* Returns: 12345 *)

To make this a bit more robust, you should probably have a gentleman's agreement with your code to only access the "objects" using type checked instantiation and setting methods. Also, code for deletion of "objects" is easy to write by using DeleteCases on the SubValues of account. That said, I've written largish applications for my own use that do not bother with such niceties.

So the naive way to set up a data structure like struct is, as the OP suggested, to simply used DownValues and/or SubValues. In the below, I use SubValues.

Copying the Wikipedia C language struct example

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

struct account s; // Create new account labelled s
s.account_number // access the account number

In Mathematica, we can talk about an "instance" of account as

account["s"]

and access its properties using SubValues

account["s"]["account_number"]

To make this a bit more robust, you should probably have a gentleman's agreement with your code to only access the "objects" using type checked instantiation and setting methods. Also, code for deletion of "objects" is easy to write by using DeleteCases on the SubValues of account. That said, I've written largish applications for my own use that do not bother with such niceties.

So the naive way to set up a data structure like struct is, as the OP suggested, to simply used DownValues and/or SubValues. In the below, I use SubValues.

Copying the Wikipedia C language struct example

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

struct account s; // Create new account labelled s
s.account_number // access the account number

In Mathematica, we can talk about an "instance" of account as

account["s"]

set and access its properties using SubValues

account["s"]["account_number"] = 12345

account["s"]["account_number"]
(* Returns: 12345 *)

To make this a bit more robust, you should probably have a gentleman's agreement with your code to only access the "objects" using type checked instantiation and setting methods. Also, code for deletion of "objects" is easy to write by using DeleteCases on the SubValues of account. That said, I've written largish applications for my own use that do not bother with such niceties.

Source Link
Simon
  • 10.2k
  • 5
  • 57
  • 73

So the naive way to set up a data structure like struct is, as the OP suggested, to simply used DownValues and/or SubValues. In the below, I use SubValues.

Copying the Wikipedia C language struct example

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

struct account s; // Create new account labelled s
s.account_number // access the account number

In Mathematica, we can talk about an "instance" of account as

account["s"]

and access its properties using SubValues

account["s"]["account_number"]

To make this a bit more robust, you should probably have a gentleman's agreement with your code to only access the "objects" using type checked instantiation and setting methods. Also, code for deletion of "objects" is easy to write by using DeleteCases on the SubValues of account. That said, I've written largish applications for my own use that do not bother with such niceties.