Re: D vs perl6

2018-11-22 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 19 November 2018 at 06:46:55 UTC, dangbinghoo wrote:
So, can you experts give a more comprehensive compare with 
perl6 and D?


Sure!

1). You can actually read and understand D code.


Re: How do I install a library?

2018-11-09 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 9 November 2018 at 00:18:28 UTC, H. S. Teoh wrote:
It's not true that you're stuck with dub.  And I'm not among 
the people who think dub is the way to go (though it's true 
that that's a minority opinion around here).  Where I have a 
choice, my own D projects do not use dub.


Me neither, I think it's a lot of work for little benefit and 
doesn't seem all that easy to use either. I rather use rdmd using 
-I flags.


Re: Which Docker to use?

2018-10-17 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 03:37:21 UTC, Ky-Anh Huynh wrote:

Hi,

I need to build some static binaries with LDC. I also need to 
execute builds on both platform 32-bit and 64-bit.



From Docker Hub there are two image groups:

* language/ldc (last update 5 months ago)
* dlang2/ldc-ubuntu (updated recently)


Which one do you suggest?

Thanks a lot.


To be honest, you don't need docker for this. You can just 
download LDC in a self-contained folder and use it as is.


https://github.com/ldc-developers/ldc/releases

That's what I do on Linux.


Re: Please don't do a DConf 2018, consider alternatives

2018-10-02 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 2 October 2018 at 07:32:58 UTC, Joakim wrote:
Thank you for making clear that the real reason you and some 
others like the current format is because you want to have a 
fun "vacation"- as I pointed out in that earlier thread- rather 
than anything to do with D or advancing the ecosystem.


Yes, please let's not have any fun at Dconf this year!!! /s


Re: Warn on unused imports?

2018-09-25 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 25 September 2018 at 13:03:30 UTC, FeepingCreature 
wrote:
I'm playing with a branch of DMD that would warn on unused 
imports:


Honestly, I hate these types of warnings/errors. It makes playing 
with and designing code such a chore. I hope this is opt-in.


Re: Copy Constructor DIP and implementation

2018-09-24 Thread Gary Willoughby via Digitalmars-d-announce
On Sunday, 23 September 2018 at 02:40:15 UTC, Nicholas Wilson 
wrote:
It appears that @implicit has been removed from the 
implementation [1], but not yet from the DIP.


https://github.com/dlang/dmd/commit/cdd8100


Good, It's not needed.


Re: Converting a character to upper case in string

2018-09-21 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 21 September 2018 at 12:15:52 UTC, NX wrote:
How can I properly convert a character, say, first one to upper 
case in a unicode correct manner?
In which code level I should be working on? Grapheme? Or maybe 
code point is sufficient?


There are few phobos functions like asCapitalized() none of 
which are what I want.


Use `asCapitalized` to capitalize the first letter or use 
something like this:



import std.conv;
import std.range;
import std.stdio;
import std.uni;

void main(string[] args)
{
string input = "noe\u0308l";
int index= 2;

auto graphemes= input.byGrapheme.array;
string upperCased = [graphemes[index]].byCodePoint.text.toUpper;

graphemes[index] = upperCased.decodeGrapheme;
string output= graphemes.byCodePoint.text;

writeln(output);
}



Why do some attributes have an @ symbol and others don't?

2018-09-15 Thread Gary Willoughby via Digitalmars-d-learn

Why do some attributes have an @ symbol and others don't?

I thought it might be because some are used as keywords for other 
things but then 'pure' doesn't follow that rule. Any ideas? Is it 
just a legacy thing?


Re: Copy Constructor DIP and implementation

2018-09-12 Thread Gary Willoughby via Digitalmars-d-announce
On Wednesday, 12 September 2018 at 16:40:45 UTC, Jonathan M Davis 
wrote:
Ultimately, I expect that if we add any attribute for this, 
people coming to D are going to think that it's downright 
weird, but if we're going to have one, if we go with @implicit, 
we're future-proofing things a bit, and personally, thinking 
about it over time, I've found that it feels less like a hack 
than something like @copy would. If we had @copy, this would 
clearly forever be something that we added just because we has 
postblit constructors first, whereas @implicit at least _might_ 
be used for something more.


That does actually make a lot of sense. Isn't there any way these 
constructors could be inferred without the attribute?


Re: Copy Constructor DIP and implementation

2018-09-12 Thread Gary Willoughby via Digitalmars-d-announce

On Tuesday, 11 September 2018 at 15:08:33 UTC, RazvanN wrote:

Hello everyone,

I have finished writing the last details of the copy 
constructor DIP[1] and also I have published the first 
implementation [2]. As I wrongfully made a PR for the DIP queue 
in the early stages of the development of the DIP, I want to 
announce this way that the DIP is ready for the draft review 
now. Those who are familiar with the compiler, please take a 
look at the implementation and help me improve it!


Thanks,
RazvanN

[1] https://github.com/dlang/DIPs/pull/129
[2] https://github.com/dlang/dmd/pull/8688


I'm not sure about the naming of `@implicit` attribute. It seems 
confusing when used. `@copy` feels more natural or do we even 
need a new attribute at all?


Re: DIP 1017--Add Bottom Type--Community Review Round 1

2018-08-09 Thread Gary Willoughby via Digitalmars-d

On Thursday, 9 August 2018 at 13:42:57 UTC, bachmeier wrote:

On Thursday, 9 August 2018 at 03:02:55 UTC, Mike Parker wrote:
This is the feedback thread for the first round of Community 
Review for DIP 1017, "Add Bottom Type":


I hope there is a better name than Tbottom. A name like that is 
not consistent with the rest of the language. Why not Bottom?


`bottom_t` or `never_t` are much better but perhaps this should 
actually be an attribute `@bottom`, or more a documenting: 
`@noreturn`?


Re: Is there a way to anonymously execute some sh script contents?

2018-08-01 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 1 August 2018 at 14:58:56 UTC, Ky-Anh Huynh wrote:
This works well with user interaction. However I don't really 
like the idea of using temporary files. Is there any better way?


Maybe take a look at:

https://dlang.org/library/std/process/pipe_shell.html


Faster printing of floats; is this something that D could benefit from?

2018-07-28 Thread Gary Willoughby via Digitalmars-d

I just saw this on hacker news:

We present Ryū, a new routine to convert binary floating point 
numbers to their decimal representations using only fixed-size 
integer operations, and prove its correctness. Ryū is simpler 
and approximately three times faster than the previously 
fastest implementation.


Links:

https://pldi18.sigplan.org/event/pldi-2018-papers-ry-fast-float-to-string-conversion
https://www.youtube.com/watch?v=kw-U6smcLzk
https://github.com/ulfjack/ryu


Re: Check whether a range is empty

2018-07-17 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 13 July 2018 at 18:37:35 UTC, vino.B wrote:

Hi All,

  How do i check whether a range is empty. eg. 
(!PFResutl.toRange).empty. I tired the below, but it is no 
printing Empty if the range is empty it just prints blank line.


if (!(!PFResutl.toRange).empty) { writeln("Empty"); }


From,
Vino.B


I thought every range at the lowest level has an `empty` 
property. So, in this case, it would be:


if (PFResutl.toRange.empty)
{
writeln("Empty");
}


Re: Struct destructors not available in -betterC?

2018-07-11 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 10 July 2018 at 20:33:46 UTC, Seb wrote:
But they easily can be: 
https://github.com/dlang/dlang.org/pull/2415


Thanks.


Struct destructors not available in -betterC?

2018-07-10 Thread Gary Willoughby via Digitalmars-d
Looking at the page on -betterC it says that struct destructors 
are not available.


See point 11:
https://dlang.org/spec/betterc.html#consequences

This doesn't seem to be true as I'm using them with no problem.


Re: Friends in D, the easy way!

2018-06-18 Thread Gary Willoughby via Digitalmars-d

On Monday, 18 June 2018 at 06:37:41 UTC, Mr.Bingo wrote:
These go in the module you want allow access to the outside 
world just as if they were in the same module!


See the package attribute:

https://dlang.org/spec/attribute.html#visibility_attributes


Re: Proposal for a standard Decimal type in alpha

2018-03-14 Thread Gary Willoughby via Digitalmars-d-announce
On Thursday, 21 December 2017 at 13:59:28 UTC, Jack Stouffer 
wrote:
A couple of months ago, Andrei noted that a donor asked for a 
precise decimal type for D specifically: 
https://forum.dlang.org/post/osnema$d5s$1...@digitalmars.com. I've 
also heard this asked for many times, so I decided to start 
work on a library for eventual proposal to Phobos.


Why not polish the one up in the review queue?:

https://wiki.dlang.org/Review_Queue


Re: Why not flag away the mistakes of the past?

2018-03-09 Thread Gary Willoughby via Digitalmars-d

On Wednesday, 7 March 2018 at 17:11:55 UTC, H. S. Teoh wrote:

Kill autodecoding, I say. Kill it with fire!!


T


Please!!!



Re: Garbage collected pointers?

2018-03-02 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 2 March 2018 at 08:44:53 UTC, Gary Willoughby wrote:
It would be interesting to test whether those methods handle 
these scenarios.


Yeah, it doesn't work.

https://dpaste.dzfl.pl/55116efd0c9c


Re: Garbage collected pointers?

2018-03-02 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 1 March 2018 at 12:20:08 UTC, Steven Schveighoffer 
wrote:

On 3/1/18 7:05 AM, Gary Willoughby wrote:

On Thursday, 1 March 2018 at 10:10:27 UTC, John Burton wrote:
My question is how do I tell if a pointer is "garbage 
collected" or not?


You could try `GC.addrOf()` or `GC.query()` in core.memory.


I was going to say this, but then I realized, it's not that the 
pointer points at GC-allocated memory, but that the pointer 
itself is stored in a location that is scanned by the GC. 
That's not as easy to figure out, as you have to look at 
stacks, added ranges, etc.


-Steve


It would be interesting to test whether those methods handle 
these scenarios.




Re: Garbage collected pointers?

2018-03-01 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 1 March 2018 at 10:10:27 UTC, John Burton wrote:
My question is how do I tell if a pointer is "garbage 
collected" or not?


You could try `GC.addrOf()` or `GC.query()` in core.memory.


Re: Does dmd not always compile all of the source code?

2017-12-06 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 6 December 2017 at 16:47:17 UTC, A Guy With a 
Question wrote:

abstract class Test(T)
{
private:
T thing;

public:
this(T theThing)
{
thing = theThing;
thisdoesnotexist(); // expect compiler error right here
}
}

...but this compiles just fine.


It's because it's not being used in your program. How can the 
compiler compile it without knowing what T would be?


When you do this:


class Test2
   : Test!int
{
this(int thing)
{
super(thing);
}
}


You are suppling T and now it is compiled and an error raised.


Re: don't answer (possible/likely) spam

2017-11-13 Thread Gary Willoughby via Digitalmars-d

On Friday, 10 November 2017 at 16:09:48 UTC, Stefan Koch wrote:

please don't answer messages which are possibly spam.
Try this by not answering to this thread.


Ok! +1 ;)


