View mode: basic / threaded / horizontal-split · Log in · Help
September 03, 2011
Re: Conversion of delegate with default arguments to delegate with less arguments?
On 09/03/2011 11:34 AM, Jacob Carlborg wrote:
> On 2011-09-02 21:11, Andrej Mitrovic wrote:
>> Code:
>>
>> void main()
>> {
>> auto foo = (int x = 10){ /* */ };
>> void delegate() bar = foo;
>> }
>>
>> Since foo takes an argument that already has a default it can be used
>> as a simple `void delegate()`, so maybe it makes sense for `foo` to be
>> able to implicitly convert to such a type.
>>
>> Unfortunately doing a cast doesn't work properly:
>>
>> import std.stdio;
>>
>> void main()
>> {
>> auto foo = (int x = 10){ writeln(x); };
>> void delegate() bar;
>> bar = cast(typeof(bar))foo;
>>
>> bar(); // prints garbage
>> }
>>
>> So maybe this type of conversion is impossible in the first place due
>> to how arguments are passed? I don't know all the technical tidbits,
>> but from a user's point of view an implicit conversion kind of makes
>> sense (if it's possible).
>
> I think it would be usable. I also think that a delegate/function that
> returns a value could be implicitly converted to a delegate/function of
> the same signature but returns void instead. I would be the same as
> calling the delegate that returns a value and not assign the returned
> value to a variable.
>

Note that this would also require a thunk that explicitly discards the 
result value in non-trivial cases.
September 03, 2011
Re: Conversion of delegate with default arguments to delegate with less arguments?
On Sat, 03 Sep 2011 23:03:18 +0200, Timon Gehr <timon.gehr@gmx.ch> wrote:

> On 09/03/2011 03:12 PM, Martin Nowak wrote:
>> On Sat, 03 Sep 2011 11:50:25 +0200, Christophe
>> <travert@phare.normalesup.org> wrote:
>>
>>>> void main()
>>>> {
>>>> auto foo = (int x = 10){ /* */ }
>>>> void delegate() bar = { return foo(); }
>>>> }
>>>>
>>>> the compiler could in theory just automatically insert a thunk like
>>>> this.
>>>
>>> That sounds reasonable.
>>
>> This look like a library solution would be best.
>> I suppose you want to store a homogeneous list of delegates.
>> Actually std.bind should do exactly this, but the module is broken and
>> needs a rewrite.
>>
>> You should also be able to use std.functional's curry but it turns out
>> it's implementation
>> can't handle anything but two parameter functions.
>> I'll make a pull request for this one though. Then you should be able to
>> do 'alias curry!(foo, 10) bar'
>>
>
> http://d.puremagic.com/issues/show_bug.cgi?id=4391
>
> Make sure to call the new implementation 'partial' or similar.
>
>

Sounds reasonable, but someone should write an implementation of 'curry'  
then.
Besides does anybody know how to get 'deprecated alias old new' to  
actually bark?

martin
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home