www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how to propagate computed type during CTFE?

reply Timothee Cour <thelastmammoth gmail.com> writes:
in example below, how do I propagate RET (or even `typeof(a)`) to the
result value of `inferType`?

does this need a language change to allow this?


```
template inference(alias emitter) {
  auto inference(){
    auto inferType(){
      emitter!((a){
        enum RET=typeof(a).stringof; // type is known here, how to propagate?
        pragma(msg, RET);  // string
        }) ();
      return "unknown";
    }
    // how to get RET? (or even typeof(a) )
    enum temp=inferType;
    pragma(msg, temp);
  }
}

void main(){
  static void fun(alias put)(){
    put("hello");
  }
  inference!fun;
}
```

use case: allow type inference in `emit`

https://github.com/timotheecour/dtools/blob/master/dtools/util/emit.d
(see forum discussion here:
https://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmars-d puremagic.com)
Feb 22 2018
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 23/02/2018 1:54 PM, Timothee Cour wrote:
 in example below, how do I propagate RET (or even `typeof(a)`) to the
 result value of `inferType`?
 
 does this need a language change to allow this?
 
 
 ```
 template inference(alias emitter) {
    auto inference(){
      auto inferType(){
        emitter!((a){
          enum RET=typeof(a).stringof; // type is known here, how to propagate?
          pragma(msg, RET);  // string
          }) ();
        return "unknown";
      }
      // how to get RET? (or even typeof(a) )
      enum temp=inferType;
      pragma(msg, temp);
    }
 }
 
 void main(){
    static void fun(alias put)(){
      put("hello");
    }
    inference!fun;
 }
 ```
 
 use case: allow type inference in `emit`
 
 https://github.com/timotheecour/dtools/blob/master/dtools/util/emit.d
 (see forum discussion here:
 https://forum.dlang.org/post/mailman.538.1458560190.26339.digitalma
s-d puremagic.com)> 
Yeah I've tried, I can't think of any way to do this.
Feb 22 2018
prev sibling next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 23 February 2018 at 00:54:34 UTC, Timothee Cour wrote:
 in example below, how do I propagate RET (or even `typeof(a)`) 
 to the
 result value of `inferType`?

 does this need a language change to allow this?
No can do. Consider what would happen if you added put(1); inside fun - what should temp become? -- Simen
Feb 22 2018
prev sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 23 February 2018 at 00:54:34 UTC, Timothee Cour wrote:
 in example below, how do I propagate RET (or even `typeof(a)`) 
 to the
 result value of `inferType`?

 does this need a language change to allow this?


 ```
 template inference(alias emitter) {
   auto inference(){
     auto inferType(){
       emitter!((a){
         enum RET=typeof(a).stringof; // type is known here, how 
 to propagate?
         pragma(msg, RET);  // string
         }) ();
       return "unknown";
     }
     // how to get RET? (or even typeof(a) )
     enum temp=inferType;
     pragma(msg, temp);
   }
 }

 void main(){
   static void fun(alias put)(){
     put("hello");
   }
   inference!fun;
 }
 ```

 use case: allow type inference in `emit`

 https://github.com/timotheecour/dtools/blob/master/dtools/util/emit.d
 (see forum discussion here:
 https://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmars-d puremagic.com)
Yes there is a language change required for this. Currently types are untouchable entities. I plan to introduce a mechanism which'll give you first-class types inside a bounded semantic construct.
Feb 23 2018