cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
msharp
Super User (Alumni)

Opposite of Default Local

So, just some syntactical sugar.

It would be really nice if there was an opposite of declaring local variables for functions.

i.e. instead of:

example= function({a,b}, {c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w},

       c = a+b;

       d = c+a;

       //...

       x = v+w;

       y = x+b;

);

print(x,y);

I could write:

example= function({a,b}, {Default Local, x, y},

       c = a+b;

       d = c+a;

       //...

       x = v+w;

       y = x+b;

);

print(x,y);

5 REPLIES 5
msharp
Super User (Alumni)

Re: Opposite of Default Local

Another example:

x="global scope";

y="global scope";

Names Default to Here(1);

x="here scope";

y="here scope";

print(::x, x, ::y, y);

f = function({}, {Default Local, x},

       x="here scope replaced";

       y="function scope";

       print(x, y);

);

f();

//x != y but it does

print(x, y);

Phil_Brown
Super User (Alumni)

Re: Opposite of Default Local

I think if you scope everything, you may get what you want.. Correct me if I'm not following but....For instance

::x="global scope";

::y="global scope";

Names Default to Here(1);

x="here scope";

y="here scope";

print(::x, here:x, ::y, here:y);

f = function({}, {Default Local},

       here:x="here scope replaced";

       ::x = "global scope replaced";

       x = "function scope x";

       y = "function scope y";

       print(here:x, ::x, x,  y);

);

f();

print(x, y); // will be whatever was assigned to these vars in the HERE namespace.

print(::x, ::y); // will be whatever was assigned to these vars in the GLOBAL namespace.


// no way of accessing local vars (actually the point of local).



PDB
Craige_Hales
Super User

Re: Opposite of Default Local

I've been using explicit locals in functions.  I think I'll try Default Local in my next project.

Craige
msharp
Super User (Alumni)

Re: Opposite of Default Local

I've never used here:x before, but that syntax makes total sense.  Thanks!

vince_faller
Super User (Alumni)

Re: Opposite of Default Local

Same goes with local:x if you'd rather explicitly scope on the fly.

f = function({}, {},

       x = "global scope replaced";

       local:x = "function scope x";

       local:y = "function scope y";

       print(::x, local:x,  y);

);

f();

You have to remember to call ::x explicitly because of the scoping rules for unscoped variables.

12892_pastedImage_69.png

Vince Faller - Predictum