Re: Pass by Reference

new topic     » goto parent     » topic index » view thread      » older message » newer message

It's taken some time to get back to my original code, and I'm starting to rewrite it to use functions to return multiple values.

It's verified my initial concern: the syntax is unsafe.

As a counter-example to the idea that a sequence is "more Euphoria-like", have a look at parameter lists. In theory, we could pass all the parameter lists as an implied sequence:

function foo() 
   -- assign parameters to the arglist 
   {p1, p2, p3} = arglist 
end function 

But this isn't how Euphoria works. One of the reasons for this is because it's important to match the number of parameters in the function call.

The parser knows how many values are expected to be passed in, and if they don't match it's an error. The parser knows this because the syntax of a function call parameter list reflects the intent.

Similarly, if we want to support multiple parameters coming back from a function, the syntax should support the intent.

Further, it's not "simple" to have functions returning single values use one syntax, and functions returning multiple values and have a different syntax.

It's necessary, but it doesn't really reflect the intent.

As another counter-example example, consider the for loop:

    for i = 1 to 10 do 
        -- do stuff 
    end for 

Everything the for loop does could be done with a while structure. But because it's a common idiom, not only does Euphoria have it, but it doesn't even require you to declare the variable outside the loop.

So while Euphoria has a tendency towards minimalism, that doesn't require that there be no amenities in the language.

I also think the syntax in the function call should match the return statement, and not "silently discard" the "extra" values. Instead, it should explicitly specify the values to be dropped:

a,,c = foo() 

Without this, the interpreter would have to guess what the intent of this is:

-- did the user forget that foo() returns multiple values? 
a = foo() 

I'm not arguing that the new syntax be removed:

{a, b, c, ... } = ... 

This is a very useful feature.

Behind the scenes this can be implemented as exactly the same functionality.

But by making the syntax explicit, the result is a lot safer, because the syntax reflects the intent, and catches bonehead errors.

- David

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu