1
$\begingroup$

I want to make and use object like stuff of Object-Oriented Programming language(like JavaScript) in Mathematica and am trying it by referring to the answer of this question using Module.

For example, I want to make an object(Module) which has the following properties and Methods:

Properties:

  • a : 2 (integer)
  • b : 4^a
  • f : func[a], with func: function which just returns an argument doing nothing else. (func : x -> x)
  • c = 4^f

Methods:

  • showA: returns a
  • showB: returns b
  • showC: returns c

I wrote a code below, but the result was not what I had expected:

In[529]:= func[x_] := (
  Return[x]
  )

foo = Module[
   {
    a = 2,
    b = 4^a,
    f = func[a],
    c = 4^f
    },
   Switch[#,
     "showA", a,
     "showB", b,
     "showC", c
     ]
    &];
foo["showA"]
foo["showB"]
foo["showC"]

Out[531]= 2

Out[532]= 1048576 //expected output: 16

Out[533]= 4^f //expected output :16

What is wrong? If it is impossible to do, what is the alternative simplest way to achieve this?

Any information would be appreciated.

$\endgroup$
4
  • 3
    $\begingroup$ the bound variables in module can't refer to each other within the first argument; so, Module[{a = 3, b = 4 a}, f[a,b]] would give f[3, 4a], because the a on the rhs of b = 4 a is not the "module version" of a, but the external a. (You can notice this from the syntax highlighting—it's blue, not green.) $\endgroup$
    – thorimur
    May 12, 2021 at 5:28
  • 1
    $\begingroup$ Also, i'm not sure func[x_] := (Return[x]) means what you might think it means! Return is only used for control flow changes in mathematica, like breaking out of For loops or sequential evaluation (;). You'll still get the expected output from func in this case, but you may as well use func[x_] := x. $\endgroup$
    – thorimur
    May 12, 2021 at 5:34
  • $\begingroup$ Thank you. I didn't know {} inside Module refers only to external variables. $\endgroup$
    – ten
    May 12, 2021 at 7:47
  • $\begingroup$ The reason I used Return instead of writing func[x_] := x is just I wanted to know whether it works as a "return" keyword of other ordinary programming languages. The function I need in my actual project requires much more to do than func[x_] := x (including For loop calculation) and I thought I needed to use Return for returning the result. $\endgroup$
    – ten
    May 12, 2021 at 7:58

1 Answer 1

0
$\begingroup$

The code below is the most minimal re-writing of the OP that I could come up with

func[x_] := x
foo = Module[{a = 2},
   b = 4^a;
   f = func[a];
   c = 4^f;
   Switch[#, "showA", a, "showB", b, "showC", c] &];
foo["showA"]
foo["showB"]
foo["showC"]

and gives the following as output

2
16
16

Is this what you were looking for?

$\endgroup$
4
  • $\begingroup$ Thank you. It worked. I didn't know {} inside Module refers only to external variables. $\endgroup$
    – ten
    May 12, 2021 at 8:11
  • 2
    $\begingroup$ But b, f, & c are now global. I would declare them in the module header, but assign values in the body. $\endgroup$
    – mikado
    May 12, 2021 at 8:11
  • $\begingroup$ sorry, maybe I misunderstood what you meant earlier. Just to be clear, do you need all variables to be defined in the Module? $\endgroup$ May 12, 2021 at 8:20
  • $\begingroup$ Yes, it is preferable to trap variables in the Module for not coming upto global scope. $\endgroup$
    – ten
    May 15, 2021 at 0:40

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.