A few Phobos projects: @safe, dip1000, public examples, properly documented functions, ...

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 30 15:19:22 UTC 2018


On Tue, Jan 30, 2018 at 08:08:14AM +0000, Dukc via Digitalmars-d wrote:
> On Monday, 29 January 2018 at 17:51:40 UTC, Seb wrote:
> 
> > - All high-level code should be usable in @safe
> 
> This is not currently possible with functions that take a delegate
> parameter, including opApply. (without sacrificing genericity)
> 
> A DIP could be made so that the function infers it's attributes from a
> delegate argument, but that's so much added complexity that it's
> debatable whether it would be worth it.
[...]

You could templatize opApply so that the compiler applies automatic
attribute inference:

	struct S {
		int opApply(Dg)(scope Dg dg)
		{
			foreach (i; 0 .. 5)
			{
				auto ret = dg(i);
				if (ret) return ret;
			}
			return 0;
		}
	}
	void systemFunc() @system
	{
		void* p;
		S s;
		foreach (int i; s) {
			p++;
		}
	}
	void safeFunc() @safe
	{
		int x;
		S s;
		foreach (int i; s) {
			x++;
		}
	}

Unfortunately, this also means that you can no longer have the compiler
deduce the type of the loop iteration variable (the 'int' in the foreach
loops is now required, since the compiler can't infer it from the
generic template parameter Dg).  Not to mention the template bloat from
each combination of delegate attributes.

What we need here is the analogue of 'inout' for function attributes.
But given the track record of inout, I'm not sure we want to have
another such hack in the language...


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce


More information about the Digitalmars-d mailing list