Templates in classes => what is wrong?
Xan
xancorreu at gmail.com
Tue Apr 17 11:13:52 PDT 2012
On Tuesday, 17 April 2012 at 18:00:55 UTC, Xan wrote:
> On Tuesday, 17 April 2012 at 15:59:25 UTC, Ali Çehreli wrote:
>> On 04/17/2012 08:42 AM, Xan wrote:
>>
>> > How to get the "code" of a function or delegate
>> >
>> > |___string toString() {
>> > |___|___return format("%s (versió %s): Domini -> Recorregut,
>> %s(x) =
>> > %s", nom, versio, nom, &funcio);
>> >
>> > |___}
>> >
>> > does not produce the desired result and &funcio without
>> ampersand
>> > produces me an error.
>> >
>> > So, my doubts are:
>> > given a function:
>> >
>> > - how can I get the domain
>> > - how can I get the range
>>
>> I did not understand those. :(
>
> Domain is the set of values that we pass to the function and
> Range is the set of Values which are returned by function.
>
> V delegate (U) f;
> f has Domain U and Range V
>
> I want to "print" the type of "U" and "V".
>
> Something like:
>
> class Algorisme(U,V) {
> string nom;
> uint versio;
> alias V delegate (U) Funcio;
> Funcio funcio;
>
>
> this(string nom, uint versio, Funcio funcio) {
> this.nom = nom;
> this.versio = versio;
> this.funcio = funcio;
> }
>
> string toString() {
> return format("%s (versió %s): %s -> %s, %s(x) = %s", nom,
> versio, V, U, nom, &funcio);
>
> }
> }
>
>
> but I receive
>
> algorisme.d:24: Error: type int has no value
> algorisme.d:24: Error: type int has no value
>
> when I call with Algorisme!(int, int) intead of receiving "int"
> and "int" as Domain and Range
>
>>
>> > - how can I get the code of the function?
>> >
>> > See https://gist.github.com/2394274
>> >
>>
>> I don't think D has any help there. You can keep the function
>> as a string yourself and convert to actual code at compile
>> time with a string mixin. For that to happen, the function
>> text may be an additional template parameter:
>>
>> import std.conv, std.stdio, std.stream, std.string;
>> import std.socket, std.socketstream;
>> import std.datetime;
>>
>> class Algorisme(U,V,string funcioText) {
>> string nom;
>> uint versio;
>> alias V delegate (U) Funcio;
>> Funcio funcio;
>>
>>
>> this(string nom, uint versio) {
>> this.nom = nom;
>> this.versio = versio;
>> this.funcio = mixin(funcioText);
>> }
>>
>> string toString() {
>> return format("%s (versió %s): Domini ->
>> Recorregut, %s(x) = %s",
>> nom, versio, nom, funcioText);
>> }
>> }
>>
>>
>> alias Algorisme!(int, int, "(int a) { return 2 * a; }")
>> AlgorismeEnters;
>>
>> void main(string [] args)
>> {
>>
>> auto alg = new AlgorismeEnters("Doblar", 1);
>> writeln(alg);
>>
>> }
>>
>> Ali
>
> It's ugly code. I think I could call some procedure like f.code
> (f is a function) to obtain the "code" how f is defined. But
> stricly in mathematical thinking it's not adequate, because
> more codes could result in the same (mathematical function): x
> + x is the double of x; and 2*x is too.
>
> Perhaps if I change Algorisme and add the string field "code"
> and if there is any procedure to copy the 3rd argument in the
> constructor and pass as string in the 4th argument (in the
> constructor)
>
> class Algorisme(U,V) {
> |___string nom;
> |___uint versio;
> |___alias V delegate (U) Funcio;
> |___Funcio funcio;
> |___string code;
>
>
> |___this(string nom, uint versio, Funcio funcio) {
> |___|___this.nom = nom;
> |___|___this.versio = versio;
> |___|___this.funcio = funcio;
> this.code = funcio.WHAT PROCEDURE?;
> |___}
>
>
> Regards,
> Xan.
The idea is behind this https://gist.github.com/2407923
But I receive:
$ gdmd-4.6 algorisme_code.d
algorisme_code.d:22: Error: variable codi cannot be read at
compile time
algorisme_code.d:22: Error: argument to mixin must be a string,
not (codi)
What can I do?
Thanks,
Xan.
More information about the Digitalmars-d-learn
mailing list