Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 20, 2006 Continuations in D? | ||||
---|---|---|---|---|
| ||||
Does D support continuations? I anticipate that continuations will become increasingly important as people seek ways to retain the simplicity of a threaded architecture while avoiding the overhead of actual threads. If D could support this language feature, I think it would make D very appealing for a whole variety of applications. Ian. |
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ian Clarke | Ian Clarke wrote:
> Does D support continuations? I anticipate that continuations will become
> increasingly important as
> people seek ways to retain the simplicity of a threaded architecture while
> avoiding the overhead of actual
> threads. If D could support this language feature, I think it would make D very
> appealing for a whole
> variety of applications.
>
> Ian.
>
>
I think the response you are going to hear is "look for it in D2.0". At
this point the concentration is on creating a D1.0.
Nick.
|
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ian Clarke | Ian Clarke wrote:
> Does D support continuations? I anticipate that continuations will become
> increasingly important as
> people seek ways to retain the simplicity of a threaded architecture while
> avoiding the overhead of actual
> threads. If D could support this language feature, I think it would make D very
> appealing for a whole
> variety of applications.
>
> Ian.
>
>
Sorry, getting late. I should clarify that a bit. The aim is to release D1.0 sometime in the foreseeable future, so most feature suggestions are being targeted at D2.0.
|
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ian Clarke | Please forgive my ignorance; what is a continuation or where can I read about it?
Thanks,
-[Unknown]
> Does D support continuations? I anticipate that continuations will become
> increasingly important as people seek ways to retain the simplicity of a threaded architecture while
> avoiding the overhead of actual threads. If D could support this language feature, I think it would make D very
> appealing for a whole variety of applications.
>
> Ian.
>
>
|
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Unknown W. Brackets wrote:
> Please forgive my ignorance; what is a continuation or where can I read about it?
>
> Thanks,
> -[Unknown]
>
>
>> Does D support continuations? I anticipate that continuations will become
>> increasingly important as people seek ways to retain the simplicity of a threaded architecture while
>> avoiding the overhead of actual threads. If D could support this language feature, I think it would make D very
>> appealing for a whole variety of applications.
>>
>> Ian.
>>
>>
To my knowledge of them, they are an alternate form of writing code.
Consider a function which returns an integer:
int foo() { return 1; }
Now suppose you wanted to return multiple integers:
int foo() { return 2; return 3; return 4; } // doesnt work
to do this requires an inversion of control, the called function will call you back with the values:
void foo( void delegate( int ) yield )
{
yield( 2 );
yield( 3 );
yield( 4 );
}
This is commonly known as continuation passing style or CPS. In this style the function passed to a function represents the 'rest of the program'. In this way the function can complete the program more than one, hence a continuation of the current program.
Converting the code to this form allows you to use pieces of code in various ways. Consider try/catch.
try
{
calc1();
calc2();
}
catch( ... ) { ..various fail code... }
Using this you can see the conversion of this to CPS form:
void failure_code() { ..various fail code... }
void try_function( void delegate() failure )
{
if ( calc1 == FAIL ) failure();
else if ( calc2 == FAIL ) failure();
}
try_function( &failure_code )
Inner functions in D make this style of code very workable.
PS. Note that with an appropriate return value from failure() you can signify that the failed calculation be retried!
This is a simplification but if you google there are several good papers on them.
-DavidM
|
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | David Medlock wrote:
<snip>
> -DavidM
As a quick follow up, the D foreach syntax is a CPS construct. The code within the braces below the foreach *is* a continuation. That is why the opApply accepts a delegate.
-DavidM
|
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | I thought it was possible to simulate continuations with a smart use of C's setjump and longjump though I never tried to implement any. Has anyone had a similar idea ? |
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Remy Moueza | Remy Moueza wrote: > I thought it was possible to simulate continuations with a smart use of C's > setjump and longjump though I never tried to implement any. > Has anyone had a similar idea ? > > http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html This method seems much less flexible than true continuations, which depict differing branches of the program which can be executed multiple times. -DavidM |
February 20, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | David Medlock wrote:
> Remy Moueza wrote:
>
>> I thought it was possible to simulate continuations with a smart use of C's
>> setjump and longjump though I never tried to implement any.
>> Has anyone had a similar idea ?
>>
>>
> http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
>
> This method seems much less flexible than true continuations, which depict differing branches of the program which can be executed multiple times.
>
> -DavidM
Oops jumped ahead of myself. Disreguard, no setjmp in that link.
|
February 22, 2006 Re: Continuations in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | In article <dtclr2$1vgq$1@digitaldaemon.com>, Unknown W. Brackets says... >Please forgive my ignorance; what is a continuation or where can I read about it? Continuations can be quite confusing, depending on how they are explained. The "correct" explanation is quite difficult to understand. A simpler way is to think of a continuation as a frozen thread, although typically they use far fewer resources than an actual frozen thread. It allows you to program in a very "thread heavy" way, but without the overhead that would typically imply. Continuations are useful in situations where you might otherwise use a lot of threads, but where those threads spend most of their time waiting for things to happen. Many networked applications fall into this category, indeed, what initially got me interested in continuations was my work on P2P architectures. The alternative to continuations is to use an event-based, or callback-based architecture, but this can quickly degenerate into spaghetti code (been there, done that). Continuations are present in some of the more academic languages, including Scheme, and some dialects of ML. Ruby also has them, as does Stackless Python. They can also be simulated in C using some rather scary low-level hacking (someone else alluded to this in this thread), but that isn't for the faint of heart, nor is it cross-platform. There are also some kludges to achieve continuations in Java, but they aren't very pretty or efficient. Support for continuations in D would make it a prime candidate for anyone interested in implementing network-based stuff in an efficient language, especially P2P stuff. Ian. |
Copyright © 1999-2021 by the D Language Foundation