Re: What are the unused but useful feature you know in D?

2017-11-07 Thread Gary Willoughby via Digitalmars-d

On Sunday, 25 June 2017 at 23:21:25 UTC, aberba wrote:
Can you share feature(s) in D people are not talking about 
which you've found very useful?


Some of the best features are in the standard library. I've 
written about them here:


http://nomad.so/2014/08/hidden-treasure-in-the-d-standard-library/
http://nomad.so/2015/08/more-hidden-treasure-in-the-d-standard-library/


Re: Note from a donor

2017-10-27 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 24 October 2017 at 13:20:10 UTC, Andrei Alexandrescu 
wrote:
A person who donated to the Foundation made a small wish list 
known. Allow me to relay it:


* RSA Digital Signature Validation in Phobos
* std.decimal in Phobos
* better dll support for Windows.


Andrei


std.decimal has been on the review list for quite some time:

https://wiki.dlang.org/Review_Queue


Re: Named arguments

2017-10-25 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 24 October 2017 at 17:30:27 UTC, Andrey wrote:
Hello, why there are no named arguments for functions like, for 
example, in kotlin i.e.:



int sum(in int a, in int b) {
return a + b;
}

sum(a = 1, b = 2);


This has been discussed to death:

http://forum.dlang.org/post/n8024o$dlj$1...@digitalmars.com

and you can do it as a library so no need to go in the language:

https://github.com/CyberShadow/ae/blob/master/utils/meta/args.d


Re: Deimos X11 bindings license question

2017-10-23 Thread Gary Willoughby via Digitalmars-d

