Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Passing arguments to a function in another script

 [1/9] from: warren:howsoft at: 7-Feb-2008 13:48


Hi All, Here is a script called arithfunc.r: Rebol[] sum: func [arg1 arg2] [ addem: arg1 + arg2 alert to-string addem ] ------------------------------------ In the same folder, I can successfully call the above function from another script and pass its arguments as follows: Rebol[] mylay: layout[ button [do %arithfunc.r sum arg1: 3 arg2: 2] ] view mylay ------------------------------------ Using the above, the Alert in the function correctly reports the result "5". However, I would like to set the arguments above to variables rather than citing the literals "3" and "2" as I have done above. The following, for example, DOES NOT WORK: Rebol[] mylay: layout[ firstvar: 3 secondvar: 2 button [do %arithfunc.r sum firstvar secondvar] ] view mylay ------------------------------------ Nor does this, or anything else I have tried: Rebol[] mylay: layout[ firstvar: 3 secondvar: 2 button [do %arithfunc.r sum arg1: firstvar arg2: secondvar] ] view mylay ------------------------------------ All the above demonstrates a wonderful noob confusion, how typical I don't know. I am obviously missing something! Even in the version that works (using literals and not variables to pass the arguments), I am not clear about why I need to say:- button [do %arithfunc.r sum arg1: 3 arg2: 2] - and NOT: button [do %arithfunc.r sum 3 2] Can anyone show me how to pass the arguments to the function in the other script with the use of variables? Thanks. Bob

 [2/9] from: warren:howsoft at: 7-Feb-2008 14:19


Just for the record, I have discovered that the following also works. I think I am discovering that "Core" and "Vid" are not as integrated as I am assuming, or something like that. Rebol[] mylay: layout[ tx1: text "3" tx2: text "2" button [do %arithfunc.r sum arg1: to-integer tx1/text arg2: to-integer tx2/text] ] view mylay --------------------------------------------- Bob

 [3/9] from: gregg::pointillistic::com at: 7-Feb-2008 9:53


Hi Bob, BW> mylay: layout[ BW> firstvar: 3 BW> secondvar: 2 BW> button [do %arithfunc.r sum firstvar secondvar] BW> ] BW> view mylay When you run this code, you should see two errors about misplaced items in the console. Is that not happening? You can't set the vars inside LAYOUT, because LAYOUT is a dialected function. True, it understands plain REBOL code in certain areas, and you can set words to face references, but you want to separate your regular code from your UI layout code. This works: firstvar: 3 secondvar: 2 mylay: layout[ button [do %arithfunc.r sum firstvar secondvar] ] view mylay -- Gregg

 [4/9] from: warren:howsoft at: 7-Feb-2008 15:51


Thanks a lot Gregg - once again!
>When you run this code, you should see two errors about misplaced
items in the console. Is that not happening? No, 'cos I was using Linux rather than Windows, and their ain't no Rebol console! For the same reason, I have to use "Alert" rather than "Print" for testing purposes.
>You can't set the vars inside LAYOUT, because LAYOUT is a dialected
function. True, it understands plain REBOL code in certain areas, and you can set words to face references, but you want to separate your regular code from your UI layout code. In fact, I tried a version similar to yours and found it not to work for some reason. In my state of confusion I must have done something stupid though: your version certainly works. Since I am starting my learning with Vid rather than Core, this probably encourages me to regard Core as a subset of Vid, which it is not. I would certainly be happier if it were! As you say, VID seems to understand Core in certain areas, but a noob doesn't know which ones! The fact that I cannot set two simple numeric variables in Vid is disappointing. However, from what I have read about the great advantages of dialects, I think that the experts are likely to disagree with my reactions expressed above, and I am in no way qualified to judge this issue at my stage of learning. Nevertheless, my beginner's difficulty might provide some food for thought. By the way, the reason I am studying this is because I want to use the script for converting to/from UTF-8 that you recommended to me yesterday**. Rather than embed this script (or selected functions from it) in my own script, I want to keep it separate. [** One thing to clarify about this issue is that the file names with accents are certainly displayed correctly by the Ubuntu File Manager. So why I should need to convert them to Western (ISO-8859-15) for Rebol to read them correctly perhaps requires a bit of elucidation.] Best regards, Bob ------------------------------------------------------ Gregg Irwin wrote:

 [5/9] from: gregg:pointillistic at: 7-Feb-2008 11:21


