Re: How to repeat structure C++

2017-01-15 Thread MGW via Digitalmars-d-learn

On Monday, 16 January 2017 at 00:07:44 UTC, Adam D. Ruppe wrote:
That's just interfaces and classes in D, so change struct to 
those words and go fro there.


// --- jd.d:  compile: dmd -c jd
import std.stdio;
extern (C++) interface IHome { int sum(int, int); };
extern (C++) void printIHome(void* ptr) {
IHome  ob = cast(jd.IHome)ptr;
ob.sum(4, 5);
}

// --- jc.cpp  compile: dmc jc.cpp jd.obj phobos.lib
#include 

struct IHome {
int sum(int a, int b) {
int s = a+b; printf("a = %d, b = %d, sum = %d\n", a, b, s);
return s;
}
};

extern "C" intrt_init();
extern "C" intrt_term();

extern void printIHome(void*);

void main() {
IHome iMyHome;
rt_init();
// ---
iMyHome.sum(2, 3);   // Call C++ method in C++
IHome* ptriMyHome = 
printIHome(ptriMyHome);// Call C++ method in D --> ERROR :-(
// ---
rt_term();
}
//  compile --
dmd -c jd
dmc jc.cpp jd.obj r:\dmd2\windows\lib\phobos.lib

// ---
Please, help to rewrite jd.d for the correct method call in 
structure С++.




Re: Quine using strings?

2017-01-15 Thread Basile B. via Digitalmars-d-learn

On Sunday, 15 January 2017 at 19:43:22 UTC, Nestor wrote:
I was reading some of the examples of writing a quine with D, 
but apparently the language has evolved and they no longer 
compiled unchanged.


So I tried to program one by myself using strings and 
std.stdio, but the result seems long and redundant:


import std.stdio;void main(){string s=`import std.stdio;void 
main(){string 
s=writefln("%s\x60%s\x60;s",s[0..38],s,s[38..$]);}`;writefln("%s\x60%s\x60;%s",s[0..38],s,s[38..$]);}


Any ideas for a shorter version (preferably without using 
pointers)?


I remember on Rosetta to have seen this:

module quine;
import std.stdio;
void main(string[] args)
{
write(import("quine.d"));
}

compiles with: dmd path/quine.d -Jpath


Re: Quine using strings?

2017-01-15 Thread Michael Coulombe via Digitalmars-d-learn

A quine I came up with a while ago, using q{} string notation:

enum s = q{enum s = q{%s};
void main() {
import std.stdio;
writefln(s,s);
}};
void main() {
import std.stdio;
writefln(s,s);
}


Re: Is it ok to inherit multiple times same templated interface?

2017-01-15 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 15 January 2017 at 20:33:30 UTC, Alexandru Ermicioi 
wrote:
Currently doing so is allowed, though, it is impossible to call 
implemented methods directly from implementation.


You should be able to do obj.Wr!(ubyte).get() too.


Re: How to repeat structure C++

2017-01-15 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 15 January 2017 at 19:05:06 UTC, MGW wrote:

struct IInterface {};
struct IMsgBox : public IInterface {
	virtual bool Confirm(const wchar* queryText, tVariant* retVal) 
= 0;

virtual bool Alert(const wchar* text) = 0;
};


That's just interfaces and classes in D, so change struct to 
those words and go fro there.


Re: Accessing a function within an object's superclass from the outside

2017-01-15 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 15 January 2017 at 02:32:29 UTC, Meta wrote:

Is this documented anywhere? I had no idea this was a feature.


Used in some examples here:
http://dlang.org/spec/class.html


Re: Quine using strings?

2017-01-15 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 15 January 2017 at 22:35:26 UTC, Nestor wrote:

You forgot to include the program... or is this a joke? ;)


Neither: the empty program compiles and runs, outputting nothing. 
Since its empty output matches its empty source file, it 
technically fits the definition of the quine :)




Re: Is it ok to inherit multiple times same templated interface?

2017-01-15 Thread Ryan via Digitalmars-d-learn
On Sunday, 15 January 2017 at 20:33:30 UTC, Alexandru Ermicioi 
wrote:

Good day,

Given following code example, where a templated interface Wr, 
and an implementation Im is present:

interface Wr(T) {
T get();
}

class Im(T : ubyte) : Wr!ubyte, Wr!ushort, Wr!string {
public T t;

ubyte get() {
return cast(ubyte) this.t;
}

ushort get() {
return cast(ushort) this.t;
}

string get() {
import std.conv;
return this.t.to!string ~ " with testings";
}
}

void main() {
auto i = new Im!ubyte;
i.t = 20;

assert((cast(Wr!ubyte) i).get == 20);
assert((cast(Wr!ushort) i).get == 20);
assert((cast(Wr!string) i).get == "20 with testings");
}

Is it ok (not undefined behavior), to have Im implementing 
multiple times interface Wr, with different template arguments?

Or doing so, will eventually lead to subtle bugs?

Currently doing so is allowed, though, it is impossible to call 
implemented methods directly from implementation.
Only by casting i to different implemented interfaces 
(Wr!ubyte, Wr!ushort, and Wr!string), is possible to call each 
implemented get method.


Thanks.


How would overloading work?

Overload resolution works based on function/method parameters, 
not return types. In the example you gave the 3 get functions are 
indistinguishable. If the template parameter was used for a 
method parameter type, then they would be distinguishable.


See overloading functions here [0]. I think yours only works with 
the cast because function parameters, including the _this_ 
pointer is taken into account.


[0] https://dlang.org/spec/function.html#function-overloading



Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

Thank you all.


Re: Quine using strings?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 22:08:47 UTC, pineapple wrote:

On Sunday, 15 January 2017 at 21:37:53 UTC, Nestor wrote:
Any ideas for a shorter version (preferably without using 
pointers)?


When compiling with the -main flag, this D program is a quine:


You forgot to include the program... or is this a joke? ;)


Re: Quine using strings?

2017-01-15 Thread pineapple via Digitalmars-d-learn

On Sunday, 15 January 2017 at 21:37:53 UTC, Nestor wrote:
Any ideas for a shorter version (preferably without using 
pointers)?


When compiling with the -main flag, this D program is a quine:


Re: Is it ok to inherit multiple times same templated interface?

2017-01-15 Thread Dmitry Olshansky via Digitalmars-d-learn
On Sunday, 15 January 2017 at 20:33:30 UTC, Alexandru Ermicioi 
wrote:

Good day,

Given following code example, where a templated interface Wr, 
and an implementation Im is present:



From the standpoint of the compiler they are 3 distinct 
interfaces, so all is good.



interface Wr(T) {
T get();
}

[...]




Re: Convert duration to years?

2017-01-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 15, 2017 03:43:32 Nestor via Digitalmars-d-learn wrote:
> Hi,
>
> I would simply like to get someone's age, but I am a little lost
> with time and date functions. I can already get the duration, but
> after reading the documentation it's unclear to me how to convert
> that into years. See following code:
>
> import std.stdio;
>
> void getAge(int , int mm, int dd) {
>import std.datetime;
>SysTime t1 = SysTime(Date(, mm, dd));
>SysTime t2 = Clock.currTime();
>writeln(t2 - t1);
> }
>
> int main() {
>try
>  getAge(1980, 1, 1);
>catch(Exception e) {
>  writefln("%s.\n(%s, line %s)", e.msg, e.file, e.line);
>}
> }
>
> Notice getAge should return ubyte instead of void, only I haven't
> been able to find how to do it. Any suggestion would be welcome.
>
> Thanks in advance.

Well, there's diffMonths:

http://dlang.org/phobos/std_datetime.html#.SysTime.diffMonths

However, I doubt that it really does quite what you want. Because of the
varying lengths of months and years, you're probably going to have to write
code that does what you want with some combination of function. You probably
want to do something like

void getAge(int , int mm, int dd)
{
auto birthdate = Date(, mm, dd);
auto currDate = cast(Date)Clock.currTime;
// This make Feb 29th become March 1st
auto birthdayThisYear = Date(currDate.year, mm, 1) + days(dd - 1);
auto years = currDate.year - birthdate.year;
if(currDate < birthdayThisYear)
--years;
writeln(years);
}

I _think_ that that does it, but I'd want to do something like

