Re: "is not an lvalue" when passing template function to spawn function
On Thursday, 9 November 2023 at 10:14:46 UTC, Bienlein wrote: On Thursday, 9 November 2023 at 09:40:47 UTC, Bienlein wrote: On Wednesday, 8 November 2023 at 16:47:02 UTC, Paul Backus wrote: On Wednesday, 8 November 2023 at 16:30:49 UTC, Bienlein wrote: ... The actual problem here is that you can't take the address of a template without instantiating it first. To make your example work, replace `` with `!int`, like this: spawn(!int, biz); All right. It seems I cannot pass on an object. So I store the object in a global and access it from the callback function passed to spawn.
Re: "is not an lvalue" when passing template function to spawn function
On Thursday, 9 November 2023 at 09:40:47 UTC, Bienlein wrote: On Wednesday, 8 November 2023 at 16:47:02 UTC, Paul Backus wrote: On Wednesday, 8 November 2023 at 16:30:49 UTC, Bienlein wrote: ... The actual problem here is that you can't take the address of a template without instantiating it first. To make your example work, replace `` with `!int`, like this: spawn(!int, biz); Thanks, Paul. This helped a step further. When applying your change it looks like this: Biz!int biz = new Biz!int(123); spawn(!int, biz); Then I get this error: 'Error: static assert: "Aliases to mutable thread-local data not allowed."' For this error I found this in the Internet: https://stackoverflow.com/questions/14395018/aliases-to-mutable-thread-local-data-not-allowed But this change, did not help: spawn(!int, cast(shared) biz); Then I moved "Biz!int biz = new Biz!int(123);" out of the main function. Compiler complains about static this. Okay, then the code outside the main function now looks this way: class Biz(T) { private T value; this(T value) { this.value = value; } } static void addToBiz(T)(Biz!T biz) { // ... } Biz!int biz; static this() { biz = new Biz!int(123); } int main() { // ... } However, this results in no gain as the compiler now shows the initial error again: 'Error: static assert: "Aliases to mutable thread-local data not allowed."' Everything I tried on my own was also to no avail. If someone could gould give me a hint again ... ;-) Thank you. If I supply a callback function with the parameter not being an instance from a parameterized class I get the same error. The problem seems to be that the parameter of the callback function takes on object as a parameter and not a built-in type like int or String. The samples on how to use the spawn function on dlang.org does not contain a sample on how to get things to work with a objecgt being supllied as parameter to the callback function
Re: "is not an lvalue" when passing template function to spawn function
On Wednesday, 8 November 2023 at 16:47:02 UTC, Paul Backus wrote: On Wednesday, 8 November 2023 at 16:30:49 UTC, Bienlein wrote: ... The actual problem here is that you can't take the address of a template without instantiating it first. To make your example work, replace `` with `!int`, like this: spawn(!int, biz); Thanks, Paul. This helped a step further. When applying your change it looks like this: Biz!int biz = new Biz!int(123); spawn(!int, biz); Then I get this error: 'Error: static assert: "Aliases to mutable thread-local data not allowed."' For this error I found this in the Internet: https://stackoverflow.com/questions/14395018/aliases-to-mutable-thread-local-data-not-allowed But this change, did not help: spawn(!int, cast(shared) biz); Then I moved "Biz!int biz = new Biz!int(123);" out of the main function. Compiler complains about static this. Okay, then the code outside the main function now looks this way: class Biz(T) { private T value; this(T value) { this.value = value; } } static void addToBiz(T)(Biz!T biz) { // ... } Biz!int biz; static this() { biz = new Biz!int(123); } int main() { // ... } However, this results in no gain as the compiler now shows the initial error again: 'Error: static assert: "Aliases to mutable thread-local data not allowed."' Everything I tried on my own was also to no avail. If someone could gould give me a hint again ... ;-) Thank you.
Re: "is not an lvalue" when passing template function to spawn function
On Wednesday, 8 November 2023 at 16:30:49 UTC, Bienlein wrote: Hello, I get the error "`addToBiz(T)(Biz!T biz)` is not an lvalue and cannot be modified" when compiling the code below. Can't find a way how to do it right. Am a D newbie and would appreciate some help. [...] static void addToBiz(T)(Biz!T biz) { // ... } int main() { auto biz = new Biz!int(123); spawn(, biz); } This is a really bad error message. The actual problem here is that you can't take the address of a template without instantiating it first. To make your example work, replace `` with `!int`, like this: spawn(!int, biz);
"is not an lvalue" when passing template function to spawn function
Hello, I get the error "`addToBiz(T)(Biz!T biz)` is not an lvalue and cannot be modified" when compiling the code below. Can't find a way how to do it right. Am a D newbie and would appreciate some help. Thank you, Bienlein class Biz(T) { private T value; this(T value) { this.value = value; } } static void addToBiz(T)(Biz!T biz) { // ... } int main() { auto biz = new Biz!int(123); spawn(, biz); }
Re: C function taking two function pointers that share calculation
On Wednesday, 14 September 2022 at 18:02:07 UTC, JG wrote: [snip] Maybe others know better but I would have thought the only way is to use globals to do this. Often c libraries that I have used get round this by taking a function and a pointer and then the library calls your function on the pointer simulating a d delegate. The C function does make use of a pointer to some data (that I neglected to mention), but your comment gives me an idea. Thanks.
Re: C function taking two function pointers that share calculation
On Wednesday, 14 September 2022 at 17:23:47 UTC, jmh530 wrote: There is a C library I sometimes use that has a function that takes two function pointers. However, there are some calculations that are shared between the two functions that would get pointed to. I am hoping to only need to do these calculations once. [...] Maybe others know better but I would have thought the only way is to use globals to do this. Often c libraries that I have used get round this by taking a function and a pointer and then the library calls your function on the pointer simulating a d delegate.
C function taking two function pointers that share calculation
There is a C library I sometimes use that has a function that takes two function pointers. However, there are some calculations that are shared between the two functions that would get pointed to. I am hoping to only need to do these calculations once. The code below sketches out the general idea of what I've tried so far. The function `f` handles both of the calculations that would be needed, returning a struct. Functions `gx` and `gy` can return the field of the struct that is relevant. Both of them could then get fed into the C function as function pointers. My concern is that `f` would then get called twice, whereas that wouldn't be the case in a simpler implementation (`gx_simple`, `gy_simple`). ldc will optimize the issue away in this simple example, but I worry that might not generally be the case. How do I ensure that the commonCalculation is only done once? ```d struct Foo { int x; int y; } Foo f(int x, int a) { int commonCalculation = a * x; return Foo(commonCalculation * x, 2 * commonCalculation); } int gx(int x, int a) { return f(x, a).x;} int gy(int x, int a) { return f(x, a).y;} //int gx_simple(int x, int a) { return a * x * x;} //int gy_simple(int x, int a) { return 2 * a * x;} void main() { import core.stdc.stdio: printf; printf("the value of x is %i\n", gx(3, 2)); printf("the value of y is %i\n", gy(3, 2)); } ```
Re: How do I assign attributes of a function to another function?
On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote: ...e.g. ```d // ... mixin ("ReturnType /*...snip...*/ " ~ member ~ "()(Parameters! /*...snip...*/ ``` Note the `()` before parameter list. This would make your member function a function template, for which attributes will be inferred by the compiler based on the calls you make in the function body. Of course, making it a template like this makes it non-composable with that same type as you're only inspecting functions, so you wouldn't be able to do e.g. a Group!(Group!(A, 3), 4);
Re: How do I assign attributes of a function to another function?
On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote: I am creating a templated object that is a storehouse for a heap object and executes their methods and returns an array of results. With the help of a template, I want to achieve this, but I want to assign the same attributes to the function. How can one pass the attributes of a function to another function? There's https://dlang.org/spec/traits.html#getFunctionAttributes . Or you could define your functions as function templates, letting the compiler infer attributes. Especially as it looks like you function's attributes may not be made the same as the ones you defer to: since you're allocating with the GC, your function cannot be @nogc even if a deferred one can.
How do I assign attributes of a function to another function?
I am creating a templated object that is a storehouse for a heap object and executes their methods and returns an array of results. With the help of a template, I want to achieve this, but I want to assign the same attributes to the function. How can one pass the attributes of a function to another function? ```d template Group(T, int objects) { struct Group { enum __length = __traits(allMembers, T).length; T[objects] __objects; this(T[objects] __objctor) @safe { __objects = __objctor; } static foreach (member; __traits(allMembers, T)) { static if ( __traits(getVisibility, __traits(getMember, T, member)) != "private" && __traits(getVisibility, __traits(getMember, T, member)) != "protected") { static if (isFunction!(__traits(getMember, T, member))) { mixin ("ReturnType!(__traits(getMember, T, member))[] " ~ member ~ "(Parameters!(__traits(getMember, T, member)) args) { ReturnType!(__traits(getMember, T, member))[] results; foreach (e; __objects) results ~= e." ~ member ~ "(args); return results; }"); }else static if (member != "Monitor") { mixin ("typeof(__traits(getMember, T, member))[] " ~ member ~ "() { typeof(__traits(getMember, T, member))[] results; foreach (e; __objects) results ~= e." ~ member ~ "; return results; }"); } } } } } ```
Re: How to convert member function to free function?
On Friday, 18 September 2020 at 18:20:41 UTC, Andrey Zherikov wrote: How can I rewrite foo() function as a free-function that won't cause struct copying? I found solution: struct S { int i = -1; this(int n) {i=n;writeln(," ",i," ",__PRETTY_FUNCTION__);} this(ref return scope inout S rhs) inout {i=rhs.i+1;writeln(," ",i," ",__PRETTY_FUNCTION__);} ~this() {writeln(," ",i," ",__PRETTY_FUNCTION__);} ref auto getRef() return { writeln(," ",i," ",__PRETTY_FUNCTION__); return this; } } ref auto foo(return ref S s) { writeln(," ",s.i," ",__PRETTY_FUNCTION__); return s; } void main() { S(5).getRef().foo().foo().foo(); } Output confirms that there is no copying happens: 7FFDE98BDDF0 5 S onlineapp.S.this(int n) ref 7FFDE98BDDF0 5 onlineapp.S.getRef() ref return 7FFDE98BDDF0 5 onlineapp.foo(return ref S s) ref @system 7FFDE98BDDF0 5 onlineapp.foo(return ref S s) ref @system 7FFDE98BDDF0 5 onlineapp.foo(return ref S s) ref @system 7FFDE98BDDF0 5 void onlineapp.S.~this()
Re: How to convert member function to free function?
On Friday, 18 September 2020 at 18:43:38 UTC, H. S. Teoh wrote: Why can't you return by ref, which would also avoid the copying? ref S foo(return ref S s) { return s; } Compiler errors out: onlineapp.d(9): Error: function onlineapp.foo(return ref S s) is not callable using argument types (S) onlineapp.d(9):cannot pass rvalue argument S() of type S to parameter return ref S s
Re: How to convert member function to free function?
On Fri, Sep 18, 2020 at 06:20:41PM +, Andrey Zherikov via Digitalmars-d-learn wrote: > How can I rewrite foo() function as a free-function that won't cause > struct copying? My simplified code is: > > struct S > { > ref S foo() return > { > return this; > } > } > > void main() > { > S().foo().foo().foo(); > } > > If I write it like "auto foo(S s) { return s; }" then statement in > main() will copy value of S three times and I want to avoid this. Why can't you return by ref, which would also avoid the copying? ref S foo(return ref S s) { return s; } T -- Let's not fight disease by killing the patient. -- Sean 'Shaleh' Perry
How to convert member function to free function?
How can I rewrite foo() function as a free-function that won't cause struct copying? My simplified code is: struct S { ref S foo() return { return this; } } void main() { S().foo().foo().foo(); } If I write it like "auto foo(S s) { return s; }" then statement in main() will copy value of S three times and I want to avoid this.
Re: How to override impure function from pure function
On Tuesday, 13 December 2016 at 05:13:01 UTC, Nikhil Jacob wrote: I mistook the original statement to mean that an impure function can be called from a pure function with some manual overrides. Thank you for the clarification. Yeah you can't do that, except in a debug statement. You can however cheat with SetFunctionAttributes (from std.traits).
Re: How to override impure function from pure function
On Tuesday, 13 December 2016 at 04:48:11 UTC, Nikhil Jacob wrote: In the D spec for pure functions it says that a pure function can override "can override an impure function, but an impure function cannot override a pure one" Can anyone help me how to do this ? what this means is class Foo { void foo() { ... } } class Bar : Foo { override void foo() pure { ... } } is allowed. but int someglobal; class Foo { void foo() pure { ... } } class Bar : Foo { override void foo() { someglobal = 42; } } in not.
Re: How to override impure function from pure function
On Tuesday, 13 December 2016 at 05:10:02 UTC, Nicholas Wilson wrote: On Tuesday, 13 December 2016 at 04:48:11 UTC, Nikhil Jacob wrote: In the D spec for pure functions it says that a pure function can override "can override an impure function, but an impure function cannot override a pure one" Can anyone help me how to do this ? what this means is class Foo { void foo() { ... } } class Bar : Foo { override void foo() pure { ... } } is allowed. but int someglobal; class Foo { void foo() pure { ... } } class Bar : Foo { override void foo() { someglobal = 42; } } in not. I mistook the original statement to mean that an impure function can be called from a pure function with some manual overrides. Thank you for the clarification.
How to override impure function from pure function
In the D spec for pure functions it says that a pure function can override "can override an impure function, but an impure function cannot override a pure one" Can anyone help me how to do this ?
Re: function argument accepting function or delegate?
On 1/17/16 1:27 AM, Jon D wrote: My underlying question is how to compose functions taking functions as arguments, while allowing the caller the flexibility to pass either a function or delegate. Simply declaring an argument as either a function or delegate seems to prohibit the other. Overloading works. Are there better ways? If you are looking for a runtime way to do this (the template alias way works too), you may look at std.functional.toDelegate which converts a function pointer to a delegate. But it is awkward to make this a requirement for your user. -Steve
function argument accepting function or delegate?
My underlying question is how to compose functions taking functions as arguments, while allowing the caller the flexibility to pass either a function or delegate. Simply declaring an argument as either a function or delegate seems to prohibit the other. Overloading works. Are there better ways? An example: auto callIntFn (int function(int) f, int x) { return f(x); } auto callIntDel (int delegate(int) f, int x) { return f(x); } auto callIntFnOrDel (int delegate(int) f, int x) { return f(x); } auto callIntFnOrDel (int function(int) f, int x) { return f(x); } void main(string[] args) { alias AddN = int delegate(int); AddN makeAddN(int n) { return x => x + n; } auto addTwo = makeAddN(2);// Delegate int function(int) addThree = x => x + 3; // Function // assert(callIntFn(addTwo, 4) == 6); // Compile error // assert(callIntDel(addThree, 4) == 7); // Compile error assert(callIntDel(addTwo, 4) == 6); assert(callIntFn(addThree, 4) == 7); assert(callIntFnOrDel(addTwo, 4) == 6); assert(callIntFnOrDel(addThree, 4) == 7); } ---Jon
Re: function argument accepting function or delegate?
On Sunday, 17 January 2016 at 06:27:41 UTC, Jon D wrote: My underlying question is how to compose functions taking functions as arguments, while allowing the caller the flexibility to pass either a function or delegate. [...] Templates are an easy way. --- auto call(F, Args...)(F fun, auto ref Args args) { return fun(args); } --- Would probably look nicer with some constraints from std.traits.
Re: function argument accepting function or delegate?
On Sunday, 17 January 2016 at 06:49:23 UTC, rsw0x wrote: On Sunday, 17 January 2016 at 06:27:41 UTC, Jon D wrote: My underlying question is how to compose functions taking functions as arguments, while allowing the caller the flexibility to pass either a function or delegate. [...] Templates are an easy way. --- auto call(F, Args...)(F fun, auto ref Args args) { return fun(args); } --- Would probably look nicer with some constraints from std.traits. Thanks much, that works!
Re: Function name from function pointer
On Saturday, 11 April 2015 at 19:08:50 UTC, Marco Leise wrote: Am Sat, 11 Apr 2015 18:28:35 + schrieb Paul D Anderson claude.re...@msnmail.com: Is there a way to return the name of a function (a string) from a pointer to that function? Function pointer example from D Reference: --- int function() fp; void test() { static int a = 7; static int foo() { return a + 3; } fp = foo; } void bar() { test(); int i = fp(); // i is set to 10 } --- Can I get foo from fp? Paul Nope, that would require that fp not only contains a pointer to the function but also a pointer to the name. That's not how it works. But continuing that thought, you could add the function's name as an additional variable and set that every time you set fp. Okay, thanks, I can see that. Paul
Re: Function name from function pointer
Am Sat, 11 Apr 2015 18:28:35 + schrieb Paul D Anderson claude.re...@msnmail.com: Is there a way to return the name of a function (a string) from a pointer to that function? Function pointer example from D Reference: --- int function() fp; void test() { static int a = 7; static int foo() { return a + 3; } fp = foo; } void bar() { test(); int i = fp(); // i is set to 10 } --- Can I get foo from fp? Paul Nope, that would require that fp not only contains a pointer to the function but also a pointer to the name. That's not how it works. But continuing that thought, you could add the function's name as an additional variable and set that every time you set fp. -- Marco
Re: Function name from function pointer
Paul D Anderson: Is there a way to return the name of a function (a string) from a pointer to that function? Perhaps creating a string[void*] AA and initializing with all the function pointers you care about. Bye, bearophile
Function name from function pointer
Is there a way to return the name of a function (a string) from a pointer to that function? Function pointer example from D Reference: --- int function() fp; void test() { static int a = 7; static int foo() { return a + 3; } fp = foo; } void bar() { test(); int i = fp(); // i is set to 10 } --- Can I get foo from fp? Paul
Re: Call a function with a function pointer
Am 10.10.2013 17:45, schrieb Namespace: On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote: Namespace: You mean like this? void foo(T)(extern(C) void function(T*) func) { } That prints: Error: basic type expected, not extern In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested): alias TF = extern(C) void function(T*); void foo(T)(TF func) {} Bye, bearophile /d917/f732.d(8): Error: basic type expected, not extern /d917/f732.d(8): Error: semicolon expected to close alias declaration /d917/f732.d(8): Error: no identifier for declarator void function(T*) I found a possible workaround. Its ugly as hell, but at least it works until the bugs are fixed. The trick is to make a helper struct. Define the function you want within that, and then use typeof to get the type. import std.stdio; extern(C) void testFunc(int* ptr) { *ptr = 5; } struct TypeHelper(T) { extern(C) static void func(T*); alias typeof(func) func_t; } void Foo(T)(TypeHelper!T.func_t func, T* val) { func(val); } void main(string[] args) { pragma(msg, TypeHelper!int.func_t.stringof); int test = 0; Foo!int(testFunc, test); writefln(%d, test); } -- Kind Regards Benjamin Thaut
Re: Call a function with a function pointer
On 10/13/13 16:43, Benjamin Thaut wrote: Am 10.10.2013 17:45, schrieb Namespace: On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote: Namespace: You mean like this? void foo(T)(extern(C) void function(T*) func) { } That prints: Error: basic type expected, not extern In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested): alias TF = extern(C) void function(T*); void foo(T)(TF func) {} Bye, bearophile /d917/f732.d(8): Error: basic type expected, not extern /d917/f732.d(8): Error: semicolon expected to close alias declaration /d917/f732.d(8): Error: no identifier for declarator void function(T*) I found a possible workaround. Its ugly as hell, but at least it works until the bugs are fixed. There's no need for such ugly workarounds -- this is just a problem with the *new* alias syntax. The old one accepts it (unless this changed recently): alias extern(C) static void function(int*) Func_t; artur
Re: Call a function with a function pointer
Am 13.10.2013 17:17, schrieb Artur Skawina: On 10/13/13 16:43, Benjamin Thaut wrote: Am 10.10.2013 17:45, schrieb Namespace: On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote: Namespace: You mean like this? void foo(T)(extern(C) void function(T*) func) { } That prints: Error: basic type expected, not extern In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested): alias TF = extern(C) void function(T*); void foo(T)(TF func) {} Bye, bearophile /d917/f732.d(8): Error: basic type expected, not extern /d917/f732.d(8): Error: semicolon expected to close alias declaration /d917/f732.d(8): Error: no identifier for declarator void function(T*) I found a possible workaround. Its ugly as hell, but at least it works until the bugs are fixed. There's no need for such ugly workarounds -- this is just a problem with the *new* alias syntax. The old one accepts it (unless this changed recently): alias extern(C) static void function(int*) Func_t; artur Oh so this bug was fixed? Thats good to know.
Re: Call a function with a function pointer
On 10/10/13 20:54, Dicebot wrote: On Thursday, 10 October 2013 at 17:47:54 UTC, Namespace wrote: import std.stdio; void foo1(void function(void*) fp) { } void foo2(void function(int) fp) { } void foo3(void*) { } void main() { foo1((void* ptr) = ( assert(ptr is null) )); foo2((int a) = ( a + 1 )); /// Fails: Error: function foo2 (void function(int) fp) is not callable using argument types (int function(int a) pure nothrow @safe) foo1(foo3); void foo4(void function(void*) fp) { } foo1(foo4); /// Fails: Error: function foo1 (void function(void*) fp) is not callable using argument types (void delegate(void function(void*) fp)) } You are using short lambda syntax a = b. Here `b` is always return statement. It is equivalent to (a) { return b; }. And your `foo2` signature expects lambda returning void, like (a) { return; } Second error is DMD incompetence in deducing minimal required type of nested function. It always treats them as delegates (== having hidden context pointer) even if those do not refer any actual context. And plain lambdas are of course binary incompatible with delegates (closures) because of that extra pointer field. It's probably not just incompetence (the compiler is able to figure this out in other contexts), but a deliberate choice. Having function types depend on their bodies would not be a good idea. Eg int c; auto f() { int a = 42; int f1() { return a; } int f2() { return 0; } return !c?f1:f2; } Mark f2 as 'static' and this code will no longer compile. If that would be done automatically then you'd have to 'undo' it manually, which would cause even more problems (consider generic code, which isn't prepared to handle this). artur [1] at least without other language improvements; enabling overloading on 'static', plus a few other enhancements, would change the picture.
Re: Call a function with a function pointer
On Friday, 11 October 2013 at 15:55:17 UTC, Artur Skawina wrote: It's probably not just incompetence (the compiler is able to figure this out in other contexts), but a deliberate choice. Having function types depend on their bodies would not be a good idea. Eg int c; auto f() { int a = 42; int f1() { return a; } int f2() { return 0; } return !c?f1:f2; } Mark f2 as 'static' and this code will no longer compile. If that would be done automatically then you'd have to 'undo' it manually, which would cause even more problems (consider generic code, which isn't prepared to handle this). artur [1] at least without other language improvements; enabling overloading on 'static', plus a few other enhancements, would change the picture. Agreed. However, I do feel uncomfortable with new habit to put `static` everywhere to avoid hidden compiler help :(
Call a function with a function pointer
I have this function: void foo(T)(void function(T*) test) { } And want to call it with a C function: foo!(SDL_Surface)(SDL_FreeSurface); but I get: Fehler 1 Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow) What would be the smartest solution?
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 14:13:47 UTC, Namespace wrote: I have this function: void foo(T)(void function(T*) test) { } And want to call it with a C function: foo!(SDL_Surface)(SDL_FreeSurface); but I get: Fehler 1 Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow) What would be the smartest solution? Wrap it in a lambda. Or change foo() signature to accept `extern(C)` functions - you can't just mix calling convention.
Re: Call a function with a function pointer
Am 10.10.2013 16:13, schrieb Namespace: I have this function: void foo(T)(void function(T*) test) { } And want to call it with a C function: foo!(SDL_Surface)(SDL_FreeSurface); but I get: Fehler1Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow) What would be the smartest solution? If you can change the signature of foo just add a extern(c) to the function pointer declaration. Otherwise just wrap the SDL_FreeSurface call into a delegate on the caller side. -- Kind Regards Benjamin Thaut
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 14:40:09 UTC, Namespace wrote: Example? I do not use lambdas often. void foo(T)(void function(T*) test) { } extern(C) void bar(int*) { } void main() { foo( (int* a) = bar(a) ); } I don't know to what extent IFTI can work here though.
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 14:28:20 UTC, Dicebot wrote: On Thursday, 10 October 2013 at 14:13:47 UTC, Namespace wrote: I have this function: void foo(T)(void function(T*) test) { } And want to call it with a C function: foo!(SDL_Surface)(SDL_FreeSurface); but I get: Fehler 1 Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow) What would be the smartest solution? Wrap it in a lambda. Example? I do not use lambdas often.
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 14:44:00 UTC, Dicebot wrote: On Thursday, 10 October 2013 at 14:40:09 UTC, Namespace wrote: Example? I do not use lambdas often. void foo(T)(void function(T*) test) { } extern(C) void bar(int*) { } void main() { foo( (int* a) = bar(a) ); } I don't know to what extent IFTI can work here though. That works. Thanks.
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote: Namespace: You mean like this? void foo(T)(extern(C) void function(T*) func) { } That prints: Error: basic type expected, not extern In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested): alias TF = extern(C) void function(T*); void foo(T)(TF func) {} Bye, bearophile /d917/f732.d(8): Error: basic type expected, not extern /d917/f732.d(8): Error: semicolon expected to close alias declaration /d917/f732.d(8): Error: no identifier for declarator void function(T*)
Re: Call a function with a function pointer
Namespace: /d917/f732.d(8): Error: basic type expected, not extern /d917/f732.d(8): Error: semicolon expected to close alias declaration /d917/f732.d(8): Error: no identifier for declarator void function(T*) It seems that even the new alias syntax doesn't support the extern :-) Perhaps this bug is not yet in Bugzilla. Try: alias extern(C) void function(T*) TF; void foo(T)(TF func) {} Bye, bearophile
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote: Namespace: You mean like this? void foo(T)(extern(C) void function(T*) func) { } That prints: Error: basic type expected, not extern In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested): alias TF = extern(C) void function(T*); void foo(T)(TF func) {} Bye, bearophile That is limitation of current extern - it can only be attached to symbol declarations, not types. AFAIK you need to do `extern(C) alias TF = ...` but anyway this method is very likely to break IFTI completely.
Re: Call a function with a function pointer
import std.stdio; void foo1(void function(void*) fp) { } void foo2(void function(int) fp) { } void foo3(void*) { } void main() { foo1((void* ptr) = ( assert(ptr is null) )); foo2((int a) = ( a + 1 )); /// Fails: Error: function foo2 (void function(int) fp) is not callable using argument types (int function(int a) pure nothrow @safe) foo1(foo3); void foo4(void function(void*) fp) { } foo1(foo4); /// Fails: Error: function foo1 (void function(void*) fp) is not callable using argument types (void delegate(void function(void*) fp)) } Can someone explain that to me?
Re: Call a function with a function pointer
On Thursday, 10 October 2013 at 17:47:54 UTC, Namespace wrote: import std.stdio; void foo1(void function(void*) fp) { } void foo2(void function(int) fp) { } void foo3(void*) { } void main() { foo1((void* ptr) = ( assert(ptr is null) )); foo2((int a) = ( a + 1 )); /// Fails: Error: function foo2 (void function(int) fp) is not callable using argument types (int function(int a) pure nothrow @safe) foo1(foo3); void foo4(void function(void*) fp) { } foo1(foo4); /// Fails: Error: function foo1 (void function(void*) fp) is not callable using argument types (void delegate(void function(void*) fp)) } Can someone explain that to me? You are using short lambda syntax a = b. Here `b` is always return statement. It is equivalent to (a) { return b; }. And your `foo2` signature expects lambda returning void, like (a) { return; } Second error is DMD incompetence in deducing minimal required type of nested function. It always treats them as delegates (== having hidden context pointer) even if those do not refer any actual context. And plain lambdas are of course binary incompatible with delegates (closures) because of that extra pointer field.
Re: Call a function with a function pointer
On 10/10/13, bearophile bearophileh...@lycos.com wrote: Perhaps this bug is not yet in Bugzilla. I'm pretty sure I saw it filed somewhere. Can't find it though..
Re: Call a function with a function pointer
Andrej Mitrovic: I'm pretty sure I saw it filed somewhere. Can't find it though.. I have just added the new test case :-) http://d.puremagic.com/issues/show_bug.cgi?id=6754 Bye, bearophile
Re: Templated Function can't deduce function arguments
On Wed, 22 May 2013 23:28:21 -0400, Jonathan M Davis wrote: On Wednesday, May 22, 2013 21:31:53 Steven Schveighoffer wrote: On Wed, 22 May 2013 21:16:44 -0400, Jonathan Crapuchettes jcrapuchet...@gmail.com wrote: Can anyone tell me why this doesn't compile? Dmd 2.062 says that it cannot deduce the template function from arguments types. import std.stdio; void main() { test!(dchar, int)('b', 6, 'a', 54); } template test(Types...) { void test(T...)(const Types v, const T values...) Do you need that last elipsis? I thought you didn't, but not sure. You don't, and I'm surprised that it compiles, since I don't think that the elipsis is actually legal there. AFAIK, the only time that an elipsis is legal in the function arguments is with array variadics; e.g. auto foo(int[] bar...) {...} - Jonathan M Davis The last ellipsis was a remnant of testing. Thank you for pointing that out. Removing it still doesn't help the compiler deduce the argument types. It appears that the issue has to do with the usage of the Types TypeTuple. If the const Types v is swapped out for const dchar v1, const int v2 the code compiles just fine. This makes me wonder if dmd is not interpreting the Types TypeTuple correctly in the inner-function. Jonathan
Re: Templated Function can't deduce function arguments
On 05/23/2013 07:21 PM, Jonathan Crapuchettes wrote: On Wed, 22 May 2013 23:28:21 -0400, Jonathan M Davis wrote: On Wednesday, May 22, 2013 21:31:53 Steven Schveighoffer wrote: On Wed, 22 May 2013 21:16:44 -0400, Jonathan Crapuchettes jcrapuchet...@gmail.com wrote: Can anyone tell me why this doesn't compile? Dmd 2.062 says that it cannot deduce the template function from arguments types. import std.stdio; void main() { test!(dchar, int)('b', 6, 'a', 54); } template test(Types...) { void test(T...)(const Types v, const T values...) Do you need that last elipsis? I thought you didn't, but not sure. You don't, and I'm surprised that it compiles, since I don't think that the elipsis is actually legal there. AFAIK, the only time that an elipsis is legal in the function arguments is with array variadics; e.g. auto foo(int[] bar...) {...} - Jonathan M Davis The last ellipsis was a remnant of testing. Thank you for pointing that out. Removing it still doesn't help the compiler deduce the argument types. It appears that the issue has to do with the usage of the Types TypeTuple. If the const Types v is swapped out for const dchar v1, const int v2 the code compiles just fine. This makes me wonder if dmd is not interpreting the Types TypeTuple correctly in the inner-function. Jonathan Yes, this is indeed a compiler bug. http://d.puremagic.com/issues/
Re: Templated Function can't deduce function arguments
Thank you for the help. Bug report at http://d.puremagic.com/issues/ show_bug.cgi?id=10156
Templated Function can't deduce function arguments
Can anyone tell me why this doesn't compile? Dmd 2.062 says that it cannot deduce the template function from arguments types. import std.stdio; void main() { test!(dchar, int)('b', 6, 'a', 54); } template test(Types...) { void test(T...)(const Types v, const T values...) { writefln(%s,%s, v); foreach (s; values) writefln(%s,%s, s); } } Thank you, Jonathan
Re: Templated Function can't deduce function arguments
On Wed, 22 May 2013 21:16:44 -0400, Jonathan Crapuchettes jcrapuchet...@gmail.com wrote: Can anyone tell me why this doesn't compile? Dmd 2.062 says that it cannot deduce the template function from arguments types. import std.stdio; void main() { test!(dchar, int)('b', 6, 'a', 54); } template test(Types...) { void test(T...)(const Types v, const T values...) Do you need that last elipsis? I thought you didn't, but not sure. -Steve
Re: function is not function
On 21/09/12 21:59, Ellery Newcomer wrote: solution is to use std.traits, but can someone explain this to me? import std.stdio; void main() { auto a = { writeln(hi); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! } The 'function' keyword is an ugly wart in the language. In void function() 'function' means 'function pointer'. But in is (X == function) 'function' means 'function'. Which is actually pretty much useless. You always want 'function pointer'. This is the only case where function type still exists in the language.
function is not function
solution is to use std.traits, but can someone explain this to me? import std.stdio; void main() { auto a = { writeln(hi); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! }
Re: function is not function
On 09/21/2012 12:59 PM, Ellery Newcomer wrote: solution is to use std.traits, but can someone explain this to me? import std.stdio; void main() { auto a = { writeln(hi); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! } You have probably tried the following already: pragma(msg, is(typeof(a) == void function())); Regardless, I think it is a bug because the documentation says that the 'function' keyword alone should work: http://dlang.org/expression.html#IsExpression quote is ( Type == TypeSpecialization ) The condition is satisfied if Type is semantically correct and is the same type as TypeSpecialization. If TypeSpecialization is one of struct union class interface enum function delegate const immutable shared then the condition is satisifed if Type is one of those. /quote Ali
Re: function is not function
Ellery Newcomer: import std.stdio; void main() { auto a = { writeln(hi); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! } There is a subtle difference between function type, type of function pointer and delegate type. int foo1() { return 0; } void main() { static assert(is(typeof(foo1) == function)); auto foo2 = { return 0; }; static assert(is(typeof(*foo2) == function)); } Bye, bearophile
Re: function is not function
On 09/21/2012 01:10 PM, Ali Çehreli wrote: You have probably tried the following already: pragma(msg, is(typeof(a) == void function())); No, but that's also not very generic. void main() { auto a = { return 1; }; pragma(msg, is(typeof(a) == void function())); // nope! pragma(msg, typeof(a)); // void function() pure nothrow @safe } guess what I'm fighting with just now
Re: function is not function
On 09/21/2012 01:17 PM, bearophile wrote: pragma(msg, is(typeof(a) == function)); // nope! code in pyd suggests this evaluated to true once upon a time.
Re: function is not function
On 09/21/2012 10:41 PM, Ellery Newcomer wrote: On 09/21/2012 01:17 PM, bearophile wrote: pragma(msg, is(typeof(a) == function)); // nope! code in pyd suggests this evaluated to true once upon a time. I don't think it ever did. It is just very easy to get wrong.
Re: function is not function
On Friday, September 21, 2012 12:59:31 Ellery Newcomer wrote: solution is to use std.traits, but can someone explain this to me? import std.stdio; void main() { auto a = { writeln(hi); }; pragma(msg, typeof(a)); // void function() pragma(msg, is(typeof(a) == delegate)); // nope! pragma(msg, is(typeof(a) == function)); // nope! } Sorry if this ends up being a double-post, but the post I made hours ago doesn't seem to be showing up, so I'm posting it again: http://stackoverflow.com/questions/11067972 - Jonathan M Davis
normal function and template function conflict
I was trying to write a PRNG, and I wanted to give it two seed methods. The first is to seed from a unique UIntType, and the other is to seed from an entire range of seeds. Like so: void seed(UIntType value = default_seed) {...} void seed(Range)(Range range) if (isInputRange!Range) {...} Where UIntType is a template parameter of the struct. However, this makes the compiler complain, because of a conflict...? Is mixing normal functions with parametrized ones impossible? I *fixed* the issue by contemplating the first call with: void seed(T)(T value = default_seed) if (is(typeof(T == UIntType))) {...} Problems: 1) It is ugly as sin. 2) The default parameter doesn't work anymore. So here are my two questions: 1) Is what I was originally trying to do actually illegal, or is it some sort of compiler limitation? TDPL implies this should work perfectly fine... 2) Is there a correct workaround?
Re: normal function and template function conflict
On Thu, 26 Jul 2012 19:18:21 +0200, monarch_dodra monarchdo...@gmail.com wrote: So here are my two questions: 1) Is what I was originally trying to do actually illegal, or is it some sort of compiler limitation? TDPL implies this should work perfectly fine... Compiler limitation. It's supposed to work. 2) Is there a correct workaround? Exactly what you did. Though, for brevity, you would write this: void seed(T : UIntType)(T value = default_seed) -- Simen
Re: normal function and template function conflict
On Thursday, 26 July 2012 at 17:57:31 UTC, Simen Kjaeraas wrote: On Thu, 26 Jul 2012 19:18:21 +0200, monarch_dodra monarchdo...@gmail.com wrote: 2) Is there a correct workaround? Exactly what you did. Though, for brevity, you would write this: void seed(T : UIntType)(T value = default_seed) Thanks I haven't seen this construct before. Can you tell me a bit more about it, or link me to some documentation about it? I suppose it means T must be UIntType, but I'd enjoy having a broader understanding of it :)
Re: normal function and template function conflict
On 07/26/2012 11:14 AM, monarch_dodra wrote: On Thursday, 26 July 2012 at 17:57:31 UTC, Simen Kjaeraas wrote: On Thu, 26 Jul 2012 19:18:21 +0200, monarch_dodra monarchdo...@gmail.com wrote: 2) Is there a correct workaround? Exactly what you did. Though, for brevity, you would write this: void seed(T : UIntType)(T value = default_seed) Thanks I haven't seen this construct before. Can you tell me a bit more about it, or link me to some documentation about it? I suppose it means T must be UIntType, but I'd enjoy having a broader understanding of it :) Search for specialization in the following resources: http://dlang.org/template.html https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf http://ddili.org/ders/d.en/templates.html Ali
Re: normal function and template function conflict
On Thu, 26 Jul 2012 20:14:10 +0200, monarch_dodra monarchdo...@gmail.com wrote: On Thursday, 26 July 2012 at 17:57:31 UTC, Simen Kjaeraas wrote: On Thu, 26 Jul 2012 19:18:21 +0200, monarch_dodra monarchdo...@gmail.com wrote: 2) Is there a correct workaround? Exactly what you did. Though, for brevity, you would write this: void seed(T : UIntType)(T value = default_seed) Thanks I haven't seen this construct before. Can you tell me a bit more about it, or link me to some documentation about it? I suppose it means T must be UIntType, but I'd enjoy having a broader understanding of it :) Ali gave the general, I'll give the specifics. is(T : Foo), void bar(T : Foo)(T t), and a few others (not really others, they're exactly the same!) means 'T is implicitly convertible to Foo'. What's the difference, you ask? Consider: void foo(T)(T value) if (is(T == uint)) {} Could you call this function like this: foo(3); The answer is no. The compiler translates this to: foo!(typeof(3))(3); And typeof(3) is not uint, it's int. In contrast, void foo(T : uint)(T value) {} foo(3); is also translated to foo!(typeof(3))(3); and the compiler then checks if int is implicitly convertible to uint. And so it is, so the compiler moves happily onwards. There is a reason I included is(T : Foo) in the beginning, for you can write the exact same constraint like this: void foo(T)(T value) if (is(T : uint)) {} and it will compile just the same. For more information on this construct, I would, in addition to the links Ali provided, recommend you read this: http://dlang.org/expression.html#IsExpression IsExpressions are, however, probably the most hairy part of D, and their understanding has proven troublesome to many (myself included, though I believe I have grasped them now). Hence, most of their functionality is wrapped in more easily understandable templates in std.traits. -- Simen
Re: normal function and template function conflict
On Thursday, 26 July 2012 at 18:21:18 UTC, Ali Çehreli wrote: Search for specialization in the following resources: Oh... Specialization. What with D's ability to conditionally implement, I had completely forgotten about specialization. So that's how it's done in D. Cool. Thanks a lot.
Re: normal function and template function conflict
On 2012-07-26 19:57, Simen Kjaeraas wrote: 2) Is there a correct workaround? Exactly what you did. Though, for brevity, you would write this: void seed(T : UIntType)(T value = default_seed) Since a template function is actually not wanted this would be the correct workaround: void seed () (UIntType value = default_seed) Less typing as well. -- /Jacob Carlborg
Re: normal function and template function conflict
On 7/26/12, Jacob Carlborg d...@me.com wrote: void seed () (UIntType value = default_seed) Less typing as well. Yep. It's funny how this works at all. I mean a template with no template parameters is somehow still a template. :)
Re: normal function and template function conflict
On Thursday, July 26, 2012 21:49:35 Andrej Mitrovic wrote: On 7/26/12, Jacob Carlborg d...@me.com wrote: void seed () (UIntType value = default_seed) Less typing as well. Yep. It's funny how this works at all. I mean a template with no template parameters is somehow still a template. :) It _is _ bit funny, but Ibelieve that it comes from permitting empty template parameter lists. Without that, recursion with eponymous templates would become problematic. e.g. enum template myTemplate(T...) { static if(T.length == 0) enum myTemplate = 42; else enum myTemplate = T[0] * 3 + myTemplate!(T[1 .. $]); } needs to be able to take an empty parameter list. It's probably possible to make that work without allowing an outright empty parameter list that isn't even a variadic template, but I suspect that it's just easier to allow it. - Jonathan M Davis