newbie question: Can D do this?

Timon Gehr timon.gehr at gmx.ch
Tue Dec 20 07:23:02 PST 2011


On 12/20/2011 03:18 PM, clk wrote:
> Thank you for your quick replies. I'm impressed by the helpfulness and
> dedication of the D community!
> Here's another one. Is there a way to pass arguments to functions by
> keyword as in the calls to f and g below?
>
> void f(int a = 0, int b = 1) {}
> void g(int a) {}
>
> void main() {
> f(b = 1, a = 0); // compile error
> g(a = 0); // also compile error
> }
>
>

No, there are no named arguments in D. Having them would sometimes be 
useful, but the drawback is that the parameter names become part of the 
public interface.

>
>
>
> On 12/19/2011 03:00 PM, digitalmars-d-learn-request at puremagic.com wrote:
>> Send Digitalmars-d-learn mailing list submissions to
>> 	digitalmars-d-learn at puremagic.com
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> 	http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn
>>
>> or, via email, send a message with subject or body 'help' to
>> 	digitalmars-d-learn-request at puremagic.com
>>
>> You can reach the person managing the list at
>> 	digitalmars-d-learn-owner at puremagic.com
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Digitalmars-d-learn digest..."
>>
>>
>> Today's Topics:
>>
>>     1. Re: newbie question: Can D do this? (Ali ?ehreli)
>>     2. Re: newbie question: Can D do this? (Kai Meyer)
>>     3. Re: newbie question: Can D do this? (Simen Kj?r?s)
>>     4. Re: newbie question: Can D do this? (Ali ?ehreli)
>>     5. Re: newbie question: Can D do this? (Jonathan M Davis)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Mon, 19 Dec 2011 10:41:29 -0800
>> From: Ali ?ehreli<acehreli at yahoo.com>
>> To:digitalmars-d-learn at puremagic.com
>> Subject: Re: newbie question: Can D do this?
>> Message-ID:<jco0gq$1ilt$1 at digitalmars.com>
>> Content-Type: text/plain; charset=UTF-8; format=flowed
>>
>> On 12/19/2011 08:17 AM, clk wrote:
>>
>>   >  I'm a little bit intimidated by the fact that the topics in the d-learn
>>   >  list look rather advanced to a newbie like me.
>>
>> We need more newbie topics here! :)
>>
>>   >  1) Does D support something like the javascript 1.8 destructuring
>>   >  assigment (multiple assigment in python):
>>   >
>>   >  [a, b] = [b, a];
>>
>> No multiple assignment like that. But useful approarches exist for most
>> needs, like the swap that simendsjo has shown.
>>
>>   >  2) D doesn't seem to support the list comprehension syntax available in
>>   >  python and javascript. Is this correct?
>>   >
>>   >  [f(x) for x in list if condition]
>>
>> List comprehension is not part of the language.
>>
>> import std.algorithm;
>>
>> void f(int x)
>> {}
>>
>> bool condition(int x)
>> {
>>       return true;
>> }
>>
>> void main()
>> {
>>       auto list = [ 0, 1, 2 ];
>>       map!f(filter!condition(list));
>> }
>>
>> You can define f and condition within the body of main().
>>
>> It is possible to use function literals as well:
>>
>> import std.algorithm;
>>
>> void main()
>> {
>>       auto list = [ 0, 1, 2 ];
>>       map!((x){
>>               /* ... this is f(x) ...*/
>>           })(filter!((x) {
>>                       return true; /* ... condition ... */
>>                   })(list));
>> }
>>
>>   >  3) D's slice operator apparently doesn't allow the use of a stride other
>>   >  than unity as is allowed with fortran and matlab. Is there a way to
>>   >  implement this feature so that
>>   >
>>   >  [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the
>>   >  non unit stride. Or is the find function from std.algorithm the only
>>   >  option to achieve the same behavior.
>>
>> std.range.stride does that:
>>
>> import std.range;
>> // ...
>> stride([1, 2, 3, 4, 5], 2)
>>
>>   >
>>   >  I find the 3 features above extremely convenient in every day coding.
>>   >  Thanks,
>>   >  -clk
>>   >
>>   >
>>
>> Ali
>>
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Mon, 19 Dec 2011 11:50:33 -0700
>> From: Kai Meyer<kai at unixlords.com>
>> To:digitalmars-d-learn at puremagic.com
>> Subject: Re: newbie question: Can D do this?
>> Message-ID:<jco11q$1lle$1 at digitalmars.com>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>> On 12/19/2011 09:17 AM, clk wrote:
>>> Hello,
>>> I'm new to this mailing list. I'm trying to learn D to eventually use it
>>> in production code.
>>> I'm a little bit intimidated by the fact that the topics in the d-learn
>>> list look rather advanced to a newbie like me.
>>> I have 3 fairly simple questions:
>>>
>>> 1) Does D support something like the javascript 1.8 destructuring
>>> assigment (multiple assigment in python):
>>>
>>> [a, b] = [b, a];
>> I would love multiple assignment like this, but it's tricky. But your
>> usage isn't really multiple assignment as much as it is a swap. What I'd
>> love is something like this:
>>
>> [a, b, c] = [get_a(), get_b(), get_c()];
>>
>> Or
>>
>> [a, b, c] = [to!(int)(argv[1]), some_other_value, argv[4]);
>>
>>
>>
>>> 2) D doesn't seem to support the list comprehension syntax available in
>>> python and javascript. Is this correct?
>>>
>>> [f(x) for x in list if condition]
>> No, D's syntax is very C-ish. I don't expect syntax like this to ever
>> show up (though what you are doing is possible with things like
>> std.algorithm)
>>
>>> 3) D's slice operator apparently doesn't allow the use of a stride other
>>> than unity as is allowed with fortran and matlab. Is there a way to
>>> implement this feature so that
>>>
>>> [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the
>>> non unit stride. Or is the find function from std.algorithm the only
>>> option to achieve the same behavior.
>> Ya, std.range, like Ali said.
>>
>>> I find the 3 features above extremely convenient in every day coding.
>>> Thanks,
>>> -clk
>>>
>>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Mon, 19 Dec 2011 20:01:06 +0100
>> From: Simen Kj?r?s<simen.kjaras at gmail.com>
>> To:digitalmars-d-learn at puremagic.com
>> Subject: Re: newbie question: Can D do this?
>> Message-ID:<op.v6q234mt0gpyof at biotronic.lan>
>> Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
>>
>> On Mon, 19 Dec 2011 17:17:43 +0100, clk<clk at clksoft.com>  wrote:
>>
>>> Hello,
>>> I'm new to this mailing list.  I'm trying to learn D  to eventually use
>>> it in production code.
>>> I'm a little bit intimidated by the fact that the topics in the d-learn
>>> list look rather advanced to a newbie like me.
>>> I have 3 fairly simple questions:
>>>
>>> 1) Does D support something like the javascript 1.8 destructuring
>>> assigment (multiple assigment in python):
>>>
>>> [a, b] = [b, a];
>>
>> This, or something quite like it, was covered on Saturday in the thread
>> "Alias/Ref Tuples ?". This works (but is hardly as elegant as Python's
>> syntax:
>>
>> import std.typetuple : TypeTuple;
>> import std.typecons : tuple;
>>
>> TypeTuple!(a, b) = tuple(b,a);
>>
>>
>> ------------------------------
>>
>> Message: 4
>> Date: Mon, 19 Dec 2011 11:07:49 -0800
>> From: Ali ?ehreli<acehreli at yahoo.com>
>> To:digitalmars-d-learn at puremagic.com
>> Subject: Re: newbie question: Can D do this?
>> Message-ID:<jco225$1r5m$1 at digitalmars.com>
>> Content-Type: text/plain; charset=UTF-8; format=flowed
>>
>> On 12/19/2011 10:39 AM, Jonathan M Davis wrote:
>>   >  it's a range (see
>>   >  http://www.informit.com/articles/printerfriendly.aspx?p=1407357  for a
>> general
>>   >  explanation of the concept of ranges)
>>
>> That's a great article.[1] I hope that this chapter is more
>> beginner-friendly:
>>
>>     http://ddili.org/ders/d.en/ranges.html
>>
>> Ali
>>
>> [1] Andrei's article has a Turkish translation as well:
>>
>>     http://ddili.org/makale/eleman_erisimi_uzerine.html
>>
>>
>>
>> ------------------------------
>>
>> Message: 5
>> Date: Mon, 19 Dec 2011 14:30:39 -0500
>> From: "Jonathan M Davis"<jmdavisProg at gmx.com>
>> To: "digitalmars.D.learn"<digitalmars-d-learn at puremagic.com>
>> Subject: Re: newbie question: Can D do this?
>> Message-ID:<20111219193039.5420 at gmx.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> On Monday, December 19, 2011 11:07:49 Ali ?ehreli wrote:
>>> That's a great article.[1] I hope that this chapter is more
>>> beginner-friendly:
>>>
>>> http://ddili.org/ders/d.en/ranges.html
>> Cool. One of the things that we're missing on the website is a solid article
>> on ranges (I started such an article a while back and really should go back
>> and finish it), but having something like this to link to should be quite
>> useful. I'll have to give it a read. Thanks!
>>
>> - Jonathan M Davis
>>
>>
>> ------------------------------
>>
>> _______________________________________________
>> Digitalmars-d-learn mailing list
>> Digitalmars-d-learn at puremagic.com
>> http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn
>>
>>
>> End of Digitalmars-d-learn Digest, Vol 71, Issue 29
>> ***************************************************
>>
>



More information about the Digitalmars-d-learn mailing list