June 26, 2012
On Tue, 26 Jun 2012 14:08:14 -0400, Benjamin Thaut <code@benjamin-thaut.de> wrote:


> Thanks this works, but it seems to be a very ugly hack just to work around the type system. Also I have templated allocator functions I can not use this trick on. This is going to be a lot of work to get done properly, so I just ignore pure for now I think.

Um... yeah :)  If you are working around the type system, it *should* be ugly!

Note that you should be *extremely* careful of saying something is pure when it is not typechecked by the compiler.  If I were designing such an allocator system, I'd separate out pure and impure allocators into two separate hierarchies, and let the person implementing the actual underlying allocator jump the fence between pure and impure.

For example, in the code I gave, really only C malloc should be forced-pure, everything else should be typechecked as pure by the compiler.  But you were asking for a specific solution.

-Steve
June 26, 2012
On Tuesday, 26 June 2012 at 18:35:31 UTC, Jonathan M Davis wrote:
> You could try std.traits.SetFunctionAttributes (it was recently added and is
> not in 2.59 IIRC). David Nadlinger created it specifically for being able to
> create easily add function attributes such as pure to a function. I haven't
> messed around with it yet, but it looks very easy to use.

I created it specifically for a related use case, but on its own, SetFunctionAttributes is strictly for manipulating function/delegate _types_ in terms of linkage. This is helpful because the D grammar for function types is quite clumsy.

For example, the following is not valid D code, and can't trivially be corrected (in the first case, creating a separate alias for the function type would work, but that's clumsy especially in generic and/or generated code):

---
void foo(extern(C) void function() nothrow cb);
auto assumePure(T)(T t) { return cast(pure T)(t); }
---

As mentioned in https://github.com/D-Programming-Language/phobos/pull/555, it would be easy to add »high-level primitives« on top of SetFunctionAttributes; assumePure and ExternC templates are included as examples its the documentation. Specifically for declaring »trusted pure« functions, making »alias assumePure!funcImpl func;« work would not be hard either.

I just was not sure which of those tools are needed frequently enough to warrant inclusion in Phobos, if any.

David
June 26, 2012
On Tuesday, 26 June 2012 at 19:48:26 UTC, Steven Schveighoffer wrote:
> Note that you should be *extremely* careful of saying something is pure when it is not typechecked by the compiler.

I can only second that – especially because depending on what the signature of the function is, the compiler might optimize out calls to them.

David
1 2
Next ›   Last »