Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:08:04+00:00 2026-05-25T15:08:04+00:00

The question on making a record like in Mathematica has been discussed in few

  • 0

The question on making a record like in Mathematica has been discussed in few places, such as Struct data type in Mathematica?.

The problem with all these methods, is that one loses the ability, it seems, to do the
specific extra check on each argument, as in when one does x_?NumericQ.

My question is: Is there a way in Mathematica to make a record or a struct, and yet be able to use the checking as above on the individual elements?

I am trying to settle down on one method to use as I am tired of having functions called with 10 parameters on them (sometimes one can’t avoid this), even when I try to make each function very specific, to minimze the number of parameters, some functions just need many parameters to do the specific job.

First I show the three methods I know about.

Method 1

foo[p_]:=Module[{},
    Plot[Sin[x],{x,from/.p,to/.p}]
]
p={from->-Pi,to->Pi};
foo[p]

Advantage: safe, as if I change the symbol ‘from’ to something else, it will still work. As the following example.

foo[p_]:=Module[{},
    Plot[Sin[x],{x,from/.p,to/.p}]
]
p={from->-Pi,to->Pi};
from=-1; (* By accident the symbol from was set somewhere. It will work*)
foo[p]

Method 2

Clear[p,foo];
foo[p_]:=Module[{},
    Print[p];
    Plot[Sin[x],{x,p["from"],p["to"]}]
]
p["from"] = -Pi;
p["to"]   = Pi;

foo[p]

Advantage: also safe, strings are immutable. Do not have to worry about the “from” value changing. But having strings everywhere is not too readable?

Method 3

Clear[p,to,from];
foo[p_]:=Module[{},
    Plot[Sin[x],{x,p[from],p[to]}]
]
p[from] = -Pi;
p[to]   = Pi;

foo[p]

Disadvantage: if any of the symbols ‘from’ or ‘to’ get overwritten somewhere, will cause problem, as in

from=-4; (*accidentally the symbol from is assigned a value*)
foo[p]   

So. I think method (1) is the most safe. But now I lose the ability to do this:

foo[from_?NumericQ, to_?NumericQ] := Module[{},
    Plot[Sin[x], {x, from, to}]
]
from = -Pi; to = Pi;
foo[from, to]

So, I am hoping to get an idea to be able to combine making a ‘record’ like, but at the same time, still be able to use the parameter checking on individual elements in the record? Or is this question is not not well posed for Mathematica functional/rule based programming style?

That is one thing I wish Mathematica had, which is a real record to help manage and organize all the variables used in the program.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:08:04+00:00Added an answer on May 25, 2026 at 3:08 pm

    First, I’d like to mention that all the methods you listed are IMO flawed and dangerous. The main reason why I don’t like them is that they introduce implicit dependences on global variables (the reasons why this is bad are discussed e.g. here), and can also mess up with the scoping. Another problem of them is that those approaches look like they won’t scale nicely to many instances of your structs existing simultaneously. The second method you listed seems the safest, but it has its problems as well (strings as field names, no way to type-check such a struct, also symbols used there may accidentally have a value).

    In my post here I discussed a possible way to build mutable data structures where methods can do extra checks. I will copy the relevant piece here:

    Unprotect[pair, setFirst, getFirst, setSecond, getSecond, new, delete];
    ClearAll[pair, setFirst, getFirst, setSecond, getSecond, new, delete];
    Module[{first, second},
       first[_] := {};
       second[_] := {};
       pair /: new[pair[]] := pair[Unique[]];
       pair /: new[pair[],fst_?NumericQ,sec_?NumericQ]:= 
          With[{p=new[pair[]]}, 
              p.setFirst[fst];
              p.setSecond[sec];
              p];
       pair /: pair[tag_].delete[] := (first[tag] =.; second[tag] =.);
       pair /: pair[tag_].setFirst[value_?NumericQ] := first[tag] = value;
       pair /: pair[tag_].getFirst[] := first[tag];
       pair /: pair[tag_].setSecond[value_?NumericQ] := second[tag] = value;
       pair /: pair[tag_].getSecond[] := second[tag];       
    ];
    Protect[pair, setFirst, getFirst, setSecond, getSecond, new, delete]; 
    

    Note that I added checks in the constructor and in the setters, to illustrate how this can be done. More details on how to use the structs constructed this way you can find in the mentioned post of mine and further links found there.

    Your example would now read:

    foo[from_?NumericQ, to_?NumericQ] :=
       Module[{}, Plot[Sin[x], {x, from, to}]];
    foo[p_pair] := foo[p.getFirst[], p.getSecond[]]
    pp = new[pair[], -Pi, Pi];
    foo[pp]
    

    Note that the primary advantages of this approach are that state is properly encapsulated, implementation details are hidden, and scoping is not put in danger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry for maybe making such a basic question but in ASP.NET websites what
Here is a question for my Father. He has been using VBA in Excel
Making echo of a question around the web: Is the syntax for svn:ignore patterns
I apologize for making my first question not the hard-hitting code-related question I was
Simple RoR question...I am learning ROR and am making a simple voting app. The
I'm reviving this question, and making it more specific: Is there a .NET framework
I am making a webapp. I have a fairly basic question about javascript performance.
I am making a flash card application. It shows the question and then a
I have a conceptual question... I am making an Intranet application (Web platform) for
Right now I'm making an extremely simple website- about 5 pages. Question is if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.