On Saturday, 21 October 2017 at 08:14:01 UTC, vondes wrote:
How we can use it in https://mobile-phone-tracker.org mobile 
recorder on Android?


That doesn't look like a very ethical program.


Re: What does ! mean?

2017-09-27 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 27 September 2017 at 14:23:01 UTC, Ky-Anh Huynh 
wrote:
Can you please explain and give any link where I can learn more 
about these things?


Thanks a lot.


http://nomad.so/2013/07/templates-in-d-explained/


Re: Deimos X11 bindings license question

2017-09-13 Thread Gary Willoughby via Digitalmars-d
On Wednesday, 13 September 2017 at 17:55:43 UTC, Steven 
Schveighoffer wrote:
Note BTW, the C headers are included in the distribution, and 
those are NOT boost licensed. I'm not a lawyer, so I have no 
idea the compatibility implications between the two.


Thanks for the heads up, they've now been removed.


dmd v2.076.0-dirty?

2017-09-06 Thread Gary Willoughby via Digitalmars-d
I've just installed the latest dmd version (on ubuntu 64bit) and 
I get this:


$ dmd --version
DMD64 D Compiler v2.076.0-dirty
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright

What does 'dirty' mean?


Deimos X11 bindings license question

2017-09-03 Thread Gary Willoughby via Digitalmars-d

Hi,

A few years ago I forked the Deimos X11 bindings[1] repo to add 
dub support. Since then my repo[2] has received bug fixes and as 
such it's being used in many projects. (Also, in the following 
years dub support was added to the Deimos repo too.) I had a 
question from a developer as to the license of the code in my 
repo. I used the LGPL because the original used it.


My question, is there a legal way to change the current license 
to Boost or MIT or something like? Because this particular 
developer wanted to use it in a project where LGPL was 
incompatible.


[1]: https://github.com/D-Programming-Deimos/libX11
[2]: https://github.com/nomad-software/x11


Re: Interpolated strings

2017-08-25 Thread Gary Willoughby via Digitalmars-d

On Thursday, 24 August 2017 at 11:07:16 UTC, Suliman wrote:
All modern languages like Dart and C# have string 
interpolation. Sharp example:


Console.WriteLine($"Hello {args[0]}!");

Who can summary is there any objective reasons why it's not 
realized in D?


Maybe you ought to read this entire thread?


Re: Named multi-imports

2017-08-16 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 15 August 2017 at 03:37:39 UTC, rikki cattermole 
wrote:

On 15/08/2017 2:59 AM, Johnson wrote:
Not only that, but it requires adding more files to the 
command line.


I currently have 3 import files to separate the gtk from gdk 
that and the only reason they exist is to combine them in to 
one named import ;/



Doesn't seem like much but that's 3 extra files that don't 
really need to exist.


Hopefully D already implements a way to do what I'm asking.


Or instead of a new language feature, the gtk-d guys could have 
package files ;)


This! Just create a PR for Gtk-D to add packages.


Re: RFC: Implementation of binary assignment operators (e.g s.x += 2) for @property functions

2017-08-15 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin 
wrote:
An implementation of binary assignment operators for @property 
functions has been submitted to the DMD pull request queue at 
https://github.com/dlang/dmd/pull/7079.  It addresses the 
following issues:


Issue 8006 - Implement proper in-place-modification for 
properties

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


I thought @property's behaviour had been removed from the 
language and even though the attribute remains, it doesn't 
actually do anything?





Re: C style 'static' functions

2017-07-19 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 19 July 2017 at 07:22:48 UTC, John Burton wrote:
In C++ I could use static or an anonymous namespace for 
implementation functions, but there doesn't seem to be anything 
similar in D.
Is there any way to achieve what I want in D (Private 
implementation functions)


Try the package keyword: 
https://dlang.org/spec/attribute.html#visibility_attributes


Re: Let's paint those bikesheds^Werror messages!

2017-06-28 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 27 June 2017 at 14:32:28 UTC, Vladimir Panteleev 
wrote:


- Yes, not everyone likes colors. You can turn all colors off 
with a command-line switch.
- Yes, everyone agrees that having all colors be configurable 
would be good. We still need defaults that are going to look OK 
on most terminals.
- Yes, no matter what colors we choose, they're going to look 
bad on some terminal somewhere. Let's worry about the major 
platforms' most common terminals for now.


All of these points give added weight to this feature being opt 
in, not opt out. Why would we want to make output worse? Also, we 
shouldn't syntax highlight the message, just colour the whole 
line.


Re: Checked vs unchecked exceptions

2017-06-27 Thread Gary Willoughby via Digitalmars-d

I think it's important to understand, D is *not* Java.


D Language accepted for inclusion in GCC

2017-06-22 Thread Gary Willoughby via Digitalmars-d-announce

D Language accepted for inclusion in GCC:

https://gcc.gnu.org/ml/gcc/2017-06/msg00111.html

Well done Iain Buclaw!

Reddit thread: 
https://www.reddit.com/r/programming/comments/6im1yo/david_edelsohn_d_language_accepted_for_inclusion/


Inside D's GC blog article on hacker news front page

2017-06-20 Thread Gary Willoughby via Digitalmars-d-announce

Inside D's GC blog article currently No.3 on hacker news

Here's the article, visit hacker news to read the comments.

http://olshansky.me/gc/runtime/dlang/2017/06/14/inside-d-gc.html


Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 11:36:45 UTC, Steven Schveighoffer 
wrote:


Nope, const works just fine. A clue is in your return type -- 
it's not inout!


This should work:

public Rational opBinary(string op)(Rational rhs) const

If Rational had any indirections, then inout would be required, 
and your signature wouldn't work (because you can't convert an 
inout(SomethingWithPointers) to SomethingWithPointers).


Why do I need to make the member function const, but not the 
parameter? Because the parameter is taken by value, 'this' is a 
reference.


-Steve


Is it possible for the `result` variable in the following code to 
be returned as an immutable type if it's created by adding two 
immutable types?


import std.stdio;

struct Rational
{
public long numerator;
public long denominator;

	public inout Rational opBinary(string op)(inout(Rational) other) 
const

{
static if (op == "+")
{
return inout(Rational)(0, 0);
}
else
{
static assert(0, "Operator '" ~ op ~ "' not 
implemented");
}
}
}

unittest
{
auto baz= immutable(Rational)(1, 3);
auto qux= immutable(Rational)(1, 6);
auto result = baz + qux;

writeln(typeof(result).stringof); // Result is not immutable!
}



Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 12 June 2017 at 20:10:17 UTC, H. S. Teoh wrote:
Therefore, nowadays I always recommend writing parenthesis with 
type modifiers, so that the intent it unambiguous, i.e., always 
write `inout(Rational)` rather than `inout Rational`, unless 
you intend for `inout` to apply to the function rather than the 
return value. (And I apologize for the slip-up in my code 
example above, where I failed to do this for the function 
parameter.)



T


Ok, thanks.


Re: Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 12 June 2017 at 19:36:52 UTC, H. S. Teoh wrote:
On Mon, Jun 12, 2017 at 07:38:44PM +, Gary Willoughby via 
Digitalmars-d-learn wrote:
In the following code is there any way to make the `opBinary` 
method generic to be able to accept immutable as well as a 
standard type? The code currently passes the unit test but I 
wonder if I could get rid of the duplication to overload the 
operator? I'm failing badly.


This is what inout was designed for:

public inout Rational opBinary(string op)(inout Rational rhs)
{
static if (op == "+")
{
return inout(Rational)(0, 0);
}
else
{
static assert(0, "Operator '" ~ op ~ "' not 
implemented");
}
}

That should do the trick.


T


Quick question about the signature, if I change it to (note the 
parens):


   public inout(Rational) opBinary(string op)(inout(Rational) rhs)

It no longer works, why is that?


Re: Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn
I don't know how H. S. Teoh managed to answer 'before' I posted 
but thanks guys! :)


Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn
In the following code is there any way to make the `opBinary` 
method generic to be able to accept immutable as well as a 
standard type? The code currently passes the unit test but I 
wonder if I could get rid of the duplication to overload the 
operator? I'm failing badly.



import std.stdio;

struct Rational
{
public long numerator;
public long denominator;

	public immutable Rational opBinary(string op)(immutable Rational 
rhs)

{
static if (op == "+")
{
return Rational(0, 0);
}
else
{
static assert(0, "Operator '" ~ op ~ "' not 
implemented");
}
}

public Rational opBinary(string op)(Rational rhs)
{
static if (op == "+")
{
return Rational(0, 0);
}
else
{
static assert(0, "Operator '" ~ op ~ "' not 
implemented");
}
}
}

unittest
{
auto foo = Rational(1, 3);
auto bar = Rational(1, 6);
writefln("%s", foo + bar);

auto baz = immutable Rational(1, 3);
auto qux = immutable Rational(1, 6);
writefln("%s", baz + qux);
}



Re: The syntax of sort and templates

2017-05-26 Thread Gary Willoughby via Digitalmars-d-learn

On Friday, 26 May 2017 at 09:59:26 UTC, zakk wrote:
1) Why is D making using of the binary ! operator, which as far 
as I understand introduces a template?


The exclamation mark here is not a binary operator, it's used in 
D templates to define where compile-time parameters are.



2) Why is a template needed here?


It's a template so you can use differently typed ranges. Remember 
a range is an interface and very different types can implement it.


3) It seems to me like the argument passed to the template is a 
lambda expression. I only know about templates taking types as 
argument. What's going on?


The function parameter is defined as an alias which is also valid 
at compile-time.


https://dlang.org/phobos/std_algorithm_sorting.html#sort


Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn

On Saturday, 20 May 2017 at 12:25:39 UTC, Stanislav Blinov wrote:

Oof. Dangerous stuff.


:)

Thanks for the heads up but I think I'm covering all cases in my 
main code.


Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn

On Saturday, 20 May 2017 at 11:15:57 UTC, Moritz Maxeiner wrote:
Because `element = tmp` destroys `element`, which you allocated 
filled with zeroes.

The destructor will run for each `element`.


Right, I get it because the destructors running on the struct 
that is being replaced. Doh!


Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn
In the following code, the `_foo` pointer (of the Foo struct) is 
null in the first call to the destructor. Why is this? I think 
it's got something to do with the foreach loop but I'm not sure. 
Any ideas?



import std.stdio;
import core.stdc.stdlib : malloc, calloc, free;

struct Foo
{
public int* _foo;

public this(int n)
{
this._foo  = cast(int*) malloc(int.sizeof);
writefln("ctor: %x", this._foo);
}

public this(this)
{
writefln("post blit: %x", this._foo);
}

public ~this()
{
// Why is this._foo null here???
writefln("dtor: %x", this._foo);
}
}

struct Bar
{
private Foo[] _data;

public this(int n)
{
this._data = (cast(Foo*) calloc(n, Foo.sizeof))[0 .. n];

foreach(ref element; this._data)
{
auto tmp = Foo(1);
element = tmp;
}
}
}

void main(string[] args)
{
auto bar = Bar(1);
}



Re: How to check a struct exists at a particular memory address?

2017-05-18 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 18 May 2017 at 21:09:06 UTC, Igor wrote:

On Thursday, 18 May 2017 at 20:20:47 UTC, Gary Willoughby wrote:

This might be a really silly question but:

I've allocated some memory like this (Foo is a struct):

this._data = cast(Foo*) calloc(n, Foo.sizeof);

How can I then later check that there is a valid Foo at 
`this._data` or `this._data + n`?


Well... I think the right answer is that everything you do with 
memory should be very deterministic so you should just know 
where is what and not have a need to check :).


Agreed. I think I'll re-visit the design.




How to check a struct exists at a particular memory address?

2017-05-18 Thread Gary Willoughby via Digitalmars-d-learn

This might be a really silly question but:

I've allocated some memory like this (Foo is a struct):

this._data = cast(Foo*) calloc(n, Foo.sizeof);

How can I then later check that there is a valid Foo at 
`this._data` or `this._data + n`?


Re: Interpolated strings

2017-04-21 Thread Gary Willoughby via Digitalmars-d
On Friday, 21 April 2017 at 12:32:01 UTC, Nick Sabalausky 
(Abscissa) wrote:


"Completely unnecessary" features like that are exactly what 
make D worthwhile in the first place. Otherwise may as well 
stick to C++ or Java.


Multiple ways of doing the same thing are not valuable or 
progressive.


Go and Rust are both smashing D in popularity and user share, 
maybe we could learn why that's the case.


Re: Interpolated strings

2017-04-21 Thread Gary Willoughby via Digitalmars-d

On Thursday, 20 April 2017 at 18:28:30 UTC, Atila Neves wrote:

I don't understand how