Hi Bob, BW> No, 'cos I was using Linux rather than Windows, and their ain't no Rebol BW> console! For the same reason, I have to use "Alert" rather than "Print" BW> for testing purposes. Hmmm. I would think it should certainly warn you somehow. That's not good. BW> Since I am starting my learning with Vid rather than Core, this probably BW> encourages me to regard Core as a subset of Vid, which it is not. I BW> would certainly be happier if it were! As you say, VID seems to BW> understand Core in certain areas, but a noob doesn't know which ones! Core is the REBOL language itself, View is the GUI system, and VID is a dialect that is only available in View. Core is not a subset of VID, you are correct, it's more like the other way around, but not exactly. Not exactly because dialects can use different syntax than plain REBOL and are more specific in what they let you do. If you read the View/VID docs, it should give you a good foundation. You can't really separate thing entirely, because you can write functions that are "plain REBOL" which operate on View/VID elements (faces). Faces are just objects. Starting with VID *can* be a little tricky at times, and there are a number of subtleties that can trip you up or help you out (e.g. knowing there is an implicit FACE value in the action block of a face). Keeping code outside the LAYOUT is the right way to do things, it's not a limitation. If you want to protect things in namespaces, use objects. -- Gregg

 [6/9] from: warren:howsoft at: 7-Feb-2008 17:27


Thanks for the great advice, Gregg. While we are on the subject of the View/VID docs, there are a few recommendations I would like to make: 1) In whatever documents, "action" blocks only ever seem to contain Prints or Alerts as examples. e.g. button red [print "You clicked me!"] (a) I have more or less come to the conclusion that such "action" blocks need to contain FUNCTIONS of some sort, but even so I am still hesitant about making such a sweeping generalisation. (b) Normally, such example blocks do not show how to get values from other faces. (And getting values from other faces is not always as consistent as a noob would imagine.) (c) Nor do such blocks usually demonstrate how to change other faces. I do, of course, appreciate that the aim of such examples might be to maintain the clarity of the code's skeleton. However, it seems that there are no examples of the above blocks in any documents at all. And when one examines a real program to see how they are used, the whole thing is often so complex that the noob (at least this one) gives up trying to analyse it. 2) Although some docs have a liberal amount of coding examples, others do not. Or the examples do not cover all the various different usages involved. For a noob, an example is often worth a thousand words, so I suggest increasing them. There can never be too many examples! Hope you don't mind my 2 cents at this stage. Experts often come to regard many things as common sense, not requiring explanation. They are wrong, of course. However, since I am likely to remain a Rebol dummy for the next 15 years or so, you can profit from my status any time you like! Then eventually, perhaps Rebol can really become a useful RAD tool for busy non-technical people such as doctors, lawyers, etc., and not a language for computer specialists only. Regards, Bob Gregg Irwin wrote:

 [7/9] from: gregg:pointillistic at: 7-Feb-2008 13:17


Hi Bob, BW> (a) I have more or less come to the conclusion that such "action" blocks BW> need to contain FUNCTIONS of some sort, but even so I am still hesitant BW> about making such a sweeping generalisation. Action blocks will contain whatever you need them to, just as event handler code in VB would. They can contain regular REBOL code. You don't normally *define* functions in action blocks, but you can certainly call them from there, and that is a common idiom. BW> (b) Normally, such example blocks do not show how to get values from BW> other faces. (And getting values from other faces is not always as BW> consistent as a noob would imagine.) BW> (c) Nor do such blocks usually demonstrate how to change other faces. Basically, you reference a face with a variable and use the get-face/set-face accessor funcs (there are also clear-face and reset-face funcs). view layout [ rad: radio-line "Hello World!" b: btn "Toggle" [set-face rad not get-face rad] ] I should point out that there is often more you need to do when it comes to some styles, and that's one of the biggest problems with VID; needing to know how some things work internally. You can see the source to all styles, but it takes time and effort to understand those things. BW> Hope you don't mind my 2 cents at this stage. Keep it coming. Input from people new to REBOL is vital. It reminds us of the holes we saw when we were new, but have now forgotten about. -- Gregg

 [8/9] from: compkarori:gmai:l at: 7-Feb-2008 21:21


We're hopeful that VID3 with REBOL3 will fix the need to know the internals of VID to use it. On Feb 8, 2008 9:17 AM, Gregg Irwin <gregg-pointillistic.com> wrote:
> I should point out that there is often more you need to do when it > comes to some styles, and that's one of the biggest problems with VID; > needing to know how some things work internally. You can see the > source to all styles, but it takes time and effort to understand those > things.
-- Graham Chiu http://www.synapsedirect.com Synapse-EMR - innovative electronic medical records system

 [9/9] from: warren::howsoft::com at: 7-Feb-2008 19:16


Thanks for a very profitable day of explanation, Gregg. Bob Gregg Irwin wrote: