Re: type of functions
On Monday, 10 May 2021 at 10:37:51 UTC, Alain De Vos wrote: Can I say that the compiler is intelligent enough to add the attributes "pure nothrow @nogc @safe" ? Yes, in those cases (and some others). The functionality is described here: https://dlang.org/spec/function.html#function-attribute-inference -- Simen
type of functions
I've written a small program and also it's output, ``` import std.stdio; double myfun(int x) pure nothrow @nogc @safe{ return x/10.0; } void main() { static double Gfunction(int x) {return x/10.0;} writeln(typeid(typeof(Gfunction))); //OUTPUT:"double function(int) pure nothrow @nogc @safe" double Lfunction(int x) {return x/10.0;} writeln(typeid(typeof(Lfunction))); //OUTPUT:"double function(int) pure nothrow @nogc @safe" alias gfunction = double function(int); gfunction g = & Gfunction; writeln(typeid(typeof(g))); //OUTPUT: "double function(int)*" double function(int) F = function double(int x) {return x/10.0;}; writeln(typeid(typeof(F))); //OUTPUT: "double function(int)*" alias lfunction = double delegate(int); lfunction l = & Lfunction; writeln(typeid(typeof(l))); //OUTPUT:"double delegate(int)" int c=2; double delegate(int) D = delegate double(int x) {return c*x/10.0;}; writeln(typeid(typeof(D))); //OUTPUT:"double delegate(int)" } ``` Can I say that the compiler is intelligent enough to add the attributes "pure nothrow @nogc @safe" ? As far as I understand it means I do not need much and I will not change much. What I find suprising is that "function" is a pointer and "delegate" is not as reported type . Feel free to elaborate.
Re: Type-qualified functions?
On 01/21/11 14:43, Sean Eskapp wrote: templates: void foo(T)(T, void delegate(T) fn) { } This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function. What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way? I believe this is the sort of thing the 'inout' qualifier was meant for, except I don't know if it works with a void return type. It's also worth noting that mutable and immutable are both castable to const, so if there is literally no difference between the two functions, see if you can get away with just the one using const. -- Chris N-S
Type-qualified functions?
How does one avoid code duplication in a snippet code like this: class A{} void foo(const A, void delegate(const A) fn) { // some stuff // ... // ... } void foo(A, void delegate(A) fn) { // exact same stuff, with different qualifiers // ... // ... }
Re: Type-qualified functions?
On Fri, 21 Jan 2011 09:08:58 -0500, Sean Eskapp eatingstap...@gmail.com wrote: How does one avoid code duplication in a snippet code like this: class A{} void foo(const A, void delegate(const A) fn) { // some stuff // ... // ... } void foo(A, void delegate(A) fn) { // exact same stuff, with different qualifiers // ... // ... } templates: void foo(T)(T, void delegate(T) fn) { } This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function. You could further restrict which templates can be instantiated with a template constraint (say for instance, to restring foo to only instantiate when T is A or const(A) ). -Steve
Re: Type-qualified functions?
templates: void foo(T)(T, void delegate(T) fn) { } This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function. What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way?
Re: Type-qualified functions?
Sean Eskapp Wrote: templates: void foo(T)(T, void delegate(T) fn) { } This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function. What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way? Their is probably better ways to do this... void main() { foo(new Foo, (int) { }); } void foo(T, U)(T t, void delegate(U) fn) if(__traits(compiles, fn(t.bar))) { } class Foo { int bar() { return 0; } }