writeln($"{a} times 3 is {a * 3}");

is even marginally better than

writeln(a, " times 3 is ", a * 3);  // look ma, works right now!

It's not even fewer characters.

Atila


This!

This is bloat that doesn't need adding. D is complicated already, 
stop adding more 'stuff' and fix what's already there. I wish 
more time was taken up on libraries and tooling than completely 
unnecessary features like this.


Re: is char[] faster than string?

2017-04-06 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 5 April 2017 at 21:58:16 UTC, Inquie wrote:

What I am looking for is something like StringBuilder in C#.


std.array.appender


Re: Memory Allocation

2017-03-30 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
I have a memory buffer allocated using different methods. It is 
simply a pointer and a size.


I would like to be able to manage this buffer by treating it as 
a memory pool or heap. I think I can use allocators to do this 
but not sure how.


You can re-task the GC to use an arena if that's suitable:

https://bitbucket.org/infognition/dstuff/src/1dca752af1021ed5998bb041004465674695113f/gcarena.d?fileviewer=file-view-default


Re: how to define my own traits

2017-03-27 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 26 March 2017 at 23:25:49 UTC, XavierAP wrote:
I've looked into Phobos to emulate it when defining my own 
trait template, and when I see this:


module std.range.primitives;
// ...
template isInputRange(R)
{
enum bool isInputRange = is(typeof(
(inout int = 0)
{
R r = R.init; // can define a range object
if (r.empty) {}   // can test for empty
r.popFront;   // can invoke popFront()
auto h = r.front; // can get the front of the range
}));

I wonder, why that unused parameter (inout int = 0)?
In my project () { /* ... */ } works the same for a custom 
trait.


Even Andrei was baffled:

http://forum.dlang.org/thread/nepm2k$311l$1...@digitalmars.com


Re: mysql-native: API Refresh RC

2017-01-30 Thread Gary Willoughby via Digitalmars-d-announce

On Monday, 30 January 2017 at 02:56:27 UTC, Nick Sabalausky wrote:
I've been working on a big refresh of mysql-native's API, to 
take care of various issues that have appeared with it. It 
involves some major breaking changes (although I've tried to 
keep old interfaces around for the moment, but marked 
deprecated), so I wanted to post it before committing to it so 
those interested have a change to take a look, give feedback, 
catch problems, etc.


[...]


Can we use semantic versioning correctly here and get an 
increment on the major version please. I'm so sick of seeing D 
libs in endless alpha/beta!


Re: Red Hat's issues in considering the D language

2016-12-21 Thread Gary Willoughby via Digitalmars-d
On Tuesday, 20 December 2016 at 23:08:28 UTC, Andrei Alexandrescu 
wrote:
Hello, a few engineers at Red Hat are taking a look at using 
the D language on the desktop and have reached out to us. They 
have created a list of issues. We are on the top-level ones, 
and of course would appreciate any community help as well.


https://gist.github.com/ximion/77dda83a9926f892c9a4fa0074d6bf2b


Thanks,

Andrei


The assert/unittest issues can be solved with a library:

https://github.com/nomad-software/dunit


Re: All function attributes possible with "@"?

2016-12-16 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 13 December 2016 at 22:40:47 UTC, 01010100b wrote:
On the wiki there is an argument given for why not to allow "@" 
on function attributes which are keywords, however it seems to 
include a reasoning error.


Related DIP: https://wiki.dlang.org/DIP64




Re: constraint on variadic template

2016-12-07 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 7 December 2016 at 16:35:52 UTC, Alex wrote:

mixin template S(T...)
{
void run()
{
foreach(s; T)
{
static assert(__traits(hasMember, s, "run"));
}
}
}

How to formulate the check inside the foreach as a template 
constraint with

mixin template S(T...) if(???)


Will some like this work?

import std.range.primitives;

mixin template S(T...) if(__traits(hasMember, 
ElementType!(T), "run"))

{
...
}

https://dlang.org/phobos/std_range_primitives.html#ElementType


Re: Release D 2.072.1

2016-12-01 Thread Gary Willoughby via Digitalmars-d-announce
On Wednesday, 30 November 2016 at 22:49:12 UTC, Martin Nowak 
wrote:

Glad to announce D 2.072.1.

http://dlang.org/download.html

This point release fixes a few issues over 2.072.0, see the 
changelog for more details.


http://dlang.org/changelog/2.072.1.html

-Martin


The download page doesn't seem to feature this new release.


What is the simplest way of doing @nogc string concatenation?

2016-11-03 Thread Gary Willoughby via Digitalmars-d-learn

What is the simplest way of doing @nogc string concatenation?


Re: Can someone please explain why the following assertion fails?

2016-11-01 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 1 November 2016 at 14:06:08 UTC, Steven Schveighoffer 
wrote:

On 10/31/16 3:08 PM, Gary Willoughby wrote:

Can someone please explain why the following assertion fails?

import std.stdio;
import std.conv;

void main(string[] args)
{
auto x = 1;

assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
}

Thanks.


IMO, it shouldn't. A string's "value" has nothing to do with 
it's location.


-Steve


It definitely is surprising. I'll raise an issue and see what 
others think.


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


Re: Can someone please explain why the following assertion fails?

2016-11-01 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 31 October 2016 at 22:10:30 UTC, Ali Çehreli wrote:

On 10/31/2016 12:08 PM, Gary Willoughby wrote:

Can someone please explain why the following assertion fails?

import std.stdio;
import std.conv;

void main(string[] args)
{
auto x = 1;

assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
}

Thanks.


I think you need TypeInfo.getHash.

  https://dlang.org/phobos/object.html#.TypeInfo.getHash

import std.conv;

auto myHashOf(T)(auto ref T value) {
return typeid(T).getHash();
}

void main() {
auto x = 1;
auto s = "1";
assert(myHashOf(x.to!string) == myHashOf(x.to!string));
assert(myHashOf(s) == myHashOf(s));
assert(myHashOf(s) == myHashOf(x.to!string));
}

Ali


Thanks but `TypeInfo.getHash` is not @nogc. Looks like I'll have 
to roll my own.


Re: Can someone please explain why the following assertion fails?

2016-10-31 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 31 October 2016 at 19:24:13 UTC, Ali Çehreli wrote:

On 10/31/2016 12:08 PM, Gary Willoughby wrote:

[...]


Because it considers the .ptr property of arrays as well:


https://github.com/dlang/druntime/blob/master/src/core/internal/hash.d#L61

[...]


Ah right.

Is there an alternative built-in, generic, nogc hash function 
that would return the same values for Strings?


Re: Can someone please explain why the following assertion fails?

2016-10-31 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 31 October 2016 at 19:08:50 UTC, Gary Willoughby wrote:

Can someone please explain why the following assertion fails?

import std.stdio;
import std.conv;

void main(string[] args)
{
auto x = 1;

assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
}

Thanks.


DMD64 D Compiler v2.072.0
Copyright (c) 1999-2016 by Digital Mars written by Walter Bright

Ubuntu 16.04


Can someone please explain why the following assertion fails?

2016-10-31 Thread Gary Willoughby via Digitalmars-d-learn

Can someone please explain why the following assertion fails?

import std.stdio;
import std.conv;

void main(string[] args)
{
auto x = 1;

assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
}

Thanks.


Comparing compilation time of random code in C++, D, Go, Pascal and Rust

2016-10-19 Thread Gary Willoughby via Digitalmars-d-announce

This was posted on twitter a while ago:

Comparing compilation time of random code in C++, D, Go, Pascal 
and Rust


http://imgur.com/a/jQUav

D was doing well but in the larger examples the D compiler 
crashed: "Error: more than 32767 symbols in object file".


Re: What exactly does the compiler switch -betterC do?

2016-09-26 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 20 September 2016 at 13:23:35 UTC, Jacob Carlborg 
wrote:

On 2016-09-19 23:09, Gary Willoughby wrote:

$ rdmd --build-only --force -betterC -de -O -inline -release 
-w test.d

$ nm test


Indeed. I just noticed now that there's a difference between 
2.070.0 and 2.071.0. I get 4 symbols with 2.070.0 and 2428 with 
2.071.0. I would say it's a bug.


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


Re: What exactly does the compiler switch -betterC do?

2016-09-19 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 20 June 2016 at 06:35:32 UTC, Jacob Carlborg wrote:

On 2016-06-19 21:53, Gary Willoughby wrote:
When compiling, what exactly does the -betterC flag do? The 
command help
says "omit generating some runtime information and helper 
functions" but

what does this really mean? Is there any specifics somewhere?


It is intended to allow you to link an application without 
druntime. A Hello World using "extern(C) main" and printing 
using "printf" contains the following symbols on OS X:


00a8 S _D4main12__ModuleInfoZ
0068 T _D4main15__unittest_failFiZv
0018 T _D4main7__arrayZ
0040 T _D4main8__assertFiZv
00b5 s _TMP1
 U __d_arraybounds
 U __d_assert
 U __d_unittest
 T _main
 U _printf

If compiled with -betterC, it contains these:

 T _main
 U _printf


I get significantly more symbols than that when compiling the 
following program any idea why?


import core.stdc.stdio;

extern(C) void main()
{
printf("Hello World!\n");
}


$ rdmd --build-only --force -betterC -de -O -inline -release -w 
test.d

$ nm test



Re: I hate new DUB config format

2016-09-16 Thread Gary Willoughby via Digitalmars-d

On Wednesday, 14 September 2016 at 11:54:56 UTC, Suliman wrote:
Sönke Ludwig, really sorry. It's look my big mistake. I looked 
at SDL more detail, and this format is much better than JSON.


I hope a lot of people is changed their position too.


There is a superset of Json that could of been used instead.


Re: I hate new DUB config format

2016-09-16 Thread Gary Willoughby via Digitalmars-d

On Wednesday, 25 November 2015 at 10:17:02 UTC, Suliman wrote:
I think that using SDL format was big mistake. Not only I do 
not want to spend time in learning yet another dead config 
format that now use only one project -- DUB. In time when DUB 
used json it was not perfect, but at last it was standard and 
everybody can read it.


Now when I come to code.dlang.org I can't simply do copy-past 
of dependence. I need go to docs page, and read how to include 
it.


This is exactly why I (and many others) spoke up against it and 
why Andrei said had he of known sooner, he would have stopped it! 
It is a terrible decision especially as SDL is so obscure.


Re: Metaprogramming with traits

2016-09-16 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 15 September 2016 at 22:05:55 UTC, Ram_B wrote:

test.d(33): Error: variable f cannot be read at compile time
test.d(33): Error: string expected as second argument of 
__traits hasMember instead of __error
test.d(46): Error: template instance test.A.t!(B) error 
instantiating


Maybe Array!(string) can't be use in compile-time code?


Re: Metaprogramming with traits

2016-09-15 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 15 September 2016 at 15:07:09 UTC, Ram_B wrote:
How i can get fields of derived classes in runtime? This not 
works


What about something like this:

import std.traits;
import std.stdio;

class A {
int a,b;
this(){}
void fields(this T)(){
writeln(FieldNameTuple!(T));
foreach(Class; TransitiveBaseTypeTuple!(T)) {
writeln(FieldNameTuple!(Class));
}
}
}

class B : A{
int c;
this(){}
}

class C : B{
int d;
this(){}
}
void main(){
C c = new C();
c.fields();
}


Re: Should debug{} allow GC?

2016-09-12 Thread Gary Willoughby via Digitalmars-d

On Sunday, 11 September 2016 at 07:46:09 UTC, Manu wrote:
I'm having a lot of trouble debugging @nogc functions. I have a 
number of debug functions that use GC, but I can't call them 
from @nogc code... should debug{} allow @nogc calls, the same 
as impure calls?


We could with something like this in Phobos:

void assumeNogc(alias Func, T...)(T xs) @nogc {
import std.traits;
static auto assumeNogcPtr(T)(T f) if (
isFunctionPointer!T || isDelegate!T
) {
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
	return cast(SetFunctionAttributes!(T, functionLinkage!T, 
attrs)) f;

};
assumeNogcPtr(!T)(xs);
};


void main() @nogc
{
import std.stdio;
assumeNogc!writefln("foo %s", 42);
}

Source: https://dpaste.dzfl.pl/8c5ec90c5b39


Re: @property Incorrectly Implemented?

2016-09-12 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 6 September 2016 at 19:18:11 UTC, John wrote:

This is all you need to know:

https://wiki.dlang.org/Property_Discussion_Wrap-up


Re: Phobos unittests either spuriously fail or spuriously pass

2016-08-14 Thread Gary Willoughby via Digitalmars-d

Maybe related: https://issues.dlang.org/show_bug.cgi?id=16204




Re: How to add nogc to delegate

2016-08-11 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 11 August 2016 at 05:12:39 UTC, ag0aep6g wrote:

On 08/11/2016 06:15 AM, Engine Machine wrote:

void foo(@nogc void delegate())

doesn't work.


Put it after the parameter list, like so:

void foo(void delegate() @nogc)


You may also need to add the scope keyword too.

Reference:
http://forum.dlang.org/thread/zaxaqgeeenwypmijr...@forum.dlang.org


Re: Decimal/Currency Type

2016-08-10 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 9 August 2016 at 10:48:50 UTC, Zane wrote:
There has been a question or two about this before, but I 
really don't feel there has been a reasonable answer (or I have 
missed it).


