Alpha Video Training
Results 1 to 11 of 11

Thread: Explanation of fname as v (vars as P) & with

  1. #1
    Member
    Real Name
    Trevor Sayers
    Join Date
    Jan 2004
    Posts
    118

    Default Explanation of fname as v (vars as P) & with

    Hi folks,

    Please could someone explain to me...

    function functionname as v (vars as p)
    with vars


    I have read and read the manual & message boards and I understand...
    - namespaces,
    - local vs gobal etc. scope
    - dot variables
    - functions with "normal" variable types (n, c, d etc.)

    ...but...
    1. What is "v" as in "as v"? It is not one of the designated variable types. Does this mean we are just setting values for variables?


    2. Now that we have multi-level dot variables, are namespaces pretty much obsolete? If not, when/why would you use which?


    3. In script such as this one...
    http://msgboard.alphasoftware.com/alphaphorum/read.php3?sortby=lastreply&direction=desc&num=15&id=13053&thread=13053#MSG13094

    ... "vars" points to the local variables only...

    dim lv as p
    lv = local_variables()
    functionname(lv)
    ...
    function functionname as v (vars as p)
    with vars

    ...What if I wanted the function to access a local variable AND one in a different namespace as well?


    4. Why do you even have "with vars" in a function? Is the function not effectively a child script of the calling script and thus would have access to all variables in the calling script anyway?


    5. How would you convert a script such as this to take variables as p instead of as c?
    http://msgboard.alphasoftware.com/alphaphorum/files/200411/a5v6/07/6107/script1.txt

    It's clearly more than just
    lv = local_variables()
    tempstate_expanded = get_state_data(lv)
    and
    function get_state_data as V (vars as p )



    Thanks in advance.

    Regards,
    Trevor

  2. #2
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    1. What is "v" as in "as v"? It is not one of the designated variable types. Does this mean we are just setting values for variables?

    V is VOID, meaning this function does not return anything

  3. #3
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    2. Now that we have multi-level dot variables, are namespaces pretty much obsolete? If not, when/why would you use which?

    Namespaces, as defined in the Alpha Five documenation, each have a different scopt, or lifetime. Your dot variables live within one of these namespaces

  4. #4
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    3. In script such as this one...
    http://msgboard.alphasoftware.com/alphaphorum/read.php3?sortby=lastreply&direction=desc&num=15&id=13053&thread=13053#MSG13094

    ... "vars" points to the local variables only...

    dim lv as p
    lv = local_variables()
    functionname(lv)
    ...
    function functionname as v (vars as p)
    with vars


    dim lv as p = local_variables()
    dim ov as p =
    functionname(lv,ov)
    ...
    function functionname as v (vars as p, vars2 as p)
    with vars
    vars2.variable

  5. #5
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    4. Why do you even have "with vars" in a function? Is the function not effectively a child script of the calling script and thus would have access to all variables in the calling script anyway?

    No, a function call is not a child script, it runs in it's own variable frame. If you don't pass variables in, your function can't get to them unless they are global or shared.

    Even with a script, you would need to use script_play_local() instead of script_play() if you want to run it in the same variable frame.

  6. #6
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    5. How would you convert a script such as this to take variables as p instead of as c?
    http://msgboard.alphasoftware.com/alphaphorum/files/200411/a5v6/07/6107/script1.txt

    It's clearly more than just
    ...


    Actually that will do it if you do the with...end with. You'll also need to use tempstate instead of state as that is the variable name used outside the function. Or you can skip the with...end with and use vars.tempstate

    But why would you wnat to do this? Unless your function is going to manipulate a whole bunch of stuff, you should really just pass in what you need.

  7. #7
    Member
    Real Name
    Trevor Sayers
    Join Date
    Jan 2004
    Posts
    118

    Default RE: Explanation of fname as v (vars as P) & w

    Wow. Look at that you folks just checking out Alpha Five

    Try and get that kind of service from Filemaker or the illegal monopolists! That's just a part of the reason the folks here LOVE Alpha Five.


    Thanks so much, Lenny.

    Just two quick follow-up questions for the record:

    1. In the line...
    function functionname as v (vars as p, vars2 as p)
    ...is "vars" some kind of special or reserved word? i.e. do we have to use the word "vars" or can we use any non-reserved words e.g.
    function functionname as v (localvars as p, globalvars as p)


    2. From the lines...
    dim lv as p = local_variables()
    dim ov as p =
    functionname(lv,ov)
    ...
    function functionname as v (vars as p, vars2 as p)
    with vars
    vars2.variable

    ...where "vars2.variable" is inside "with vars" it looks like "vars2" might be subordinate to "vars", as if it were further down the chain of a dot.variable
    e.g.
    var.some-local-variable01
    var.some-local-variable02
    var.var2.some-variable-in-a-different-namespace01

    Is that correct? If so, do we need to pass the variables lv,ov etc to the function in any particular order e.g. local variables first, then global, then...?


    Thanks again.

    Regards
    Trevor





  8. #8
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    1) There is nothing special about "vars"

    2) vars2.variable is not a subordinate of vars. What happens is when you reference a variable within a with..end with block, the interpreter first looks for it within vars. If it is not found, it then looks up the stack outside of the with.

    I personally almost never use with. I'd rather explicitely reference a variable as vars.variable - that way i know exactly what variable I am working with, particularly a few weeks down the road when I revisit the code I wrote

  9. #9
    Alpha Software Employee AaronBBrown's Avatar
    Real Name
    Aaron Brown
    Join Date
    Feb 2001
    Location
    Waltham, MA
    Posts
    2,836

    Default RE: Explanation of fname as v (vars as P) & w

    You will find that over time passing the whole local variable frame is not a very good way to approach coding. It causes you to take shortcuts by not examining your design ahead of time. Basically, if a function requires use of more than a few passed variables, you need to reevaluate the design and probably break that function up into multiple functions. Essentially, passing local variables removes the abstraction and encapsulation parts of the design. Essentially, the fucntion now becomes a glorified macro.

    On top of that, they become very difficult to manage over time because it is unclear where variables are coming from. Are you modifying a variable that is changed somewhere else? If you change it here, will it have an unseen effect? What if you are calling this function in a for loop? Now you have a for loop inside the function. You now have to worry about the name of your iterator variable otherwise everything will get all messed up. The function is no longer a function, it's just become a shorthand.

    Instead, if there are a large number of variables that you know need to be used inside several functions, I would create a dot-variable structure specifically for those variables to be passed around rather than using local_variables().

  10. #10
    Member
    Real Name
    Trevor Sayers
    Join Date
    Jan 2004
    Posts
    118

    Default RE: Explanation of fname as v (vars as P) & w

    That's pretty much the conclusion I had come to i.e. that using "with vars" adds a extra layer of complication/abstraction for little apparent gain and thought I was missing something. Apparently not.

    When you're (both) talking about referencing the variables directly and using dot variables, am I correct in thinking taht the structure you're talking about is as follows & no "with" statement?


    dim a.x as c
    dim a.y as n
    dim a.z as p

    functionname(a.x,c.z,b.y)
    ...
    function functionname as v (var1 as c, var2 as p, var3 as n)
    someothervar = a.x
    a.y = someothervar2
    end function


    Thanks again.

    Regards,
    Trevor

  11. #11
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,699

    Default RE: Explanation of fname as v (vars as P) & w

    Not quite. Once you are inside the function, there is no more a.x and a.y. Instead your function has var1, var2 and var3 available to it.

Similar Threads

  1. Export Fields AND VARS
    By David Boomer in forum Alpha Five Version 6
    Replies: 3
    Last Post: 07-27-2005, 09:38 AM
  2. explanation of formula
    By Ian_Edwards in forum Alpha Five Version 6
    Replies: 3
    Last Post: 09-16-2004, 10:02 AM
  3. WITH ... END WITH and dot vars
    By Marcel Kollenaar in forum Alpha Five Version 5
    Replies: 3
    Last Post: 07-08-2004, 10:09 AM
  4. Synch 2 vars
    By Peter.Greulich in forum Alpha Five Version 5
    Replies: 4
    Last Post: 10-16-2002, 11:26 AM
  5. Join Table Explanation Please
    By forskare in forum Alpha Five Version 4
    Replies: 2
    Last Post: 08-01-2001, 03:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •