Re: How to specify an exact template?

2021-01-16 Thread solidstate1991 via Digitalmars-d-learn
Well, it's quite complicated to do. I have to manually unwrap 
each and all function template, then pray for the compile-time 
optimization gods that they'll be inlined.


This is so annoying, that I might issue a DIP...


Re: How define accepted types in a template parameter?

2021-01-16 Thread Basile B. via Digitalmars-d-learn

On Saturday, 16 January 2021 at 18:39:03 UTC, Marcone wrote:
For example, I want my function template to only accept integer 
or string;


You can do that with either

- `static if` inside the body [1]

  import std.traits;
  void foo(T)(T t)
  {
static if (isIntegral!T) {}
else static assert false;
  }


- template constraint [2]

  import std.traits;
  void foo(T)(T t)
  if (isIntegral!T)
  {
  }

- template parameter specialization [3]

  void foo(T : ulong)(T t) // : meaning implictly convert to
  {
  }


2 and 3 being the more commonly used.
1 is more to use the same body instead of using N overloads

[1] : https://dlang.org/spec/version.html#staticif
[2] : https://dlang.org/spec/template.html#template_constraints
[3] : 
https://dlang.org/spec/template.html#parameters_specialization


Re: How define accepted types in a template parameter?

2021-01-16 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Saturday, 16 January 2021 at 18:39:03 UTC, Marcone wrote:
For example, I want my function template to only accept integer 
or string;


There are different ways of doing that. I'd say this one is easy 
to follow:


import std.stdio;

void print(T)(T entry) if(is(T==string) || is(T==int))
{
writeln(entry);

}

void main(){
int i = 5;
string foo = "foo";

double j = 0.6;

print(i);
print(foo);
print(j); // compilation error
}


How define accepted types in a template parameter?

2021-01-16 Thread Marcone via Digitalmars-d-learn
For example, I want my function template to only accept integer 
or string;


Re: How to specify an exact template?

2021-01-16 Thread Tove via Digitalmars-d-learn
On Saturday, 16 January 2021 at 15:41:38 UTC, solidstate1991 
wrote:

On Saturday, 16 January 2021 at 14:18:55 UTC, Tove wrote:
probably you can use 
https://dlang.org/spec/traits.html#getOverloads


I don't know how to use it with functions outside of 
structs/classes.


void foo() {}
void foo(string i) {}

static foreach(overload; __traits(getOverloads, 
mixin(__MODULE__), "foo"))

pragma(msg, typeof(overload));


Re: How to specify an exact template?

2021-01-16 Thread solidstate1991 via Digitalmars-d-learn

On Saturday, 16 January 2021 at 14:18:55 UTC, Tove wrote:
probably you can use 
https://dlang.org/spec/traits.html#getOverloads


I don't know how to use it with functions outside of 
structs/classes.


Re: reference counting resources but NOT memory

2021-01-16 Thread rikki cattermole via Digitalmars-d-learn

What you are describing sounds like regular reference counting.

All resources (i.e. windows) are pinned somewhere in memory. Its just 
that you have to use special API's to destroy them rather than free.


Re: How to specify an exact template?

2021-01-16 Thread Tove via Digitalmars-d-learn
On Saturday, 16 January 2021 at 14:14:57 UTC, solidstate1991 
wrote:
On Saturday, 16 January 2021 at 14:13:29 UTC, solidstate1991 
wrote:

Here's the following line, among many others:

return !(ubyte);

This generates an error, as this function template matches two 
instances, but currently I don't know how to choose the one I 
need, other than write a local function, that looks a bit ugly.


Forgot to link the code example: 
https://github.com/ZILtoid1991/pixelperfectengine/blob/master/pixeperfectengine/src/PixelPerfectEngine/graphics/layers/base.d#L143


probably you can use 
https://dlang.org/spec/traits.html#getOverloads




Re: How to specify an exact template?

2021-01-16 Thread solidstate1991 via Digitalmars-d-learn
On Saturday, 16 January 2021 at 14:13:29 UTC, solidstate1991 
wrote:

Here's the following line, among many others:

return !(ubyte);

This generates an error, as this function template matches two 
instances, but currently I don't know how to choose the one I 
need, other than write a local function, that looks a bit ugly.


Forgot to link the code example: 
https://github.com/ZILtoid1991/pixelperfectengine/blob/master/pixeperfectengine/src/PixelPerfectEngine/graphics/layers/base.d#L143


How to specify an exact template?

2021-01-16 Thread solidstate1991 via Digitalmars-d-learn

Here's the following line, among many others:

return !(ubyte);

This generates an error, as this function template matches two 
instances, but currently I don't know how to choose the one I 
need, other than write a local function, that looks a bit ugly.


reference counting resources but NOT memory

2021-01-16 Thread Tove via Digitalmars-d-learn
Is there any elegant/smart solution to reference counting 
resources without ever freeing any objects?


When the ref hits 0 it should free some other resource that isn't 
memory...


Resource[10] resources;

resources[3].inc; // 1 ref - 0->1 transition enable  some feature
resources[3].dec; // 0 ref - 1->0 transition disable some feature
resources[3].inc; // 1 ref - instantly ready to be used again 
without any alloc/free


Maybe it's best to use normal constructor/destructor with some 
arena memory allocator that never actually frees anything? Must 
be a common problem, anyone got experience with a good design?