[...]


There is one in the review queue since forever.

https://wiki.dlang.org/Review_Queue

Maybe take a look at that?


Re: I need a @nogc version of hashOf(). What are the options?

2016-08-07 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 7 August 2016 at 18:37:19 UTC, ag0aep6g wrote:

On 08/07/2016 07:10 PM, ag0aep6g wrote:

https://github.com/dlang/druntime/pull/1624


Has been merged. Is going to be part of 2.072.


Very cool!

MurmurHash3 is a great addition too. Thanks guys.


I need a @nogc version of hashOf(). What are the options?

2016-08-07 Thread Gary Willoughby via Digitalmars-d-learn
I need a @nogc version of hashOf(). Here's one i'm currently 
using but it's not marked as @nogc.


https://github.com/dlang/druntime/blob/master/src/object.d#L3170

What are the options now?

Is there anything D offers that I could use? I need a function 
that takes a variable of any type and returns a numeric hash.


Re: Endiannes & Splitting Values

2016-07-07 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 7 July 2016 at 10:48:56 UTC, Lodovico Giaretta wrote:

On Thursday, 7 July 2016 at 10:45:12 UTC, Gary Willoughby wrote:
On Thursday, 7 July 2016 at 08:21:53 UTC, Lodovico Giaretta 
wrote:
Are you sure that this works in both big-endian and 
little-endian systems?


