Compiler gets confused with ambiguity when `int` matches both `real` and `float`.

2017-12-22 Thread IM via Digitalmars-d-learn

The following expression:

import std.math : sqrt;
sqrt(400);

produces the following compiler error:

std.math.sqrt called with argument types (int) matches both:
/usr/include/dmd/phobos/std/math.d(1592,7): 
std.math.sqrt(float x)

and:
/usr/include/dmd/phobos/std/math.d(1598,6): 
std.math.sqrt(real x)


Shouldn't it just pick one according to some defined rules?





Re: Don't expect class destructors to be called at all by the GC

2017-12-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Friday, 22 December 2017 at 00:09:31 UTC, Mike Franklin wrote:


What condition(s) would cause a destructor for an object that 
is managed by the GC to potentially not be called?




Good question. It's true that barring an Error, they should be 
called by the GC at runtime termination.




Re: Don't expect class destructors to be called at all by the GC

2017-12-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 22 December 2017 at 23:34:55 UTC, Mengu wrote:

i really wonder how Objective-C and Swift is pulling this off.


It isn't a fundamental problem, D just can't express it in the 
existing language (heck, even D, as defined, could do it, the 
implementation just isn't there.)


Re: One liner for creating an array filled by a factory method

2017-12-22 Thread Mengu via Digitalmars-d-learn
On Thursday, 21 December 2017 at 21:11:58 UTC, Steven 
Schveighoffer wrote:

On 12/21/17 4:00 PM, kerdemdemir wrote:

I have a case like :

http://rextester.com/NFS28102

I have a factory method, I am creating some instances given 
some enums.

My question is about :


void PushIntoVector( BaseEnum[] baseEnumList )
{
     Base[] baseList;
     foreach ( tempEnum; baseEnumList )
     {
    baseList ~= Factory(tempEnum);
     }
}

I don't want to use "foreach" loop. Is there any cool std 
function that I can call ?


Something like baseEnumList.CoolStdFunc!( a=> Factory(a) 
)(baseList);




https://dlang.org/phobos/std_algorithm_iteration.html#map

-Steve


so basically it becomes:

Base[] baseList = baseEnumList.map!(el => Factory(el));

there's also a parallel version of map [0] if you ever need to 
map the list concurrently.


[0] https://dlang.org/phobos/std_parallelism.html#.TaskPool.map


Re: Don't expect class destructors to be called at all by the GC

2017-12-22 Thread Mengu via Digitalmars-d-learn
On Thursday, 21 December 2017 at 18:45:27 UTC, Adam D. Ruppe 
wrote:

On Thursday, 21 December 2017 at 18:20:19 UTC, H. S. Teoh wrote:
When the scoped destruction of structs isn't an option, 
RefCounted!T seems to be a less evil alternative than an 
unreliable class dtor. :-/


Alas, RefCounted doesn't work well with inheritance...

Though, what you could do is make the refcounted owners and 
borrow the actual reference later.


i really wonder how Objective-C and Swift is pulling this off.


Re: Converting member variables to strings with using reflection from base class

2017-12-22 Thread Mengu via Digitalmars-d-learn

On Friday, 22 December 2017 at 22:09:05 UTC, H. S. Teoh wrote:
On Fri, Dec 22, 2017 at 09:13:31PM +, kerdemdemir via 
Digitalmars-d-learn wrote:
I want to make a logging function for member variables by 
using reflection.

[...]

class B
{
void Log()
{
auto a = [__traits(derivedMembers, D)];
foreach(memberName; a) {
// Somehow write only member variables with their 
names

// Result should be : a = 4.0, b = 3.0


Try this:

import std.traits : FieldNameTuple;
foreach (memberName; FieldNameTuple!B) {
writefln("%s = %s", memberName, mixin("this." ~ memberName));
}


T


and then turn it into a LoggerMixin with a mixin template and 
re-use it any time you want.


import std.stdio : writeln, writefln;
import std.traits : FieldNameTuple;

mixin template LoggerMixin() {
  void Log() {
foreach (memberName; FieldNameTuple!(typeof(this))) {
  writefln("%s = %s", memberName, mixin("this." ~ 
memberName));

}

  }
}

struct S {
  int x;
  bool y;
  double z;

  mixin LoggerMixin;
}

void main() {

  S s1 = S(int.min, true, );
  S s2 = S(int.max, false, );
  s1.Log();
  s2.Log();
}



Re: alias to struct method

2017-12-22 Thread Mengu via Digitalmars-d-learn

On Friday, 22 December 2017 at 17:53:34 UTC, Marc wrote:

How can I create a alias to a struct method?


struct S {
 string doSomething(int n) { return ""; }
}


I'd like to do something like this (imaginary code):

alias doSomething = S.doSomething;

then call it by doSomething(3)

I got the following error from this code:


Error: need 'this' for 'gen' of type 'string(int n)'


So I tried create a instance:


alias doSomething = S().doSomething;


Changes the error to:

app.d(96): Error: function declaration without return type. 
(Note that > constructors are always named this)

app.d(96): Error: semicolon expected to close alias declaration


it is also possible with getMember trait but why don't you just 
mark that method as static?


How to I get the dub package version that I want...

2017-12-22 Thread WhatMeWorry via Digitalmars-d-learn


I can compile the derelict-fmod example with
dub.json
   ...
"dependencies": {
"derelict-util": ">=1.9.1"
   ...
and dub.selections.json
...
"versions": {
"derelict-util": "2.1.0"
...
dub run
Fetching derelict-util 2.1.0 (getting selected version)...
Performing "debug" build using ldc2 for x86_64.
derelict-util 2.1.0: building configuration "library"...

==

In my project, I use SDL
dub.sdl
   ...
dependency "derelict-util"  version=">=1.9.1"
   ...
dub upgrade
Upgrading project in /home/generic/Delivery/apps/06_03_09_audio
Fetching derelict-util 2.0.6 (getting selected version)...

which is a higher version but no cigar.  So I try brute force with
dub.sdl
   ...
dependency "derelict-util"  version="==2.1.0"
   ...

but dub upgrade returns:
Upgrading project in /home/generic/Delivery/apps/06_03_09_audio
Root package 06_03_09_audio reference derelict-util 2.1.0 cannot 
be satisfied.


There is definitely a 2.1.0 derelict-util package with a time 
stamp of 2016-Sep-05


So what am I doing wrong?








Re: Converting member variables to strings with using reflection from base class

2017-12-22 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Dec 22, 2017 at 09:13:31PM +, kerdemdemir via Digitalmars-d-learn 
wrote:
> I want to make a logging function for member variables by using reflection.
[...]
> class B
> {
> void Log()
> {
> auto a = [__traits(derivedMembers, D)];
> foreach(memberName; a) {
> // Somehow write only member variables with their names
> // Result should be : a = 4.0, b = 3.0

Try this:

import std.traits : FieldNameTuple;
foreach (memberName; FieldNameTuple!B) {
writefln("%s = %s", memberName, mixin("this." ~ memberName));
}


T

-- 
People walk. Computers run.


Re: Parsing string to octal(for umask) at runtime?

2017-12-22 Thread Ryan David Sheasby via Digitalmars-d-learn
On Friday, 22 December 2017 at 21:36:20 UTC, Ryan David Sheasby 
wrote:
Hi. Struggling to figure this out. At the bottom of this page: 
https://dlang.org/library/std/conv/octal.html is a vague 
reference to using parse. However, when I use what I would 
assume to be correct based on this: 
https://dlang.org/phobos/std_conv.html#.parse.3 and the fact 
that in the octal page it says octal is also a enum, I see no 
reason this syntax shouldn't work:


import std.conv;

umaskVal = parse!octal(data.umask);

Yet I get an error saying the compiler cannot deduce which 
overload of parse to use... How do I do this parse? Am I going 
about this the wrong way? Is there a better way to parse a 
string as a normal base8 ushort?


Nevermind. I've just figured it out from this forum post :-) 
http://forum.dlang.org/thread/nbvdebjxodabukfbe...@forum.dlang.org


All I needed to do was:

umaskVal = parse!ushort(data.umask, 8);


Parsing string to octal(for umask) at runtime?

2017-12-22 Thread Ryan David Sheasby via Digitalmars-d-learn
Hi. Struggling to figure this out. At the bottom of this page: 
https://dlang.org/library/std/conv/octal.html is a vague 
reference to using parse. However, when I use what I would assume 
to be correct based on this: 
https://dlang.org/phobos/std_conv.html#.parse.3 and the fact that 
in the octal page it says octal is also a enum, I see no reason 
this syntax shouldn't work:


import std.conv;

umaskVal = parse!octal(data.umask);

Yet I get an error saying the compiler cannot deduce which 
overload of parse to use... How do I do this parse? Am I going 
about this the wrong way? Is there a better way to parse a string 
as a normal base8 ushort?


Converting member variables to strings with using reflection from base class

2017-12-22 Thread kerdemdemir via Digitalmars-d-learn
I want to make a logging function for member variables by using 
reflection.


import std.stdio;

class D : B
{
override void foo() {
a  = 4.0;
b  = 3.0;
}
double a;
double b;
}

class B
{
void Log()
{
auto a = [__traits(derivedMembers, D)];
foreach(memberName; a) {
// Somehow write only member variables with their 
names

// Result should be : a = 4.0, b = 3.0
}
}

void foo()
{
}
}

void main()
{
 auto b = new D;
 b.Log();
}

As I wrote in the comments I want to see member variable's name 
and its value.

What is the best way to achieve that?

Erdem


Re: alias to struct method

2017-12-22 Thread Ali Çehreli via Digitalmars-d-learn

On 12/22/2017 09:53 AM, Marc wrote:
> How can I create a alias to a struct method?
>
>> struct S {
>>  string doSomething(int n) { return ""; }
>> }
>
> I'd like to do something like this (imaginary code):
>
> alias doSomething = S.doSomething;
>
> then call it by doSomething(3)

That can't work because there is no S object to call doSomething on. One 
way is to use a delegate:


struct S {
string doSomething(int n) { return ""; }
}

void main() {
S s;
auto d = (int n) { s.doSomething(n); };
d(3);
}

Ali



Re: Does LDC support profiling at all?

2017-12-22 Thread Nathan S. via Digitalmars-d-learn

On Friday, 22 December 2017 at 09:52:26 UTC, Chris Katko wrote:
DMD can use -profile and -profile=gc. But I tried for HOURS to 
find the equivalent for LDC and came up with only 
profile-guided optimization--which I don't believe I want. Yet, 
if we can get PGO... where's the PROFILE itself it's using to 
make those decisions! :)


Thanks.


Is -fprofile-instr-generate= what you're 
looking for?


Re: GC in D and synadard library.

2017-12-22 Thread Kagamin via Digitalmars-d-learn
See also how dplug is implemented 
https://forum.dlang.org/post/hbmbztydvyfwemfne...@forum.dlang.org


alias to struct method

2017-12-22 Thread Marc via Digitalmars-d-learn

How can I create a alias to a struct method?


struct S {
 string doSomething(int n) { return ""; }
}


I'd like to do something like this (imaginary code):

alias doSomething = S.doSomething;

then call it by doSomething(3)

I got the following error from this code:


Error: need 'this' for 'gen' of type 'string(int n)'


So I tried create a instance:


alias doSomething = S().doSomething;


Changes the error to:

app.d(96): Error: function declaration without return type. 
(Note that > constructors are always named this)

app.d(96): Error: semicolon expected to close alias declaration


Re: Version Cygwin

2017-12-22 Thread Anonymouse via Digitalmars-d-learn
On Friday, 22 December 2017 at 03:24:15 UTC, rikki cattermole 
wrote:

You are not using a Cygwin build.
It doesn't matter who calls a process, it doesn't change the 
version's by itself.


As far as I know, nobody supports Cygwin like this.


I see, thank you.


Re: why ushort alias casted to int?

2017-12-22 Thread Patrick Schluter via Digitalmars-d-learn

On Friday, 22 December 2017 at 10:14:48 UTC, crimaniak wrote:

My code:

alias MemSize = ushort;

struct MemRegion
{
MemSize start;
MemSize length;
@property MemSize end() const { return start+length; }
}

Error: cannot implicitly convert expression 
`cast(int)this.start + cast(int)this.length` of type `int` to 
`ushort`


Both operands are the same type, so as I understand casting to 
longest type is not needed at all, and longest type here is 
ushort in any case. What am I doing wrong?


@property MemSize end() const { return cast(int)(start+length); }

The rule of int promotion of smaller types comes from C as they 
said. There are 2 reason to do it that way. int is supposed in C 
to be the natural arithmetic type of the CPU it runs on, i.e. the 
default size the processor has the least difficulties to handle. 
The second reason is that it allows to detect easily without much 
hassle if the result of the operation is in range or not. When 
doing arithmetic with small integer types, it is easy that the 
result overflows. It is not that easy to define portably this 
overflow behaviour. On some cpus it would require extra 
instructions. D has inherited this behaviour so that copied 
arithmetic code coming from C behaves in the same way.


@property MemSize end() const
{
  MemSize result = start+length;
  assert(result <= MemSize.max);
  return cast(int)result;
}

with overflow arithmetic this code is not possible (as is if 
MemSize was uint or ulong).


Re: std way to remove multiple indices from an array at once

2017-12-22 Thread aliak via Digitalmars-d-learn
On Thursday, 21 December 2017 at 15:59:44 UTC, Steven 
Schveighoffer wrote:

Here's a similar solution with an actual range:

https://run.dlang.io/is/gR3CjF

Note, all done lazily. However, the indices must be 
sorted/unique.


-Steve


Noice! :D


Re: why ushort alias casted to int?

2017-12-22 Thread Nathan S. via Digitalmars-d-learn

On Friday, 22 December 2017 at 10:42:28 UTC, crimaniak wrote:
Hm, really. ok, I will use the explicit cast, but I don't like 
it.


It's because the C programming language has similar integer 
promotion rules. That doesn't make it any more convenient if you 
weren't expecting it but that is the reason behind it.


Re: why ushort alias casted to int?

2017-12-22 Thread crimaniak via Digitalmars-d-learn

On Friday, 22 December 2017 at 10:18:52 UTC, ketmar wrote:

crimaniak wrote:

Both operands are the same type, so as I understand casting to 
longest type is not needed at all, and longest type here is 
ushort in any case. What am I doing wrong?


it is hidden in specs: all types shorter than int are promoted 
to int before doing any math.


Hm, really. ok, I will use the explicit cast, but I don't like it.


Re: why ushort alias casted to int?

2017-12-22 Thread ketmar via Digitalmars-d-learn

crimaniak wrote:

Both operands are the same type, so as I understand casting to longest 
type is not needed at all, and longest type here is ushort in any case. 
What am I doing wrong?


it is hidden in specs: all types shorter than int are promoted to int 
before doing any math.


why ushort alias casted to int?

2017-12-22 Thread crimaniak via Digitalmars-d-learn

My code:

alias MemSize = ushort;

struct MemRegion
{
MemSize start;
MemSize length;
@property MemSize end() const { return start+length; }
}

Error: cannot implicitly convert expression `cast(int)this.start 
+ cast(int)this.length` of type `int` to `ushort`


Both operands are the same type, so as I understand casting to 
longest type is not needed at all, and longest type here is 
ushort in any case. What am I doing wrong?


Does LDC support profiling at all?

2017-12-22 Thread Chris Katko via Digitalmars-d-learn
DMD can use -profile and -profile=gc. But I tried for HOURS to 
find the equivalent for LDC and came up with only profile-guided 
optimization--which I don't believe I want. Yet, if we can get 
PGO... where's the PROFILE itself it's using to make those 
decisions! :)


Thanks.