void printAge(int , int mm, int dd)
{
writeln(getAge(cast(Date)Clock.currTime(), , mm, dd);
}

int getAge(Date currDate, int , int mm, int dd)
{
auto birthdate = Date(, mm, dd);
auto currDate = cast(Date)Clock.currTime;
// This make Feb 29th become March 1st
auto birthdayThisYear = Date(currDate.year, mm, 1) + days(dd - 1);
auto years = currDate.year - birthdate.year;
if(currDate < birthdayThisYear)
--years;
return years;
}

and then add unit tests for getAge to verify that it did the correct thing
for various dates. It's quite possible that there's something subtley wrong
with it. Also, depending on what exactly you're trying to do, it's possible
that I didn't quite understand what you're trying to do and that it needs
some additional tweaks in order to do what you want.

- Jonathan M Davis



Re: Querying parameter passing semantics for `auto ref const` variables

2017-01-15 Thread Nordlöw via Digitalmars-d-learn

On Sunday, 15 January 2017 at 17:41:36 UTC, Nordlöw wrote:

This

struct S { int x, y; }
void f()(auto ref const S s)
{
pragma(msg, "type:", typeof(s), " isRef:", isRef!s);
}
f(S.init);
S s;
f(s);

prints

type:const(S) isRef:false
type:const(S) isRef:true


given that

enum isRef(alias fn) = __traits(isRef, fn);

:)


Re: Parsing a UTF-16LE file line by line, BUG?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 16:29:23 UTC, Daniel Kozák wrote:
This is because byLine does return range, so until you do 
something with that it does not cause any harm :)


I see. So correcting my original doubt:

How could I parse an UTF16LE file line by line (producing a 
proper string in each iteration) without loading the entire file 
into memory?


Quine using strings?

2017-01-15 Thread Nestor via Digitalmars-d-learn
I was reading some of the examples of writing a quine with D, but 
apparently the language has evolved and they no longer compiled 
unchanged.


So I tried to program one by myself using strings and std.stdio, 
but the result seems long and redundant:


import std.stdio;void main(){string s=`import std.stdio;void 
main(){string 
s=writefln("%s\x60%s\x60;s",s[0..38],s,s[38..$]);}`;writefln("%s\x60%s\x60;%s",s[0..38],s,s[38..$]);}


Any ideas for a shorter version (preferably without using 
pointers)?


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 16:57:35 UTC, biozic wrote:

On Sunday, 15 January 2017 at 14:20:04 UTC, Nestor wrote:
On second thought, if a baby was born in march 1 of 1999 
(non-leap year), in march 1 of 2000 (leap year) the age would 
have been one year plus one day (because of february 29).


No. A baby born on March 1st 1999 is just "one year old" on 
March 1st 2000, as it also is on March 2nd or any day after 
during the same year.




Perhaps I didn't make myself clear. I was not refering here to 
age in the conventional sense, but to the actual aging process. 
In other words, in this particular case the amount of days 
elapsed would have been 366 instead of 365.


Re: How to repeat structure C++

2017-01-15 Thread MGW via Digitalmars-d-learn

On Sunday, 15 January 2017 at 19:00:49 UTC, MGW wrote:

Hi!


struct IInterface {};
struct IMsgBox : public IInterface {
	virtual bool Confirm(const wchar* queryText, tVariant* retVal) = 
0;

virtual bool Alert(const wchar* text) = 0;
};



How to repeat structure C++

2017-01-15 Thread MGW via Digitalmars-d-learn

Hi!

I write plugin for 1C:Enterprise 8.3 on dmd now.

https://youtu.be/apLppufZulI

I try to repeat structure C++ (interface) on dmd.
But D no inheritance of structures. What it is possible
to replace with the D following code on C ++struct IInterface {};

C++
-
struct IMsgBox : public IInterface {
	virtual bool Confirm(const wchar* queryText, tVariant* retVal) = 
0;

virtual bool Alert(const wchar* text) = 0;
};



Re: Querying parameter passing semantics for `auto ref const` variables

2017-01-15 Thread Nordlöw via Digitalmars-d-learn

On Sunday, 15 January 2017 at 17:00:41 UTC, kinke wrote:

On Sunday, 15 January 2017 at 14:33:25 UTC, Nordlöw wrote:
A call to `isRef!T` inside the function `f` is always `false` 
for `l-value` and `r-value` passing.


According to 
https://dlang.org/spec/template.html#auto-ref-parameters, it 
should be `__traits(isRef, x)`.


This

struct S { int x, y; }
void f()(auto ref const S s)
{
pragma(msg, "type:", typeof(s), " isRef:", isRef!s);
}
f(S.init);
S s;
f(s);

prints

type:const(S) isRef:false
type:const(S) isRef:true

I made the mistake of incorrectly using

isRef!S

when I should have used

isRef!s

Thanks!


Re: Using Dub

2017-01-15 Thread Daniel N via Digitalmars-d-learn

On Sunday, 15 January 2017 at 13:23:25 UTC, Russel Winder wrote:
Is there any way of setting dub to default to ldc2 rather than 
dmd as the compiler of use? (I do not want to have to put 
--compiler ldc2 on every dub command.)


I have never used dub, but I know it's now also bundled with ldc2.

I would assume that if your PATH to ldc2 comes before that of 
dmd, it would find the ldc2 bundled version of dub and it would 
do the smart thing(if not, that's a bug).




Re: Querying parameter passing semantics for `auto ref const` variables

2017-01-15 Thread kinke via Digitalmars-d-learn

On Sunday, 15 January 2017 at 14:33:25 UTC, Nordlöw wrote:
A call to `isRef!T` inside the function `f` is always `false` 
for `l-value` and `r-value` passing.


According to 
https://dlang.org/spec/template.html#auto-ref-parameters, it 
should be `__traits(isRef, x)`.


Re: Convert duration to years?

2017-01-15 Thread biozic via Digitalmars-d-learn

On Sunday, 15 January 2017 at 14:20:04 UTC, Nestor wrote:

On Sunday, 15 January 2017 at 14:04:39 UTC, Nestor wrote:

...
For example, take a baby born in february 29 of year 2000 
(leap year). In february 28 of 2001 that baby was one day 
short to one year.


Family can make a concession and celebrate birthdays in 
february 28 of non-leap years, but march 1 is the actual day 
when the year of life completes. Which one to choose?




On second thought, if a baby was born in march 1 of 1999 
(non-leap year), in march 1 of 2000 (leap year) the age would 
have been one year plus one day (because of february 29).


No. A baby born on March 1st 1999 is just "one year old" on March 
1st 2000, as it also is on March 2nd or any day after during the 
same year.


So perhaps the best thing is to always perform a "relaxed" 
calculation.


I guess the problem of people born on February 29th is really 
application-dependent, and it also depends on the use of the 
calculated age. A social web app: users probably would like to 
see their age change on the 28th of non-leap years. A 
regulation-aware software: just follow what the law says. Etc.






Re: Parsing a UTF-16LE file line by line, BUG?

2017-01-15 Thread Daniel Kozák via Digitalmars-d-learn
V Sun, 15 Jan 2017 14:48:12 +
Nestor via Digitalmars-d-learn  napsáno:

> On Friday, 6 January 2017 at 11:42:17 UTC, Mike Wey wrote:
> > On 01/06/2017 11:33 AM, pineapple wrote:  
> >> On Friday, 6 January 2017 at 06:24:12 UTC, rumbu wrote:  
> 
>  I'm not sure if this works quite as intended, but I was at 
>  least able
>  to produce a UTF-16 decode error rather than a UTF-8 decode 
>  error by
>  setting the file orientation before reading it.
> 
>  import std.stdio;
>  import core.stdc.wchar_ : fwide;
>  void main(){
>  auto file = File("UTF-16LE encoded file.txt");
>  fwide(file.getFP(), 1);
>  foreach(line; file.byLine){
>  writeln(file.readln);
>  }
>  }  
> >>>
> >>> fwide is not implemented in Windows:
> >>> https://msdn.microsoft.com/en-us/library/aa985619.aspx  
> >>
> >> That's odd. It was on Windows 7 64-bit that I put together and 
> >> tested
> >> that example, and calling fwide definitely had an effect on 
> >> program
> >> behavior.  
> >
> > Are you compiling a 32bit binary? Because in that case you 
> > would be using the digital mars c runtime which might have an 
> > implementation for fwide.  
> 
> After some testing I realized that byLine was not the one 
> failing, but any string manipulation done to the obtained line. 
> Compile the following example with and without -debug and run to 
> see what I mean:
> 
> import std.stdio, std.string;
> 
> enum
>EXIT_SUCCESS = 0,
>EXIT_FAILURE = 1;
> 
> int main() {
>version(Windows) {
>  import core.sys.windows.wincon;
>  SetConsoleOutputCP(65001);
>}
>auto f = File("utf16le.txt", "r");
>foreach (line; f.byLine()) try {
>  string s;
>  debug s = cast(string)strip(line); // this is the one causing 
> problems
>  if (1 > s.length) continue;
>  writeln(s);
>} catch(Exception e) {
>  writefln("Error. %s\nFile \"%s\", line %s.", e.msg, e.file, 
> e.line);
>  return EXIT_FAILURE;
>}
>return EXIT_SUCCESS;
> }

This is because byLine does return range, so until you do something with that
it does not cause any harm :)



