newbie question: Can D do this?

mta`chrono chrono at mta-international.net
Wed Dec 21 00:36:18 PST 2011


In PHP frameworks often use $option arrays which are some kind of key
value pairs. they are merged with the default valuzes inside the function.

pro:
 - you can pass an argument by name
 - you only need to pass those who needed.

cons:
 - hash arrays

it should be possible to create a similar method in d. but I really
don't like this solution in d. it feels bad and looks ugly.


<?php

$model->query(array(
   'firstname' => 'John',
   'country' => 'France',
   'order' => 'asc'
));

class Model
{
    function query($options = array())
    {
        // merge with defaults
        $options = array_merge(array(
            'deep' => true,
            'order' => 'desc',
            'type' => 'sql',
            'backend' => 'mysql'
            ...
        ), $options);
    }

}

?>


More information about the Digitalmars-d-learn mailing list