Default value in function parameters?

classic Classic list List threaded Threaded
39 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Default value in function parameters?

Grellier, Thierry

Hello,

 

A quick search didn’t gave me much hints on this. But I guess it must have already discussed…

my understanding is that this is not part of the Lua grammar. Hence one shall write something like:

 

function f(a, b, c)

  a = a or my_default_a

  b = b or my_default_b

  c = c or my_default_c

end

 

So this is possible to obtain what we want, although a little verbose. I’m however thinking of a syntax sugar like that:

 

function f(a or my_default_a, b or my_default_b, c or my_default_c)

end

 

Would it be too expensive or unlikely to add this to the language?

I understand that one might judge it error prone when calling a function with the false assumption that gaps will be filled. But I guess a kind of lua lint could be used to track this. And well anyway this problem is already here…

 

Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Andreas Stenius-2
Grellier, Thierry skrev:
[...]
I’m however thinking of a syntax sugar like that:

Token Filters are experts on syntax sugar!
It's a few lines of quite trivial code to acheive what you ask for.
The drawback? it requires a patched lua.


Reply | Threaded
Open this post in threaded view
|

Patch+patch+patch=>lua 5.2? was Default value in function parameters?

Grellier, Thierry
Well, I know we can patch, and I'll do it when really necessary. But
- I'm not sure of the merge of all patches
- it isn't that easy convincing management that using lua is worth the
maintenance overhead compared to other scripts languages. 
- this reduces chances of using LuaJIT if it appears that it would help.
- more generally this reduces chances to use any third part tool
(debugger, lint, swig, tolua, whatever) which already are weak enough...

This change is minor, because there are ways to obtain the behavior. I
have yet a major patch in Lua already to support overloadable bitwise
operators. 
I've exposed the patch in the list. I've understood that not every body
would like to have this support, so this can be disabled/enabled within
lua_conf.h on demand (but with byte code incompatibility). Thus
including changes like can be almost not sensitive.

Then I just wonder what would be the latency to integrate patches into
reference implementation. That is the absence of roadmap and evolution
procedure doesn't give much visibility on language, which is a drawback.

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Andreas
Stenius
Sent: Tuesday, September 19, 2006 1:11 PM
To: Lua list
Subject: Re: Default value in function parameters?

Grellier, Thierry skrev:
[...]
> I'm however thinking of a syntax sugar like that:

Token Filters are experts on syntax sugar!
It's a few lines of quite trivial code to acheive what you ask for.
The drawback? it requires a patched lua.



Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

mitchell-3
In reply to this post by Grellier, Thierry
Hi,
  
> function f(a or my_default_a, b or my_default_b, c or my_default_c)
> 
> end

What about passing false intentionally?

function f(flag or true) ... end

f(false)

-Mitchell;


Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Andy Jones
Myself, I don't really see the advantage to this sugar.

I think it's shorter, but less clear.

My 10c.

On 9/19/06, mitchell <[hidden email]> wrote:
Hi,

> function f(a or my_default_a, b or my_default_b, c or my_default_c)
>
> end

What about passing false intentionally?

function f(flag or true) ... end

f(false)

-Mitchell;




--
It's Like This

Even the Samurai
Have teddy bears
And even the teddy bears
Get drunk

Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Grellier, Thierry
In reply to this post by mitchell-3
Yes, you are right !
At least this shows that using default values is currently not so easy.
Then the issue is not to provide a syntax sugar but to reuse "or" token
to implement it. Maybe "else' is a better choice.

function f(a else my_default_a, b else my_default_b, c else
my_default_c)
to be expanded to
function f(a, b, c)
a = a or (a == nil and my_default_a)
b = b or (b == nil and my_default_b)
c = c or (c == nil and my_default_c)

There is also the possibility to introduce a new default keyword (gasp).
function f(a default my_default_a, b default my_default_b, c default
my_default_c)
This could have at least the advantage to make call safer:
f(false, default, 3)
with default expanded to nil in this case

I hope that people are not used to name variables with so poor name, so
that it doesn't break many programs...

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of mitchell
Sent: Tuesday, September 19, 2006 2:30 PM
To: [hidden email]
Subject: Re: Default value in function parameters?

Hi,
  
> function f(a or my_default_a, b or my_default_b, c or my_default_c)
> 
> end

What about passing false intentionally?

function f(flag or true) ... end

f(false)

-Mitchell;



Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Vyacheslav Egorov-2
Grellier, Thierry wrote:
Maybe "else' is a better choice.
Take a look at this http://docs.python.org/dev/whatsnew/pep-308.html.
Do you really want something like that happen with Lua?

--
e.v.e


Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Paul Hudson-2
In reply to this post by Grellier, Thierry
Default is a perfectly reasonable name for a default in a language that does
not reserve the word default (phew :)

I don't see the need for this syntactic sugar. The current ways of doing
seem clear enough to me.

P.

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Grellier, Thierry
Sent: 19 September 2006 14:57
To: Lua list
Subject: RE: Default value in function parameters?

There is also the possibility to introduce a new default keyword (gasp).

I hope that people are not used to name variables with so poor name, so
that it doesn't break many programs...




Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Grellier, Thierry
In reply to this post by Vyacheslav Egorov-2
Just let me return you the question: why you would really not like
something like this to happen?

I think that conditional expressions are extremely useful when it deals
with configuration which is the initial aim of Lua after all. If
properly use it can make the code more readable, especially for non
programmers. 
To my mind, a high level programming language's goal is to make thing as
close as possible to natural language.

So, I would prefer to be able to write something like (VHDL flavor, and
yet another even more disturbing keyword :-) knowing that if elseif...
could be reused)
style = "native"
a = { a = 1,
      b = { b = 2,
            c = 1 when style == "native",
                2 when style == "rock",
                0 else
          }
     }
rather than
style = "native"
a = { a = 1,
      b = { b = 2,
          }
     }

if style == "native" then
  a.b.c = 1
elseif style == "rock" then
  a.b.c = 2
else
  a.b.c = 0
end

or

style = "native"
a = { a = 1,
      b = { b = 2, c = f(style)
          }
     }
function f(style)
...
end

which disseminates the code, and well looks like coding.

But this has probably a noticeable impact on grammar. But I wonder, how
many people with hard size constraints really embed lua compiler? 

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Vyacheslav
Egorov
Sent: Tuesday, September 19, 2006 4:45 PM
To: Lua list
Subject: Re: Default value in function parameters?

Grellier, Thierry wrote:
> Maybe "else' is a better choice.
Take a look at this http://docs.python.org/dev/whatsnew/pep-308.html.
Do you really want something like that happen with Lua?

-- 
e.v.e



Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Grellier, Thierry
In reply to this post by Paul Hudson-2
I think this is poor identifier, because default has no meaning by
itself, this is always the default of something, and hence an identifier
shall also include the default of what it is. There can be many things
with default.  So it is better naming default_something than just
default.

This is also poor because often used as a keyword in languages, notably
C which Lua interfaces too. When using translators, this may lead to
name mangling.

I find it short to say you don't see the need. 

Do you never use this mechanism when it is present in a language (like
C++, python...) because you consider this is a poor construct, pruning
bad design? 
(I've seen interesting article claiming than default value was sign of
over specification or too much generalization, but more in API context)

and if not, do you think that the complexity involved in parser would
make you miss your code size targets?

This is also paradoxical. Because, when you don't use it, having it
present or not have no incidence on you providing that default keyword
isn't use, but else or that you don't name variables with default
intensively.


-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Paul Hudson
Sent: Tuesday, September 19, 2006 5:46 PM
To: 'Lua list'
Subject: RE: Default value in function parameters?


Default is a perfectly reasonable name for a default in a language that
does
not reserve the word default (phew :)

I don't see the need for this syntactic sugar. The current ways of doing
seem clear enough to me.

P.

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Grellier,
Thierry
Sent: 19 September 2006 14:57
To: Lua list
Subject: RE: Default value in function parameters?

There is also the possibility to introduce a new default keyword (gasp).

I hope that people are not used to name variables with so poor name, so
that it doesn't break many programs...





Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Rici Lake-2
In reply to this post by Grellier, Thierry

On 19-Sep-06, at 11:45 AM, Grellier, Thierry wrote:

So, I would prefer to be able to write something like (VHDL flavor, and
yet another even more disturbing keyword :-) knowing that if elseif...
could be reused)
style = "native"
a = { a = 1,
      b = { b = 2,
            c = 1 when style == "native",
                2 when style == "rock",
                0 else
          }
     }

I actually like "when" (although i use it with "otherwise" because "else" is already in use).

But more Lua like would be:

a = { a = 1,
      b = { b = 2,
            c = {native = 1, rock = 2}[style] or 0
          }
    }

By the way, all this discussion over new keywords does strike me as an issue.

I'd suggest that one way of handling it (for token filter folks) would be to
implement a declaration like this:

feature <keyword> ... end

(or maybe:   --$feature <keyword> ... end )

and only do transformations using a keyword if it had been declared in
a feature statement in that chunk. (The workaround for the standalone
interpreter would be a hook that prepended an appropriate feature
declaration to every chunk passed to the compiler.)


Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Paul Hudson-2
In reply to this post by Grellier, Thierry
>I find it short to say you don't see the need. 

I'm not sure how much more I need to add. Anyway...

I find I do not have default values often in functions that I write in Lua,
or in other languages that do support default values explicitly. I'm not
sure why this is, but perhaps it's because I think default values are often
indicators of what "Writing Solid Code" calls a "candy machine interface".

Even if I did use default values a lot, the value of the proposed syntactic
sugar seems quite low - it doesn't really help reduce errors, and doesn't
increase clarity significantly(in my view, anyway).

I think the real value of such a feature comes when the programming language
or environment allows the developer to see the signature of the function
(e.g. with Visual Studio's Intellisense) - i.e. then it is a documentation
convenience to have it in the signature declaration, but Lua doesn't have
function signature declarations.

On the "default" keyword, I'm not a fan of long names for variables local to
a function; depending on the context "default" may well be a reasonable
name.

More generally, I'm also not a fan of adding more keywords/syntactic sugar
to Lua, unless there are some very compelling reasons.

Lua is mostly a "there's one way to do it" language, on the opposite end of
the spectrum from, say, Perl, where "there's more than one way to do it" is
an accepted principle. http://www.wall.org/~larry/onion/talk.html

Both approaches have value (I'm not as down on Perl as some members of this
list), but they're different, but a mixture can be confusing and complex. I
like the spare, almost austere feel of Lua's syntax, and I think it should
be sugared only very seldom.

Long enough? :)

P.




Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

mitchell-3
In reply to this post by Grellier, Thierry
Hi,

> function f(a, b, c)
> a = a or (a == nil and my_default_a)
> b = b or (b == nil and my_default_b)
> c = c or (c == nil and my_default_c)

function f(a, b, c)
  a = a == nil and my_default_a or a
  b = b == nil and my_default_b or b
  c = c == nil and my_default_c or c
end

I think that looks more readable, although it doesn't place emphasis on
keeping the variable passed.

-Mitchell


Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Stefan Brantschen
In reply to this post by Rici Lake-2

On 19 Sep 2006, at 18:58, Rici Lake wrote:

But more Lua like would be:

a = { a = 1,
      b = { b = 2,
            c = {native = 1, rock = 2}[style] or 0
          }
    }

Yeah! -- the power and straight-line, Bauhaus-style elegance of Lua. No new keywords needed... :-)

With regards
- Stefan


Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Taj Khattra
In reply to this post by mitchell-3
On 9/19/06, mitchell <[hidden email]> wrote:
> a = a or (a == nil and my_default_a)

  a = a == nil and my_default_a or a

I think that looks more readable, although it doesn't place emphasis on
keeping the variable passed.

they're not equivalent though since (nil or false) ~= (false or nil).
consider a=nil and my_default_a=false.

Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

mitchell-3
Hi,

>> > a = a or (a == nil and my_default_a)
>>
>>   a = a == nil and my_default_a or a
>>
>> I think that looks more readable, although it doesn't place emphasis on
>> keeping the variable passed.
> 
> they're not equivalent though since (nil or false) ~= (false or nil).
> consider a=nil and my_default_a=false.

Yes, you are correct. My mistake. Very subtle...

-Mitchell;


Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Inmate2993
In reply to this post by Grellier, Thierry
Actually, Lua can kind of do this already, through Associative Array and Functions.

const["native"] = 1
const["rock"] = 2

a = { a = 1,
      b = { b = 2,
            c = function() { return const[style] }
          }
     }

style = "native"
x = a.b.c()

William C. Bubel

Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Philippe Lhoste
In reply to this post by Grellier, Thierry
Grellier, Thierry a écrit :
So, I would prefer to be able to write something like (VHDL flavor, and
yet another even more disturbing keyword :-) knowing that if elseif...
could be reused)
style = "native"
a = { a = 1,
      b = { b = 2,
            c = 1 when style == "native",
                2 when style == "rock",
                0 else
          }
     }
rather than
[...]
style = "native"
a = { a = 1,
      b = { b = 2, c = f(style)
          }
     }
function f(style)
...
end

which disseminates the code, and well looks like coding.

In an application I made with Lua, I used functions in tables in a similar way, except that functions were evaluated each time.

a =
{
  a = 1,
  b =
  {
    b = 2,
    c = function (style)
	  if     style == "native" then return 1
	  elseif style == "rock"   then return 2
	  else                          return 0
	  end
	end
  }
}
Slightly more verbose than your proposal, but still bearable and using a familiar syntax.

It was a kind of database of city ways, the field indicated which postal area a way belong to. For some ways, it depends on the number of address in the way, eg. odd vs. even, below or above a value, and so on. When I needed the postal address, I checked the type of the field: if numerical, just use it; if it is a function, evaluate it. Worked well. No "dissemination". Really nice, showing the flexibility of Lua.

Your need is slightly different as you want to evaluate only once, at construction time. Should I have to do it, I suppose I would walk the table and evaluate the functions I find to fix the values.

I made a typo while writing this code, typing
if style = "native" then

Since assignments are not expressions, I wonder why the == syntax was chosen over the simpler and unambiguous = (beside the familiarity for C users...).

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

RE: Default value in function parameters?

Grellier, Thierry
In reply to this post by Stefan Brantschen
I like it too ! But I'm programmer. I'm not sure this is so easy to
explain to a non programmer. And when condition has more than one term
like inside a range...

The reason for me to choose lua is not the footprint (although I much
prefer light footprint, but I can't seriously claim this is so needed in
my case)
Not much the speed. Well anyway difference between other script
languages are not that meaningful for my usage, where most of the time
is spent in C++ code.

So the main motivation is the very simple syntax to define simple and
readable configurations. So this is why, I try to put the emphasis on
simplicity to write this.



-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Stefan
Brantschen
Sent: Tuesday, September 19, 2006 8:50 PM
To: Lua list
Subject: Re: Default value in function parameters?


On 19 Sep 2006, at 18:58, Rici Lake wrote:

> But more Lua like would be:
>
> a = { a = 1,
>       b = { b = 2,
>             c = {native = 1, rock = 2}[style] or 0
>           }
>     }

Yeah! -- the power and straight-line, Bauhaus-style elegance of Lua.  
No new keywords needed... :-)

With regards
- Stefan



Reply | Threaded
Open this post in threaded view
|

Re: Default value in function parameters?

Philippe Lhoste
In reply to this post by Rici Lake-2
Rici Lake a écrit :
On 19-Sep-06, at 11:45 AM, Grellier, Thierry wrote:
So, I would prefer to be able to write something like (VHDL flavor, and
yet another even more disturbing keyword :-) knowing that if elseif...
could be reused)
style = "native"
a = { a = 1,
      b = { b = 2,
            c = 1 when style == "native",
                2 when style == "rock",
                0 else
          }
     }

I actually like "when" (although i use it with "otherwise" because "else" is already in use).

And I find it clearer, I searched something after the else... Sounds corny. Of course, that's more keywords...

But more Lua like would be:

a = { a = 1,
      b = { b = 2,
            c = {native = 1, rock = 2}[style] or 0
          }
    }

Argh! WOULD BE! I tried it, and wondered by I had syntax errors...
Indeed, it seems a bit strange at first, but it is quite elegant in its simplicity (and no new keyword...).

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


12