Use strings to create words and paths in Red language

前端 未结 3 653
春和景丽
春和景丽 2021-01-21 17:21

I have strings in a namelist, that correspond to variables as well as field names in the application.

The function should read strings from namelist, add an

3条回答
  •  滥情空心
    2021-01-21 17:57

    Step 1: refactor

    Here is your code reformatted and print (1) statements added:

    namelist: ["var1" "var2"]
    var1: 5
    var2: 10
    
    process: [
        print "process: start"      ; (1)
        repeat i length? namelist [
            (to-set-path compose rejoin [namelist/:i "f/text"] (to-word namelist/:i))
        ]
        print "process: end"        ; (1)
    ]
    
    lay: layout [ 
        text "Values to appear here: "
        var1f: field "a"
        var2f: field "b"
    
        button "Click" [do process]
    ]
    
    view lay
    

    When I run this in the console and press "Click", it gives the following:

    process: start
    process: end
    

    So I know at least the button works

    Step 2: debug with print

    Now I can focus, moving print inside the code block:

    process: [
        repeat i length? namelist [
            print (
                to-set-path compose rejoin [
                    namelist/:i "f/text"
                ] (to-word namelist/:i)
            )
        ]
    ]
    

    Almost immediately I can see what's wrong here :

    var1    ; expecting `var1f` here
    var2    ;
    

    Step 3: we need to go deeper with probe

    Aside

    Now, before I proceed further, notice that this code doesn't access anything inside the view block (because it doesn't work!). But the nice thing here is you could ignore this and come back to it later.

    What you need is a way to access var1f/text programmatically

    Keeping that in mind, here is a better way to phrase this question:

    Step 3a: how to dynamically create objects with different names and set values to them?

    var1f/text: 5

    (given the code in step 2)

    Now, I reach a conundrum here. This would probably be best asked as a different, simpler question.

    I decided to continue assuming you accomplished this (there's another answer too)

    Note

    The important thing to take home in this step is the datatype Red view uses and what you're working with is the same thing: red objects. There is no difference (all are instances of a simple face object)

    Step 4: you're done! Or are you?

    So you're able to create the gui you want for your work and you're done! Right?

    But then you ask yourself, is this the best way to do it? What if you want to add some more of this, or something else entirely?

    1. You have read the official gui docs especially the part about view engine
    2. You've looked at examples of vid and adding view face objects manually
    3. You've looked at the repo on github for sample code and small apps
    4. You've even tried the old, but stable rebol2

    But you still don't get it? Don't despair, this is normal. A lot of stuff have names that are conceptually similar to what you are familiar in other languages but are different in subtle ways which tends to make them really different. In the end tho, a lot is simpler than you'd think but stranger(having deeper implications)

    tl;dr

    • Separate your view code from the rest so it's easier to debug
    • Use print, probe and dump-face to debug

提交回复
热议问题