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

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

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

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: 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

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"); }

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()`

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.

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: 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

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

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

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

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;

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

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 {

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

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: 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

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

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

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)) ==

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)) ==

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

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

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.

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

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

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

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)(){

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:

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

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

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

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

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

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

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

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

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

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

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[]

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

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)()

Is there a more elegant way of testing T for multiple types?

2016-06-18 Thread Gary Willoughby via Digitalmars-d-learn
Here I'm testing T is either a class or interface: void foo(T)(T bar) if (is(T == class) || is(T == interface)) { ... } Is there a more elegant way of testing T for multiple types? Because it doesn't scale well if I need to add more. I would love to use something like this: void

Re: Default initialization of structs?

2016-06-17 Thread Gary Willoughby via Digitalmars-d-learn
On Friday, 17 June 2016 at 10:53:40 UTC, Lodovico Giaretta wrote: struct Foo(T) { private int _bar = 1; this(int bar) { this._bar = bar; } } auto foo = Foo!(string)(); This should do the trick. Thanks, I forgot to mention I'm also doing lots of other stuff in the

Default initialization of structs?

2016-06-17 Thread Gary Willoughby via Digitalmars-d-learn
I have a struct where I need to perform default initialization of some members but the compiler doesn't allow to define a default constructor which allow optional arguments. struct Foo(T) { private int _bar; this(int bar = 1) { this._bar = bar; } } auto foo =

Re: Creating a reference counted type?

2016-06-12 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 12 June 2016 at 15:05:53 UTC, ketmar wrote: this is basically how refcounted structs are done. note that i just typed the code into reply box, so it may not compile or contain some small bugs, but i think you got the idea. Thanks for the replies guys.

Re: Creating a reference counted type?

2016-06-12 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 12 June 2016 at 14:45:12 UTC, ketmar wrote: ahem... wut?! we have one copy of our struct freed half the way, and another copy has refcount of 2, so it won't be freed at all. it doesn't so innocent as it looks: we may try to use `f` in `main`... just to find out that resources was

Re: Creating a reference counted type?

2016-06-12 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 12 June 2016 at 14:29:19 UTC, Gary Willoughby wrote: Another thing that is puzzling me is that when creating an instance of the above struct and passing as an argument to a function, the copy constructor is called and the reference count is incremented. This is expected. However,

Creating a reference counted type?

2016-06-12 Thread Gary Willoughby via Digitalmars-d-learn
I'm wondering if it's this easy to create a reference counted type: struct Foo { int _refCount = 1; this(...) { // allocate resources, etc. } this(this) { this._refCount++; } ~this() {

Re: D, GTK, Qt, wx,…

2016-06-04 Thread Gary Willoughby via Digitalmars-d-learn
On Saturday, 4 June 2016 at 08:27:53 UTC, Russel Winder wrote: On Sun, 2016-05-29 at 14:01 +0200, Jordi Sayol via Digitalmars-d-learn wrote: […] https://github.com/nomad-software/tkd I am not a great fan of tk even in Python. It is true that Tk is everywhere and so meets the portability

Re: Is there any overhead iterating over a pointer using a slice?

2016-06-01 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 31 May 2016 at 20:52:20 UTC, Johan Engelen wrote: On Tuesday, 31 May 2016 at 18:55:18 UTC, Gary Willoughby wrote: If I have a pointer and iterate over it using a slice, like this: T* foo = foreach (element; foo[0 .. length]) { ...

Is there any overhead iterating over a pointer using a slice?

2016-05-31 Thread Gary Willoughby via Digitalmars-d-learn
In relation to this thread: http://forum.dlang.org/thread/ddckhvcxlyuvuiyaz...@forum.dlang.org Where I asked about slicing a pointer, I have another question: If I have a pointer and iterate over it using a slice, like this: T* foo = foreach (element; foo[0 .. length])

Re: How to hash any type to an integer?

2016-05-30 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 29 May 2016 at 16:26:58 UTC, Seb wrote: On Sunday, 29 May 2016 at 11:05:21 UTC, Gary Willoughby wrote: I'm currently implementing a hash map as an exercise and wondered if there is a built-in function I could use to hash keys effectively? What I'm looking for is a function that

How to hash any type to an integer?

2016-05-29 Thread Gary Willoughby via Digitalmars-d-learn
I'm currently implementing a hash map as an exercise and wondered if there is a built-in function I could use to hash keys effectively? What I'm looking for is a function that hashes any variable (of any type) to an integer. I've been looking at the `getHash` function of the `TypeInfo` class

Re: Is there a way to clear an OutBuffer?

2016-05-25 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 23 May 2016 at 03:03:12 UTC, Jon Degenhardt wrote: Currently not possible. Enhancement request perhaps? Looking at the implementation, setting its 'offset' member seems to work. Based on example from documentation: import std.outbuffer; void main() { OutBuffer b = new

Re: Is there an easy way to convert a pointer to malloc'd memory to an array?

2016-05-24 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 24 May 2016 at 18:43:22 UTC, Adam D. Ruppe wrote: On Tuesday, 24 May 2016 at 18:42:41 UTC, Gary Willoughby wrote: I have a T* pointer to the start of a malloc'd chunk of memory, the type T and the number of T's stored in the chunk. Is there an efficient way of converting this

Is there an easy way to convert a pointer to malloc'd memory to an array?

2016-05-24 Thread Gary Willoughby via Digitalmars-d-learn
I have a T* pointer to the start of a malloc'd chunk of memory, the type T and the number of T's stored in the chunk. Is there an efficient way of converting this information to a D array of type T[] or even T[n]?

Re: D equivalent of C++ bind ?

2016-05-10 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 10 May 2016 at 09:39:53 UTC, chmike wrote: Is there an equivalent in D of the C++11 std.bind template class See http://dlang.org/phobos/std_functional.html#partial

Re: Accepting function or delegate as function argument

2016-05-06 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 4 May 2016 at 15:23:20 UTC, Rene Zwanenburg wrote: On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote: Two constructors, one accepting a function and the other one accepting a delegate would do the job for the API. Is there a simple method to convert a function pointer into a

Re: D GUI Toolkit Comparison

2016-04-29 Thread Gary Willoughby via Digitalmars-d-learn
On Friday, 29 April 2016 at 13:52:59 UTC, Nordlöw wrote: Could somebody briefly outline the different GUI toolkits available in D and how they differ especially in terms of cleverly the make use of all idioms available in the language aswell as in Phobos. For instance: DlangUI and Adams D

Re: Three Cool Things about D?

2016-03-02 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 1 March 2016 at 20:26:36 UTC, Ali Çehreli wrote: On 03/01/2016 12:17 PM, Wulfrick wrote: On Tuesday, 1 March 2016 at 20:15:00 UTC, Wulfrick wrote: It looks like the link in wiki.dlang.org/Videos to Andrei's "Three Cool Things about D" is dead. Do you know of another link? Maybe

Re: Printing a C "string" with write(f)ln

2016-02-09 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 9 February 2016 at 12:50:27 UTC, Jakob Ovrum wrote: writefln et al sensibly does *not* assume that a pointer to char is a C string, for memory safety purposes. Print the result of std.string.fromStringz[1] instead: writeln(fromStringz(pString)); writefln("%s",

Re: Printing a C "string" with write(f)ln

2016-02-09 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 9 February 2016 at 16:58:03 UTC, Daniel Kozak wrote: Or use `to` like this: import std.conv; writefln("%s", pString.to!(string)); this will allocate new string which can be performance problem. Which is good in most cases. It's better to have the GC take care of the D string

Re: Printing a C "string" with write(f)ln

2016-02-09 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 9 February 2016 at 17:02:28 UTC, Jakob Ovrum wrote: to!string behaving like that was a poor design choice[1]. Please use fromStringz. [1] https://github.com/D-Programming-Language/phobos/pull/1607 It's not a poor design choice. It ensures the string is handled by the D GC

Re: How do you check if object o has base type B?

2016-02-04 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 4 February 2016 at 05:51:22 UTC, Enjoys Math wrote: Consider: class C { } class B : C { } class A : B { } class D : C { } C[] objList; how do we test if objLis[k] is of base type "B"? Ie for [new A(), new B(), new D(), new C()] would give output [true, true, false,

Re: Call D from TCL

2016-02-03 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 3 February 2016 at 12:20:42 UTC, Vasileios Anagnostopoulos wrote: Is there any example,framework or tutorial on how to call D from Tcl (write new commands in D for Tcl)? I am on Windows 10 x86_64. thank you. I've created a wrapper around Tcl/Tk to create GUI's here:

Re: Call D from TCL

2016-02-03 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 3 February 2016 at 14:19:32 UTC, Vasileios Anagnostopoulos wrote: Thank you very much. I investigate Tcl_CmdProc more closely. On Wed, Feb 3, 2016 at 3:50 PM, Gary Willoughby via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: On Wednesday, 3 Februar

Re: What is the best declaration type for a string like parameter?

2016-01-28 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 28 January 2016 at 15:10:38 UTC, Adam D. Ruppe wrote: On Thursday, 28 January 2016 at 13:36:46 UTC, Puming wrote: I searched the forum and found that people use `const(char)[]` or `in char[]` to accept both string and char[] arguments. There's also the hyper-generic signatures

Re: How can i track the GC when it's runing?

2016-01-25 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 25 January 2016 at 09:33:15 UTC, Dsby wrote: I want to know How can i track the GC when it's runing? And Which algorithm is D's GC used,only Scan-Mark? There is a good resource here: https://dlang.org/spec/garbage.html It details compiler flags to use to profile and log the GC

Re: [Dlang] Delegate Syntax Question

2016-01-10 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 10 January 2016 at 14:32:02 UTC, Jack wrote: ... Just to make your code a little more clear, try using aliases when defining delegate parameters. Like this: alias Action = void delegate(); Then in your code you use the alias, like this: class Bar() { private Action _action;

Re: GC greediness

2016-01-05 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 5 January 2016 at 17:20:07 UTC, Justin Whear wrote: On Tue, 05 Jan 2016 16:07:36 +, Jack Applegame wrote: On a server with 4GB of RAM our D application consumes about 1GB. Today we have increased server memory to 6 Gb and the same application under the same conditions began

Re: A few range questions

2016-01-05 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 5 January 2016 at 18:47:30 UTC, Charles Smith wrote: 1. `arr[].sort` is changing arr in place. Any way to not do that? Use this instead: auto result = sort(arr[].dup); .dup duplicates the array and sort(...) uses the std.algorithm sort and not the built-in array sort method.

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 19:24:46 UTC, TheDGuy wrote: On Sunday, 3 January 2016 at 13:25:04 UTC, Gary Willoughby wrote: On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote: I think I've noticed one problem with the code above. You are using `text.ptr`. You shouldn't do that

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 12:30:34 UTC, TheDGuy wrote: I get an access violation with this code: ... There are a few things you can do to improve your code to make it easier to debug. 1. When converting a D string to a char pointer for use with C, use `std.string.toStringz`:

Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote: I think I've noticed one problem with the code above. You are using `text.ptr`. You shouldn't do that because you are passing a pointer not an array. Just use `text`. Forget this line, my mistake. Use `toStringz` and pass a

Re: Is it possible to elegantly create a range over a binary heap?

2015-12-28 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 27 December 2015 at 22:42:21 UTC, Ivan Kazmenko wrote: If you implement a struct with range primitives over it, you can use it as a range. See the second code example in std.container.binaryheap's docs at http://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap. Or do

Re: Is it possible to elegantly create a range over a binary heap?

2015-12-28 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 28 December 2015 at 14:05:42 UTC, Ivan Kazmenko wrote: 1. You can find maximum, then second maximum, then third maximum and so on - each in constant memory and linear time. So, if performance is somehow not an issue, there is a way to do it @nogc but in N^2 operations. That's

Is it possible to elegantly create a range over a binary heap?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
I have a binary tree storing ints implemented using an array. The internal state looks like this: 8,7,6,4,1,3,5,2 When extracting this data, it is returned as 8,7,6,5,4,3,2,1. Is it possible to elegantly add a range on top of the internal state to return the correct value order I would

Is this an rvalue reference problem?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
See the following code: import std.stdio; void foo(ref int x) { writefln("%s", x); } void main(string[] args) { int y = 0; foo(y++); } When compiled it produces this error: test.d(11): Error: function test.foo (ref int x) is not callable using argument types (int)

Re: Is it possible to elegantly create a range over a binary heap?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 27 December 2015 at 17:23:35 UTC, Gary Willoughby wrote: I have a binary tree storing ints implemented using an array. The internal state looks like this: 8,7,6,4,1,3,5,2 When extracting this data, it is returned as 8,7,6,5,4,3,2,1. Is it possible to elegantly add a range on top

Re: Is this an rvalue reference problem?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 27 December 2015 at 18:54:25 UTC, Adam D. Ruppe wrote: On Sunday, 27 December 2015 at 16:05:39 UTC, Gary Willoughby wrote: [...] Yes, but more than that, what, exactly, would you expect from that? The order of operations with the postfix ++ operator and ref would probably lead to

Re: TreeViews in tkd

2015-12-17 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 17 December 2015 at 09:47:42 UTC, TheGag96 wrote: I've been trying to get into tkd to make some GUI apps, since it looked like the simplest/intuitive library out there so far. I've been attempting to use their TreeViews to make interactable lists of things, but it almost looks

Re: How to use readText to read utf16 file?

2015-11-17 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 17 November 2015 at 02:40:14 UTC, Domain wrote: How to use readText to read utf16 file? Or other encoding file. Here's a helpful resource when working with text files in D. http://nomad.so/2015/09/working-with-files-in-the-d-programming-language/

Re: @property

2015-11-10 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 9 November 2015 at 22:42:16 UTC, Fyodor Ustinov wrote: If this feature will be removed, it will be very lacking code, like: writeln = "Hello, world!"; :) WBR, Fyodor. The feature is not being removed. Only the @property attribute and compiler check is being removed.

Re: When a variable is passed into a function, is its name kept somewhere for the function to acceess

2015-11-10 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 10 November 2015 at 14:14:33 UTC, DlangLearner wrote: Please enlighten me if this can be done, thanks. If i understand you, you could use a templated function: import std.stdio; void foo(alias a)() { writefln("%s was passed in.", a.stringof); } void main(string[] args) {

Re: Associative array with duplicated keys?

2015-11-05 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 5 November 2015 at 10:04:02 UTC, Andrea Fontana wrote: Anyway: are duplicated keys on declaration allowed? IMHO This should at least be a warning.

Re: Capturing __FILE__ and __LINE in a variadic templated function

2015-11-02 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 2 November 2015 at 08:23:16 UTC, Nordlöw wrote: I need `T` to be an alias in order for .stringof to work. typeof(T).stringof

  1   2   3   >