Re: "is not an lvalue" when passing template function to spawn function

2023-11-09 Thread Bienlein via Digitalmars-d-learn
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

Re: "is not an lvalue" when passing template function to spawn function

2023-11-09 Thread Bienlein via Digitalmars-d-learn
!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) { // ... }

Re: "is not an lvalue" when passing template function to spawn function

2023-11-09 Thread Bienlein via Digitalmars-d-learn
or 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 abo

Re: "is not an lvalue" when passing template function to spawn function

2023-11-08 Thread Paul Backus via Digitalmars-d-learn
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

"is not an lvalue" when passing template function to spawn function

2023-11-08 Thread Bienlein via Digitalmars-d-learn
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)

Re: C function taking two function pointers that share calculation

2022-09-14 Thread jmh530 via Digitalmars-d-learn
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

Re: C function taking two function pointers that share calculation

2022-09-14 Thread JG via Digitalmars-d-learn
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

C function taking two function pointers that share calculation

2022-09-14 Thread jmh530 via Digitalmars-d-learn
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

Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn
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 wi

Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn
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

How do I assign attributes of a function to another function?

2021-11-05 Thread Li30U via Digitalmars-d-learn
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

Re: How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
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__);}

Re: How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
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

Re: How to convert member function to free function?

2020-09-18 Thread H. S. Teoh via Digitalmars-d-learn
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 >

How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
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) { ret

Re: How to override impure function from pure function

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn
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

Re: How to override impure function from pure function

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn
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

Re: How to override impure function from pure function

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
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

How to override impure function from pure function

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
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?

2016-01-22 Thread Steven Schveighoffer via Digitalmars-d-learn
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

function argument accepting function or delegate?

2016-01-16 Thread Jon D via Digitalmars-d-learn
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

Re: function argument accepting function or delegate?

2016-01-16 Thread rsw0x via Digitalmars-d-learn
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

Re: function argument accepting function or delegate?

2016-01-16 Thread Jon D via Digitalmars-d-learn
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

Re: Function name from function pointer

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
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

Re: Function name from function pointer

2015-04-11 Thread Marco Leise via Digitalmars-d-learn
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

Re: Function name from function pointer

2015-04-11 Thread bearophile via Digitalmars-d-learn
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

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
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

Re: Call a function with a function pointer

2013-10-13 Thread Benjamin Thaut
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

Re: Call a function with a function pointer

2013-10-13 Thread 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

Re: Call a function with a function pointer

2013-10-13 Thread Benjamin Thaut
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

Re: Call a function with a function pointer

2013-10-11 Thread Artur Skawina
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

Re: Call a function with a function pointer

2013-10-11 Thread Dicebot
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

Call a function with a function pointer

2013-10-10 Thread 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: Fehler 1 Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function

Re: Call a function with a function pointer

2013-10-10 Thread Dicebot
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

Re: Call a function with a function pointer

2013-10-10 Thread Benjamin Thaut
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

Re: Call a function with a function pointer

2013-10-10 Thread Dicebot
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

2013-10-10 Thread Namespace
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

Re: Call a function with a function pointer

2013-10-10 Thread Namespace
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

Re: Call a function with a function pointer

2013-10-10 Thread 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

Re: Call a function with a function pointer

2013-10-10 Thread bearophile
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

Re: Call a function with a function pointer

2013-10-10 Thread Dicebot
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

Re: Call a function with a function pointer

2013-10-10 Thread Namespace
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

Re: Call a function with a function pointer

2013-10-10 Thread Dicebot
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

Re: Call a function with a function pointer

2013-10-10 Thread Andrej Mitrovic
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

2013-10-10 Thread bearophile
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

2013-05-23 Thread Jonathan Crapuchettes
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

Re: Templated Function can't deduce function arguments

2013-05-23 Thread Timon Gehr
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

Re: Templated Function can't deduce function arguments

2013-05-23 Thread Jonathan Crapuchettes
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

2013-05-22 Thread Jonathan Crapuchettes
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

Re: Templated Function can't deduce function arguments

2013-05-22 Thread Steven Schveighoffer
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

Re: function is not function

2012-09-26 Thread Don Clugston
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

function is not function

2012-09-21 Thread Ellery Newcomer
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

2012-09-21 Thread Ali Çehreli
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

Re: function is not function

2012-09-21 Thread bearophile
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

Re: function is not function

2012-09-21 Thread Ellery Newcomer
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

Re: function is not function

2012-09-21 Thread Ellery Newcomer
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

2012-09-21 Thread Timon Gehr
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

2012-09-21 Thread Jonathan M Davis
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

normal function and template function conflict

2012-07-26 Thread monarch_dodra
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)

Re: normal function and template function conflict

2012-07-26 Thread Simen Kjaeraas
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

Re: normal function and template function conflict

2012-07-26 Thread monarch_dodra
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)

Re: normal function and template function conflict

2012-07-26 Thread Ali Çehreli
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:

Re: normal function and template function conflict

2012-07-26 Thread Simen Kjaeraas
)) {} 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

Re: normal function and template function conflict

2012-07-26 Thread monarch_dodra
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

2012-07-26 Thread Jacob Carlborg
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

Re: normal function and template function conflict

2012-07-26 Thread Andrej Mitrovic
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

2012-07-26 Thread Jonathan M Davis
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.