It shouldn't matter. You're just interested in the high and 
low 4 byte chunks (which are to be interpreted as an int) 
which will return in the relevant endianess of your machine.


But are the high 4 bytes the first 4 or the second 4? It 
depends on endianness. So your high and low variables may be 
switched, if I understand correctly.


Ah, I see. You could modify it like this:

union Value
{
ulong full;

static struct Bits
{
version (BigEndian)
{
uint high;
uint low;
}
else
{
uint low;
uint high;
}

}

Bits bits;
alias bits this;

this(ulong value)
{
this.full = value;
}
}


Re: Endiannes & Splitting Values

2016-07-07 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 7 July 2016 at 08:21:53 UTC, Lodovico Giaretta wrote:
Are you sure that this works in both big-endian and 
little-endian systems?


It shouldn't matter. You're just interested in the high and low 4 
byte chunks (which are to be interpreted as an int) which will 
return in the relevant endianess of your machine.


Re: Endiannes & Splitting Values

2016-07-07 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 6 July 2016 at 21:44:37 UTC, BitGuy wrote:
I'm trying to implement a feistel cipher that'll give the same 
results regardless of the endianness of the machine it runs on. 
To make the cipher I need to split a 64bit value into two 32bit 
values, mess with them, and then put them back together. I can 
think of a few ways to split a 64bit value with versions or the 
endianness functions in bitmanip but it all seems pretty messy 
for just wanting to split a value... I'm thinking maybe I can 
just cast and bitshift so I can forget about the endianness but 
I'm not really sure about the casting down rules and if that'd 
work?


What about something like:

import std.stdio;

union Value
{
ulong full;

static struct Bits
{
uint high;
uint low;
}

Bits bits;
alias bits this;

this(ulong value)
{
this.full = value;
}
}

void main(string[] args)
{
auto value = Value(77309411348);

writefln("%s, (%b)", value.high, value.high);
writefln("%s, (%b)", value.low, value.low);
writefln("%s, (%b)", value.full, value.full);
}


Re: Minor feature request

2016-06-28 Thread Gary Willoughby via Digitalmars-d

On Tuesday, 28 June 2016 at 06:13:44 UTC, Superstar64 wrote:

---
auto func(MyObj obj) with(obj)
{
//...
}

auto func(int arg) return arg;

auto func() try
{
//...
}
finally
{
return //...
}
---


Please no! All of these are awful.


Re: When using the -profile flag is it known behaviour that phobos unit tests fail?

2016-06-25 Thread Gary Willoughby via Digitalmars-d

On Friday, 24 June 2016 at 22:24:09 UTC, Walter Bright wrote:
Please post bug reports to bugzilla. They'll get lost in the 
n.g.


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


Re: When using the -profile flag is it known behaviour that phobos unit tests fail?

2016-06-24 Thread Gary Willoughby via Digitalmars-d

On Friday, 24 June 2016 at 17:36:49 UTC, Gary Willoughby wrote:
When using the -profile flag is it known behaviour that phobos 
unit tests fail?


(Ubuntu 16.04 - DMD64 D Compiler v2.071.0)

For example, when following these steps I get a failed unit 
test:


$ cd /usr/include/dmd/phobos/std/
$ rdmd -I/usr/include/dmd/phobos/std 
-I/usr/include/dmd/phobos/core -main -unittest -profile format.d


Without the -profile flag it works.


You may need to add the --force option to rdmd.

$ rdmd --force -I/usr/include/dmd/phobos/std 
-I/usr/include/dmd/phobos/core -main -unittest -profile format.d


When using the -profile flag is it known behaviour that phobos unit tests fail?

2016-06-24 Thread Gary Willoughby via Digitalmars-d
When using the -profile flag is it known behaviour that phobos 
unit tests fail?


(Ubuntu 16.04 - DMD64 D Compiler v2.071.0)

For example, when following these steps I get a failed unit test:

$ cd /usr/include/dmd/phobos/std/
$ rdmd -I/usr/include/dmd/phobos/std 
-I/usr/include/dmd/phobos/core -main -unittest -profile format.d


Without the -profile flag it works.


Re: Is there anyway to make opApply @nogc?

2016-06-22 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 22 June 2016 at 13:36:54 UTC, Marc Schütz wrote:

On Tuesday, 21 June 2016 at 19:21:01 UTC, Gary Willoughby wrote:
Right ok, thanks! It doesn't seem to help though as the 
compiler complains about it being not @nogc.


