« Return to Thread: constructors with mix of params and Map

Re: constructors with mix of params and Map

by Aaron Digulla :: Rate this Message:

Reply to Author | View in Thread

Quoting Alexandru Popescu ☀ <the.mindstorm.mailinglist@...>:

> On 10/9/07, Guillaume Laforge <glaforge@...> wrote:
>> Is there some reason (use case?) which triggered this idea?
>> Frankly, I'm not too fond of mixing maps and parameters too much.
>>
>
> Python supports the following format:
>
> positional arguments
> *args:
> **args: named parameters
>
> This seems to work oke there. But I don't think that mixing them makes sense.

You can't mix them in Python, either. The order must be:

- Normal parameters
- Keyword arguments (or arguments with default value).
- Argument lists
- Maps

In the code, this looks like so:

def f(arg, argWithDefault='default value', *args, **kw):
     print 'arg=%s, argWithDefault=%s, *args=%s, **kw=%s' % (str(arg),  
str(argWithDefault), str(args), str(kw))

Resolution:

- The first parameter is stored in "arg"
- If there is a second parameter, then the value is stored in  
"argWithDefault". If there isn't, "argWithDefault" has the value  
'default value'. Note: The value is evaluated once. If you say, for  
example, x=[] to define a list, all invocations of f() will be passed  
the same list instance.
- All other, unnamed arguments end up in *args which is a list
- All named arguments end up in the map **kw.

Examples:

f() -> Error, because arg is missing
f(1) -> arg=1, argWithDefault='default value', *args=[], **kw={}
f(1,2) -> arg=1, argWithDefault=2, *args=[], **kw={}
f(1,2,3) -> arg=1, argWithDefault=2, *args=[3], **kw={}
f(1,2,3,4) -> arg=1, argWithDefault=2, *args=[3,4], **kw={}
f(1,2,3,4, argWithDefault=5) -> Error: Value of argWithDefault is  
specified twice.
f(1,argWithDefault=5,2,3,4) -> Error: non-keyword arg after keyword arg
f(1,2,3,4,a=5) -> arg=1, argWithDefault=2, *args=(3, 4), **kw={'a': 5}

> ./alex
> --
> .w( the_mindstorm )p.
>
>> On 10/9/07, Alex Tkachman <alex.tkachman@...> wrote:
>> > Do we want to code like this where constructor params mixed with named
>> > properties?
>> >
>> > class Book {
>> >   Book (String author, String coauthor) {
>> >       this.author = author
>> >       this.coauthor = coauthor
>> >   }
>> >
>> >   String title, author, coauthor
>> > }
>> >
>> > class SuperBook extends Book {
>> >   SuperBook (Map map, String author, String coauthor) {
>> >       super (author, coauthor)
>> >       title = "SuperBook"
>> >   }
>> > }
>> >
>> > class NewInstanceTest extends GroovyTestCase{
>> >     void testNewInstance () {
>> >        def book = Book.newInstance("Alex", title: "All about it",  
>>  "Graeme" )
>> >        assertEquals book.title, "All about it"
>> >        assertEquals book.author, "Alex"
>> >        assertEquals book.coauthor, "Graeme"
>> >     }
>> >
>> >     void testConstructor () {
>> >        def book = new Book("Alex", title: "All about it", "Graeme" )
>> >        assertEquals book.title, "All about it"
>> >        assertEquals book.author, "Alex"
>> >        assertEquals book.coauthor, "Graeme"
>> >     }
>> >
>> >     void testSuper () {
>> >        def book = new SuperBook("Alex", title: "All about it", "Graeme" )
>> >        assertEquals book.title, "SuperBook"
>> >        assertEquals book.author, "Alex"
>> >        assertEquals book.coauthor, "Graeme"
>> >     }
>> > }
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list please visit:
>> >
>> >     http://xircles.codehaus.org/manage_email
>> >
>> >
>>
>>
>> --
>> Guillaume Laforge
>> Groovy Project Manager
>> http://glaforge.free.fr/blog/groovy
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://www.pdark.de/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: constructors with mix of params and Map