Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set const

    Hello,
    New to Mata, and do not seem to find the answer for a simple question.

    In Mata, how to declare a const variable (not the kind in regression models) but the one that most modern programming languages use to name a value eg in Pascal
    const ExchangeRate = 1.14;
    Thanks,

  • #2
    I think you should be more specific about why you want to define a particular variable as const and what kind of behavior you expect. I am not an expert in Pascal (and I guess few of us are). If you want to define a (scalar) variable to be available to other functions of your program, then you can define it as external (help [M-2] declarations).

    Code:
    void main()
    {
    
    external real scalar ExchangeRate 
    
    ExchangeRate = 1.14
    
    }
    
    real scalar foo(x)
    {
    
    y = ExchangeRate*x
    
    return(y)
    }

    Comment


    • #3
      Thanks for your reply.
      I would like to define named constants to avoid hardcoding certain values. For example,

      const real scalar macos=1
      const real scalar linux=2
      const real scalar win=3

      then elsewhere,

      if (os==macos) {
      }
      else if (os==linux) {
      }

      I guess I could use a Stata scalar definition before outside the Mata code block, but I would like to be sure that no one will try to redefine or change the value of a const.
      Thanks

      Comment


      • #4
        I have also considered using 'compile-time' macros

        local macos=1 etc..

        The problem is the code I am writing will be maintained by programmers who are allergic to using back ticks, e.g., `macos'. Avoiding macros is one of the reasons we decided to use Mata.

        I think the readability of Mata code will be greatly enhanced if we had consts (as above) and typedefs (so one would write:

        typedef myStringVector string rowvector

        then

        myStringVector somefunction () {... }

        Comment


        • #5
          Both const and typedef are reserved words in Mata, which implies that StataCorp is considering—or at least has considered—implementing them. Neither as far as I know has got any further than that. You might want to post on one of the Wish List threads to add these.

          Comment


          • #6
            If you plan to use compile-time macros I would use global macros instead of locals (or a header file with the defintion of your macros). Globals have the advantage to be available to every do-files.

            Comment


            • #7
              I have a silly question Salah : why defining your value as a scalar doesn't fit your needs?

              Code:
              ExchangeRate =1.14
              This way this ``variable" has only one value, and is not a variable in the Stata sense (the kind you call for regression models), and you could use it without using back ticks, as it is not a local nor a global (Although their use is sometimes the best option as suggested above.)

              I simply don't understand why a scalar isn't appropriate, perhaps I'm missing something.

              Comment


              • #8
                Originally posted by Charlie Joyez View Post
                I have a silly question Salah : why defining your value as a scalar doesn't fit your needs?. . . I simply don't understand why a scalar isn't appropriate, perhaps I'm missing something.
                It's not a silly question. The answer is that someone (or some function or some subroutine, as a side effect) can change a scalar at any time during program execution, and the author of the application cannot prevent it. With a constant, it is fixed—although it can be used in the module, its value cannot be altered at any time during program execution. There are situations where it is vital that certain (often, globally accessible) values cannot be altered, intentionally or accidentally, during program execution.

                Many (most) programming languages allow for such a situation, in the form of a const, or equivalent, declaration. Mata does not (yet) have that ability.

                Comment


                • #9
                  Ok, thank you Joseph for the explanation. Clear and instructive!

                  Comment


                  • #10
                    Thanks everyone for your replies,
                    Unfortunately, I ran into other issues and decided to implement my parser in a general purpose language (Go). The challenges included complier messages not specific enough to permit debugging, inconsistent output (might have to do with what routines were loaded at the time although I was using cscript) and limited information online (compared to what is available to for example R or Python).

                    For fairness, these are not specific to Mata. I have similar issues in the past using R and Python. For certain applications, there is no substitute to statically-typed and rigorously implemented general programming languages.

                    ​​​​​​​Cheers,

                    Comment

                    Working...
                    X