Re: Parsing a UTF-16LE file line by line, BUG?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 14:48:12 UTC, Nestor wrote:
After some testing I realized that byLine was not the one 
failing, but any string manipulation done to the obtained line. 
Compile the following example with and without -debug and run 
to see what I mean:


import std.stdio, std.string;

enum
  EXIT_SUCCESS = 0,
  EXIT_FAILURE = 1;

int main() {
  version(Windows) {
import core.sys.windows.wincon;
SetConsoleOutputCP(65001);
  }
  auto f = File("utf16le.txt", "r");
  foreach (line; f.byLine()) try {
string s;
debug s = cast(string)strip(line); // this is the one 
causing problems

if (1 > s.length) continue;
writeln(s);
  } catch(Exception e) {
writefln("Error. %s\nFile \"%s\", line %s.", e.msg, e.file, 
e.line);

return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


By the way, when caught, the exception says it's in file 
src/phobos/std/utf.d line 1217, but that file only has 784 lines. 
That's quite odd.


(I am compiling with dmd 2.072.2)


Re: Parsing a UTF-16LE file line by line, BUG?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Friday, 6 January 2017 at 11:42:17 UTC, Mike Wey wrote:

On 01/06/2017 11:33 AM, pineapple wrote:

On Friday, 6 January 2017 at 06:24:12 UTC, rumbu wrote:


I'm not sure if this works quite as intended, but I was at 
least able
to produce a UTF-16 decode error rather than a UTF-8 decode 
error by

setting the file orientation before reading it.

import std.stdio;
import core.stdc.wchar_ : fwide;
void main(){
auto file = File("UTF-16LE encoded file.txt");
fwide(file.getFP(), 1);
foreach(line; file.byLine){
writeln(file.readln);
}
}


fwide is not implemented in Windows:
https://msdn.microsoft.com/en-us/library/aa985619.aspx


That's odd. It was on Windows 7 64-bit that I put together and 
tested
that example, and calling fwide definitely had an effect on 
program

behavior.


Are you compiling a 32bit binary? Because in that case you 
would be using the digital mars c runtime which might have an 
implementation for fwide.


After some testing I realized that byLine was not the one 
failing, but any string manipulation done to the obtained line. 
Compile the following example with and without -debug and run to 
see what I mean:


import std.stdio, std.string;

enum
  EXIT_SUCCESS = 0,
  EXIT_FAILURE = 1;

int main() {
  version(Windows) {
import core.sys.windows.wincon;
SetConsoleOutputCP(65001);
  }
  auto f = File("utf16le.txt", "r");
  foreach (line; f.byLine()) try {
string s;
debug s = cast(string)strip(line); // this is the one causing 
problems

if (1 > s.length) continue;
writeln(s);
  } catch(Exception e) {
writefln("Error. %s\nFile \"%s\", line %s.", e.msg, e.file, 
e.line);

return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


Re: Querying parameter passing semantics for `auto ref const` variables

2017-01-15 Thread Nordlöw via Digitalmars-d-learn

On Sunday, 15 January 2017 at 14:33:25 UTC, Nordlöw wrote:

Is there a way to query at compile-time whether a call to


Further, overloading such as

struct S { int x, y; }
static f(in S s) {}
static f(const ref S s) {}
f(S.init);
S s;
f(s);

fails as

declaration f is already defined


Querying parameter passing semantics for `auto ref const` variables

2017-01-15 Thread Nordlöw via Digitalmars-d-learn

Is there a way to query at compile-time whether a call to

f(T)(auto ref const T x)

passed the variable `x` from a l-value or r-value?

A call to `isRef!T` inside the function `f` is always `false` for 
`l-value` and `r-value` passing.


I need this to detect automatic delayed evaluation of 
sub-expressions in my GNU MP wrapper at


https://github.com/nordlow/gmp-d


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 14:04:39 UTC, Nestor wrote:

...
For example, take a baby born in february 29 of year 2000 (leap 
year). In february 28 of 2001 that baby was one day short to 
one year.


Family can make a concession and celebrate birthdays in 
february 28 of non-leap years, but march 1 is the actual day 
when the year of life completes. Which one to choose?




On second thought, if a baby was born in march 1 of 1999 
(non-leap year), in march 1 of 2000 (leap year) the age would 
have been one year plus one day (because of february 29). So 
perhaps the best thing is to always perform a "relaxed" 
calculation.





Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

On Sunday, 15 January 2017 at 11:01:28 UTC, biozic wrote:

On Sunday, 15 January 2017 at 08:40:37 UTC, Nestor wrote:
I cleaned up the function a little, but it still feels like a 
hack:


uint getAge(uint , uint mm, uint dd) {
  import std.datetime;
  SysTime t = Clock.currTime;
  ubyte correction = 0;
  if(
(t.month < mm) ||
( (t.month == mm) && (t.day < dd) )
  ) correction += 1;
  return (t.year -  - correction);
}

Isn't there anything better?


It doesn't feel like a hack to me, because it's simple and 
correct code that comply with the common definition of a 
person's age. The only inaccuracy I can think of is about 
people born on February 29th...


I know. I thought about it as well, but it's not something you 
can deal with cleanly.


For example, take a baby born in february 29 of year 2000 (leap 
year). In february 28 of 2001 that baby was one day short to one 
year.


Family can make a concession and celebrate birthdays in february 
28 of non-leap years, but march 1 is the actual day when the year 
of life completes. Which one to choose?


Another way to deal with this is modifying the function to take a 
parameter which allows to do a relaxed calculation in non-leap 
years if one so desires.


Re: std.container.array.Array is not @nogc?

2017-01-15 Thread Jack Stouffer via Digitalmars-d-learn

On Sunday, 15 January 2017 at 13:08:52 UTC, drug007 wrote:

Thanks for answer. Looking forward for your PR.


https://github.com/dlang/phobos/pull/5036


Using Dub

2017-01-15 Thread Russel Winder via Digitalmars-d-learn
Is there any way of setting dub to default to ldc2 rather than dmd as
the compiler of use? (I do not want to have to put --compiler ldc2 on
every dub command.)

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc
Description: This is a digitally signed message part


Re: std.container.array.Array is not @nogc?

2017-01-15 Thread drug007 via Digitalmars-d-learn

On 15.01.2017 15:49, Jack Stouffer wrote:


No you're not.

Array was designed before the @nogc attribute was created, so it wasn't
coded with it's requirements in mind. Looking at the code, Array
allocates GC memory for exception throwing in some cases. These can and
should be changed to asserts.

I am writing a PR now to fix this. There doesn't seem to be too many
cases to fix.

Thanks for answer. Looking forward for your PR.


Re: std.container.array.Array is not @nogc?

2017-01-15 Thread Jack Stouffer via Digitalmars-d-learn

On Sunday, 15 January 2017 at 11:47:06 UTC, drug007 wrote:

Is there a way to use Array in @nogc code:
```
import std.container.array : Array;

@nogc:
void main(string[ ] args)
{
   Array!int ai;
   ai ~= 1;
   assert(ai[0] == 1);
}
```
fails:
```
main.d(8): Error: @nogc function 'D main' cannot call non-@nogc 
function 'std.container.array.Array!int.Array.opOpAssign!("~", 
int).opOpAssign'



main.d(9): Error: @nogc function 'D main' cannot call non-@nogc 
function 'std.container.array.Array!int.Array.opIndex'

```
am I doing something wrong?


No you're not.

Array was designed before the @nogc attribute was created, so it 
wasn't coded with it's requirements in mind. Looking at the code, 
Array allocates GC memory for exception throwing in some cases. 
These can and should be changed to asserts.


I am writing a PR now to fix this. There doesn't seem to be too 
many cases to fix.


std.container.array.Array is not @nogc?

2017-01-15 Thread drug007 via Digitalmars-d-learn

Is there a way to use Array in @nogc code:
```
import std.container.array : Array;

@nogc:
void main(string[ ] args)
{
   Array!int ai;
   ai ~= 1;
   assert(ai[0] == 1);
}
```
fails:
```
main.d(8): Error: @nogc function 'D main' cannot call non-@nogc function 
'std.container.array.Array!int.Array.opOpAssign!("~", int).opOpAssign' 



main.d(9): Error: @nogc function 'D main' cannot call non-@nogc function 
'std.container.array.Array!int.Array.opIndex'

```
am I doing something wrong?


Re: Convert duration to years?

2017-01-15 Thread biozic via Digitalmars-d-learn

On Sunday, 15 January 2017 at 08:40:37 UTC, Nestor wrote:
I cleaned up the function a little, but it still feels like a 
hack:


uint getAge(uint , uint mm, uint dd) {
  import std.datetime;
  SysTime t = Clock.currTime;
  ubyte correction = 0;
  if(
(t.month < mm) ||
( (t.month == mm) && (t.day < dd) )
  ) correction += 1;
  return (t.year -  - correction);
}

Isn't there anything better?


It doesn't feel like a hack to me, because it's simple and 
correct code that comply with the common definition of a person's 
age. The only inaccuracy I can think of is about people born on 
February 29th...




Re: Convert duration to years?

2017-01-15 Thread ag0aep6g via Digitalmars-d-learn

On 01/15/2017 07:58 AM, Nestor wrote:

I eventually came up with this, but it seems an ugly hack:

import std.stdio;

uint getAge(int , ubyte mm, ubyte dd) {
  ubyte correction;
  import std.datetime;
  SysTime t = Clock.currTime();
  if (t.month < mm) correction = 1;
  else if (t.month == mm) correction = (t.day < dd) ? 1 : 0;
  else correction = 0;
  return (t.year -  - correction);
}

void main() {
  try
writefln("Edad: %s años.", getAge(1958, 1, 21));
  catch(Exception e) {
writefln("%s.\n(%s, line %s)", e.msg, e.file, e.line);
  }
}


That's the better approach, I think. Years have variable lengths. 
Determining "age" in years works by comparing dates, not durations.


I would write it like this, but as far as I see yours does the same thing:


int getAge(int , int mm, int dd)
{
import std.datetime;

immutable SysTime now = Clock.currTime();
immutable int years = now.year - ;

return mm > now.month || mm == now.month && dd > now.day
? years - 1 // birthday hasn't come yet this year
: years; // birthday has already been this year
}

void main()
{
import std.stdio;

/* Day of writing: 2017-01-15 */
writeln(getAge(1980, 1, 1)); /* 37 */
writeln(getAge(1980, 1, 15)); /* 37 (birthday is today) */
writeln(getAge(1980, 1, 30)); /* 36 */
writeln(getAge(1980, 6, 1)); /* 36 */
}



Isn't there a built-in function to do this?


If there is, finding it in std.datetime would take me longer than 
writing it myself.


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn
I cleaned up the function a little, but it still feels like a 
hack:


uint getAge(uint , uint mm, uint dd) {
  import std.datetime;
  SysTime t = Clock.currTime;
  ubyte correction = 0;
  if(
(t.month < mm) ||
( (t.month == mm) && (t.day < dd) )
  ) correction += 1;
  return (t.year -  - correction);
}

Isn't there anything better?


Re: template instance does not match template declaration

2017-01-15 Thread Fabrice Marie via Digitalmars-d-learn

On Sunday, 8 January 2017 at 05:45:52 UTC, Meta wrote:

On Sunday, 8 January 2017 at 03:27:26 UTC, Fabrice Marie wrote:


void main()
{
  
  Cache!(BasicObject, string, lookupBasicObject);
}


In addition to what Nicholas Wilson said, what you're doing 
here is the equivalent of writing `int;`. It doesn't make any 
sense as Cache!(...) is a type, and you are declaring a 
variable of that type... except you're not providing a variable 
name so the syntax is wrong. After you've fixed the other 
problem change it to this:


Cache!(BasicObject, string, lookupBasicObject) c;


Thanks a lot! exactly what I needed.


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn
On Sunday, 15 January 2017 at 07:25:26 UTC, rikki cattermole 
wrote:
So I had a go at this and found I struggled looking at "magic" 
functions and methods.

Turns out there is a much simpler answer.

int getAge(int , int mm, int dd) {
  import std.datetime;
  auto t1 = cast(DateTime)SysTime(Date(, mm, dd));
  auto t2 = cast(DateTime)Clock.currTime();

  int numYears;
  while(t2 > t1) {
 t1.add!"years"(1);
 numYears++;
  }

  return numYears;
}



Well... correct me if I am wrong, but isn't t1.add!"years"(1) 
simply adding one year to t1?