DIP69 - Implement scope for escape proof references

via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 17 07:55:15 PST 2014


On Wednesday, 17 December 2014 at 13:35:53 UTC, Marc Schütz wrote:
> I think that enforcing alias-freeness by default (the way a 
> strict linear type system requires) doesn't fit too well into 
> the existing language. Some way of signalling this to the 
> compiler will therefore be required. It might be as simple as 
> C's `restrict` keyword (but it needs to be enforced!), or 
> better yet, an `unrestrict` keyword. Parameters would be 
> alias-free by default unless explicitly specified otherwise. 
> Local aliases (inside one function) are however ok, as long as 
> not more than one is passed to another function at the same 
> time.

Hmm, I kind of feel that D is having too many parameter 
keywords/types/qualifiers. Parameter specification is primarily 
useful where it provides information about constraints that the 
programmer need to know about when using a library.

"restricted scope ref" could be a useful default for 
non-templated functions, then use templated ownership reference 
typing or inference to go beyond it.

I think that if one improved the templating system so that it was 
capable of analysing similarities between instantiations and use 
heuristics to analyse when a new instance is profitable then you 
could make functions templated by default and propagate knowledge 
from the call site to the template-instantiation mechanism of the 
compiler. So if the reference is known to be aliasfree then the 
compiler can use heuristics to determine if it should instantiate 
a "restricted" version or use a slower general version of the 
templated function.

Similarily, if refcounters always are on offset 0 and destructors 
are virtual, then you only need a single container method 
template instance for refcouting references, even if you have 
many types using the same method. If refcounters are big enough, 
you can have a unique_ptr that just set it to a large value and 
use the same template instance for a unique_ptr and 
refcounted_ptr behind the scene when referencing objects that 
provide a refcount field.

There are also many other things that could be done to enhance 
the template system, like querying properties of the block in 
loops that iterate over a "range", so that you can instantiate 
different "optimized ranges" depending on whether the loop 
contains a "break" statement or not. Or like Jonathan Blow's 
language, where you can issue a "remove element and continue" 
like statement within a foreach-loop,  which might require a 
different "range iteration" implementation.


More information about the Digitalmars-d mailing list