Re: How to deprecate member function from outside?

2018-12-22 Thread Dru via Digitalmars-d-learn

You're right it did define two functions
same signature and no compile error, heh





Re: How to deprecate member function from outside?

2018-12-22 Thread Neia Neutuladh via Digitalmars-d-learn
On Sat, 22 Dec 2018 21:33:14 +, Dru wrote:
> The first example shows that it is possible to deprecate a function
> separately from it's definition.

No, it doesn't. You declared two *different* functions. One was 
deprecated; the other wasn't.

> I want to know if it is possible to "fix" the second example? (deprecate
> a member function separately from it's definition)

No. All functions must be deprecated where you declare them.


Is InputRange intentionally more restrictive than isInputRange?

2018-12-22 Thread balddenimhero via Digitalmars-d-learn
I recently required a function to return an input range over some 
base element type E. I was under the impression that this is 
exactly what the interfaces in std.range.interface are for but 
found that these are more restrictive than the checks from 
std.range.primitives.


In particular, isInputRange(R) only requires R to implement 
front, empty and popFront, while InputRange!E requires moveFront 
and opApply in addition. To me this was unexpected and seems 
inconsistent, but maybe there's a good reason for that.


Can someone explain whether (and why) this is intentional.


Polymorphic hashOf

2018-12-22 Thread Per Nordlöw via Digitalmars-d-learn
If I want to include the class type (TypeInfo) inside a class 
instance hash, is the following snippet a good `@safe pure 
nothrow @nogc`-solution?:



import core.internal.hash : hashOf;

/** Hash that distinguishes `Expr(X)` from `NounExpr(X)`. */
hash_t hashOfPolymorphic(Class)(Class aClassInstance) @trusted 
pure nothrow @nogc

if (is(Class == class))
{
assert(Class.alignof == 8);
return (cast(hash_t)(cast(void*)typeid(Class)) >> 3) ^ 
hashOf(aClassInstance);

}

class Thing
{
}

class Expr : Thing
{
@safe pure nothrow @nogc:
alias Data = string;
this(Data data)
{
this.data = data;
}
@property override hash_t toHash() const @safe pure nothrow 
@nogc

{
return hashOf(data);
}
Data data;
}

class NounExpr : Expr
{
@safe pure nothrow @nogc:
this(Data data)
{
super(data);
}
@property override hash_t toHash() const @safe pure nothrow 
@nogc

{
return hashOf(data);
}
}

class Year : Thing
{
@safe pure nothrow @nogc:
alias Data = long;
@property override hash_t toHash() const @safe pure nothrow 
@nogc

{
return hashOf(data);
}
Data data;
}

@safe pure nothrow unittest
{
auto car1 = new Expr("car");
auto car2 = new Expr("car");
auto bar1 = new Expr("bar");
auto ncar = new NounExpr("car");

void testEqual() @safe pure nothrow @nogc
{
assert(hashOf(car1) == hashOf(car2));
assert(hashOfPolymorphic(car1) == 
hashOfPolymorphic(car2));

}

void testDifferent1() @safe pure nothrow @nogc
{
assert(hashOf(car1) != hashOf(bar1));
assert(hashOfPolymorphic(car1) != 
hashOfPolymorphic(bar1));

}

void testDifferent2() @safe pure nothrow @nogc
{
assert(hashOf(car1) == hashOf(ncar));
assert(hashOfPolymorphic(car1) != 
hashOfPolymorphic(ncar));

}

testEqual();
testDifferent1();
testDifferent2();
}



Re: How to deprecate member function from outside?

2018-12-22 Thread Dru via Digitalmars-d-learn

Thanks for the reply
I will try to explain what I mean:

The first example shows that it is possible to deprecate a 
function separately from it's definition.


The second example tries the same for a member function but it 
fails with a compile error.



I want to know if it is possible to "fix" the second example?
(deprecate a member function separately from it's definition)



Re: How to deprecate member function from outside?

2018-12-22 Thread Neia Neutuladh via Digitalmars-d-learn
On Sat, 22 Dec 2018 18:55:47 +, Dru wrote:
> I would like to use "deprecated" on a member function, but do it from a
> separate file
> 
> this works:
> ///
> void func() {}
> 
> deprecated {
>void func();
> }

You're defining two functions, presumably in two different modules and 
with two different fully qualified names. One function is deprecated but 
has no body, so you will get a deprecation warning and a linker error if 
you try using it. One function is not deprecated and has a body, so you'll 
get no errors if you try using it.

If, on the other hand, you define both the non-deprecated and deprecated 
functions in the same file, and you try using them, you'll get an error 
saying that the function call matches multiple functions.


How to deprecate member function from outside?

2018-12-22 Thread Dru via Digitalmars-d-learn

Hi,
I would like to use "deprecated" on a member function, but do it 
from a separate file


this works:
///
void func() {}

deprecated {
  void func();
}


this doesn't work:

class C{
  void func() {}
}

deprecated {
  void C.func();
}


I get: Error: semicolon expected, not .

Thanks


Re: Mixin operator 'if' directly

2018-12-22 Thread Basile B. via Digitalmars-d-learn

On Saturday, 22 December 2018 at 10:11:23 UTC, bauss wrote:

On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote:


Awesome hack!
Being a hack, it would be even nicer if it worked ouf of the 
box:


mixin template foo(bool b)
{
int _impl() { writeln(b); return int.init; }
int _ipml2 = _impl();
}

vs

mixin template foo(bool b)
{
writeln(b);
}


I think this is the closest we can come to it:

mixin template Execute(alias p)
{
auto __execute__()
{
p();

return 0;
}
auto __unused__ = __execute__();
}

mixin template MyTemplate(int a)
{
mixin Execute!({
import std.stdio : writeln;
writeln(a);
});
}

void main()
{
mixin MyTemplate!10;
}


One more line can be saved using a lambda to wrap the alias:

template ExpressionStatement(alias p)
{
const __unused__ = {p(); return 0;}();
}

Also i prefer the name `ExpressionStatement` to `Execute` because 
this describes exactly what the template does and define its 
limits at the same time.


And congrats to Neia Neutuladh for this find.


Re: Mixin operator 'if' directly

2018-12-22 Thread bauss via Digitalmars-d-learn

On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote:


Awesome hack!
Being a hack, it would be even nicer if it worked ouf of the 
box:


mixin template foo(bool b)
{
int _impl() { writeln(b); return int.init; }
int _ipml2 = _impl();
}

vs

mixin template foo(bool b)
{
writeln(b);
}


I think this is the closest we can come to it:

mixin template Execute(alias p)
{
auto __execute__()
{
p();

return 0;
}
auto __unused__ = __execute__();
}

mixin template MyTemplate(int a)
{
mixin Execute!({
import std.stdio : writeln;
writeln(a);
});
}

void main()
{
mixin MyTemplate!10;
}


Re: Some Fundamental Paradigm Questions

2018-12-22 Thread Ron Tarrant via Digitalmars-d-learn
Thanks, Dennis and H.S. for the well-thought-out and 
comprehensive replies.




Re: Mixin operator 'if' directly

2018-12-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote:
On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh 
wrote:

On Wed, 19 Dec 2018 15:12:14 +, bauss wrote:
Or while instantiating it:

mixin template foo()
{
  int _ignoreme()
  {
if (readln.strip == "abort") throw new AbortException;
return 1;
  }
  int _alsoIgnoreMe = _ignoreme();
}
void main()
{
  mixin foo;
}


Awesome hack!
Being a hack, it would be even nicer if it worked ouf of the 
box:


mixin template foo(bool b)
{
int _impl() { writeln(b); return int.init; }
int _ipml2 = _impl();
}

vs

mixin template foo(bool b)
{
writeln(b);
}


https://issues.dlang.org/show_bug.cgi?id=19506


Re: How to reuse the space of RAM of an array of strings?

2018-12-22 Thread Giovanni Di Maria via Digitalmars-d-learn
On Friday, 21 December 2018 at 22:31:26 UTC, Steven Schveighoffer 
wrote:

On 12/21/18 3:41 PM, Giovanni Di Maria wrote:

[...]


Note: alloca is a builtin intrinsic, so I wouldn't use that as 
a function name. Don't think it's affecting your program, but I 
wanted to point that out.



 [...]


This does NOT free the ram, it simply resets vec to null.

If you want to free the memory, use

GC.free(vec.ptr); vec = null;


 [...]


Note, this allocates a lot of smaller arrays on its way up to 
the really big array. You are better off doing:


vec.reserve(5_000_000);

which will pre-allocate the capacity needed. This will make you 
only allocate once.


-Steve








Hi Steve
Ok, thank you very much.
Ciao
Giovanni