www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to compose delegate type

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
how do I construct F!(T) to yield

void delegate() immutable

when T is void delegate()

?

[its been a long day]
Dec 05 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 6 December 2013 at 02:42:44 UTC, Ellery Newcomer wrote:
 how do I construct F!(T) to yield

 void delegate() immutable

 when T is void delegate()

 ?

 [its been a long day]
I don't think I understand what you mean: void main() { F!(void delegate()); } immutable(T) F(T)() { return () {}; } Initially I thought you meant T was a void delegate but passing delegates as compile time parameters isn't really an option.
Dec 05 2013
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 12/05/2013 09:33 PM, Jesse Phillips wrote:
 I don't think I understand what you mean:
this code illustrates it: class Z { string a() immutable { return " 1"; } string b() { return "2"; } } template F(t) { alias immutable(t) F; } alias typeof(&Z.init.a) Texpected; alias typeof(&Z.init.a) T; static assert(is(F!(T) == Texpected)); void main() {} above F doesn't work; immutable(void delegate()) and void delegate() immutable are different types (I think the latter means 'this' is immutable). If t were a function pointer, you would apply the immutable to the pointer target like so: alias immutable(pointerTarget!t)* F
Dec 06 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 7 December 2013 at 01:18:06 UTC, Ellery Newcomer 
wrote:
 On 12/05/2013 09:33 PM, Jesse Phillips wrote:
 I don't think I understand what you mean:
this code illustrates it: class Z { string a() immutable { return " 1"; } string b() { return "2"; } } template F(t) { alias immutable(t) F; } alias typeof(&Z.init.a) Texpected; alias typeof(&Z.init.a) T; static assert(is(F!(T) == Texpected)); void main() {} above F doesn't work; immutable(void delegate()) and void delegate() immutable are different types (I think the latter means 'this' is immutable). If t were a function pointer, you would apply the immutable to the pointer target like so: alias immutable(pointerTarget!t)* F
This declaration doesn't make sense to me: string a() immutable { return " 1"; } 'a' is already immutable (unless you consider the vtable, at which point: final string a(); would be immutable). The compiler spits out: static assert (is(immutable(string delegate()) == string delegate() immutable)) is false Which is strange because I don't see how immutability on 'a' means anything. And because of this I think your assertion is wrong: static assert(is(F!(T) == Texpected)); Is basically asking static assert(is(immutable(int) == int)); Which is not true, but this is (I've made modifications to your code): class Z { string a() { return " 1"; } string b() { return "2"; } } template F(t) { alias immutable(t) F; } alias typeof(&Z.init.a) Texpected; alias typeof(&Z.init.b) T; static assert(is(immutable(int) : int)); // Implicitly converts to static assert(is(int : immutable(int))); // Implicitly converts to static assert(is(F!(T) : Texpected)); // Implicitly converts to static assert(is(Texpected : F!(T) )); // Implicitly converts to void main() {}
Dec 07 2013
parent reply "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
On Saturday, 7 December 2013 at 19:36:50 UTC, Jesse Phillips 
wrote:
 This declaration doesn't make sense to me:

     string a() immutable {
         return " 1";
     }
http://dlang.org/class.html#member-functions
Dec 07 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 7 December 2013 at 23:02:18 UTC, Ellery Newcomer 
wrote:
 On Saturday, 7 December 2013 at 19:36:50 UTC, Jesse Phillips 
 wrote:
 This declaration doesn't make sense to me:

    string a() immutable {
        return " 1";
    }
http://dlang.org/class.html#member-functions
That isn't really the type of a delegate though. Outside of a class that usage has no meaning or value. Even so, I don't actually understand what value it brings over declaring the function const (maybe only callable by an immutable instance?). What is wrong with the current template which returns an immutable delegate type? It still store you're immutable member function. class Z { string a() immutable { return " 1"; } } template F(t) { alias immutable(t) F; } alias typeof(&Z.init.a) Texpected; alias typeof(&Z.init.a) T; static assert(is(F!(T) : Texpected)); static assert(is(Texpected : F!(T) )); void main() {}
Dec 07 2013
parent "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
On Sunday, 8 December 2013 at 00:43:51 UTC, Jesse Phillips wrote:
 What is wrong with the current template which returns an 
 immutable delegate type? It still store you're immutable member 
 function.
It composes the wrong type. It composes a type that has different constness than the target type, which will likely cause problems due to transitive const. Anyways, I'm trying to find a workaround to https://d.puremagic.com/issues/show_bug.cgi?id=11694
Dec 08 2013