You probably need to declare the delegate and opApply() itself 
as @nogc, too:


int opApply(scope int delegate(int) @nogc dg) @nogc { }


That fixed it, thanks all for your help. :)


Re: Proposed Enhancement: Deprecate std.conv.text With 0 Arguments

2016-06-22 Thread Gary Willoughby via Digitalmars-d

On Wednesday, 22 June 2016 at 15:39:11 UTC, Meta wrote:
If it is called with 0 arguments it will return null. This 
behaviour has caused several bugs in my code because combined 
with optional parens and UFCS, it is easy to accidentally call 
text with 0 args but have it look like passing a variable to a 
function.


It's really annoying:

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


Re: Is there anyway to make opApply @nogc?

2016-06-21 Thread Gary Willoughby via Digitalmars-d-learn

On Tuesday, 21 June 2016 at 12:53:11 UTC, Adam D. Ruppe wrote:

On Tuesday, 21 June 2016 at 12:48:04 UTC, Gary Willoughby wrote:
I have no idea what that means. Can anyone shed more light on 
this, please?


So when you use local variables in a delegate, the compiler 
usually makes a copy of them just in case the delegate gets 
saved for later.


When you mark it scope, you promise that you won't save it for 
later, so the compiler skips making the copy.


Right ok, thanks! It doesn't seem to help though as the compiler 
complains about it being not @nogc.


Re: Is there anyway to make opApply @nogc?

2016-06-21 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 20 June 2016 at 15:27:32 UTC, Adam D. Ruppe wrote:

On Monday, 20 June 2016 at 15:13:53 UTC, Gary Willoughby wrote:
I think the problem is that the delegate which is required by 
opApply is allocated using the GC.


make the delegate in opApply scope

int opApply(scope int delegate(whatever) dg)


I'm still not sure what this achieves. The description on the 
stackoverflow question reads: "And when the compiler sees this on 
delegates, it will avoid allocating a closure when taking the 
address of a local function. This is essential in opApply loops."


I have no idea what that means. Can anyone shed more light on 
this, please?


Re: Is there anyway to make opApply @nogc?

2016-06-20 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 20 June 2016 at 15:47:44 UTC, Gary Willoughby wrote:

On Monday, 20 June 2016 at 15:27:32 UTC, Adam D. Ruppe wrote:

On Monday, 20 June 2016 at 15:13:53 UTC, Gary Willoughby wrote:
I think the problem is that the delegate which is required by 
opApply is allocated using the GC.


make the delegate in opApply scope

int opApply(scope int delegate(whatever) dg)


What effect does this have? and how does it beat the GC?


Found the explanation here:

http://stackoverflow.com/questions/4711309/meaning-of-scope-in-d-for-a-parameter

I give it a go. Thanks.


Re: Is there anyway to make opApply @nogc?

2016-06-20 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 20 June 2016 at 15:27:32 UTC, Adam D. Ruppe wrote:

On Monday, 20 June 2016 at 15:13:53 UTC, Gary Willoughby wrote:
I think the problem is that the delegate which is required by 
opApply is allocated using the GC.


make the delegate in opApply scope

int opApply(scope int delegate(whatever) dg)


What effect does this have? and how does it beat the GC?


Re: Is there anyway to make opApply @nogc?

2016-06-20 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 20 June 2016 at 14:34:33 UTC, Mathias Lang wrote:
Can't `opApply` with `auto` return type works since it infers 
attributes ?


I think the problem is that the delegate which is required by 
opApply is allocated using the GC.


Is there anyway to make opApply @nogc?

2016-06-20 Thread Gary Willoughby via Digitalmars-d-learn
Is there any way to make opApply @nogc? or provide the same 
foreach functionality without implementing a range interface?


I want to iterate over a piece of memory using a pointer. I 
thought about using opSlice but that doesn't provide information 
for an index in a foreach loop.


auto opSlice()
{
return this._pointer[0 .. size];
}

Anyone got any ideas?


What exactly does the compiler switch -betterC do?

2016-06-19 Thread Gary Willoughby via Digitalmars-d-learn
When compiling, what exactly does the -betterC flag do? The 
command help says "omit generating some runtime information and 
helper functions" but what does this really mean? Is there any 
specifics somewhere?


Re: Why do I get this error when casting to an immutable or shared byRef return type?

2016-06-19 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 19 June 2016 at 10:35:59 UTC, Gary Willoughby wrote:

...


A more correct example:

import core.stdc.stdlib;
import std.traits;

ref T foo(T)()
{
alias Type = Unqual!(T);

Type* foo = cast(Type*) malloc(Type.sizeof * 8);

return *foo;
}

void main(string[] args)
{
// Works!
auto bar = foo!(int)();

// Works!
auto bar = foo!(const(int))();

// Error: cast(immutable(int))*foo is not an lvalue
auto bar = foo!(immutable(int))();

// Error: cast(shared(int))*foo is not an lvalue
auto bar = foo!(shared(int))();
}


Why do I get this error when casting to an immutable or shared byRef return type?

2016-06-19 Thread Gary Willoughby via Digitalmars-d-learn
In the following code, the `foo` function doesn't work when 
casting to an immutable or shared type. Can anyone please explain 
what is happening here? Is there any way of returning such 
variables byRef from a malloc'd chunk of memory?


import core.stdc.stdlib;

ref T foo(T)()
{
int* foo = cast(int*) malloc(int.sizeof * 8);

return *foo;
}

void main(string[] args)
{
// Works!
auto bar = foo!(int)();

// Works!
auto bar = foo!(const(int))();

// Error: cast(immutable(int))*foo is not an lvalue
auto bar = foo!(immutable(int))();

// Error: cast(shared(int))*foo is not an lvalue
auto bar = foo!(shared(int))();
}


Is it possible to create a static factory method on a templated struct?

2016-06-18 Thread Gary Willoughby via Digitalmars-d-learn

I've tried the following code and I get the error:

Error: template Foo(A) does not have property 'of'


struct Foo(A)
{
private int _foo;

@disable this();

public this(int foo)
{
this._foo = foo;
}

public static auto of(B)()
{
return Foo!(B)(8);
}
}

void main(string[] args)
{
auto foo = Foo.of!(string);
}


Is it possible to even have static methods on structs like this? 
What am I doing wrong?


  1   2   3   4   5   6   7   8   9   10   >