Menu

Inconsistent behavior for proc value assignment

Anonymous
2017-05-19
2017-08-30
  • Anonymous

    Anonymous - 2017-05-19

    Hello,

    I'm playing around with assigning proc values to variables but not sure what I'm doing wrong.

    In the code below I get the following error:

    test.icn:11: # "x" unclosed literal or missing operator

    Seems my assignment to "foreach" doesn't work, whereas the one for "collect" does. I can't spot the difference.

    procedure main()
        local x
        foreach := every
        collect := tab
        "123456abcde" ? { 
            x := collect(many(&digits)) 
            }
        write(x)
    
        foreach x := !"abcdefgh" do {
            write(x)
            }
    
        return
    end
    
     
  • Jafar

    Jafar - 2017-08-30

    foreach := every doesn't do what you think it does. "every" is a reserved word and can't be assigned litrally. What you get is the resulat of evluating the expression "every collect := tab". which will be asisnged to foreach. What you want to do is"

    $define foreach every

    at the top, that will give you the behavior you are aiming for.

     
  • Brian Tiffin

    Brian Tiffin - 2017-08-30

    Anon,

    every is not a procedure, it's a reserved word, so it'll need a different way of creating the alias

    Preprocessor might do

    ::unicon
    $define foreach every
    
    procedure main()
        local x
    
        #foreach := every
        collect := tab
    
        "123456abcde" ? { 
            x := collect(many(&digits)) 
            }
        write(x)
    
        foreach x := !"abcdefgh" do {
            write(x)
            }
    
        return
    end
    

    but that is maybe not what you are aiming for?

    An expression like write(type(foreach) with your orginal would show a null as foreach would be assigned the result of an empty every expression. (Or that form of every fails, and you end up with a new variable reference when you look at it?? need to run some experiments).

    Plus, be careful redefining collect, it's already a builtin for the garbage collector. You can still get at the original with proc("collect", 0) though.

    Edit: shouldn't leave reply windows open for too long; Hard working people like Jafar come by and make the info look redundant. ;-)

    Cheers,
    Brian

     

    Last edit: Brian Tiffin 2017-08-30

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB
Gen AI apps are built with MongoDB Atlas
Atlas offers built-in vector search and global availability across 125+ regions. Start building AI apps faster, all in one place.
Try Free →