Re: formatting a float or double in a string with all significant digits kept

2019-10-08 Thread berni44 via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 20:37:03 UTC, dan wrote:
But i would like to be able to do this without knowing the 
expansion of pi, or writing too much code, especially if 
there's some d function like writeAllDigits or something 
similar.


You can use the property .dig to get the number of significant 
digits of a number:


writeln(PI.dig); // => 18

You still need to account for the numbers before the dot. If 
you're happy with scientific notation you can do:


auto t = format("%.*e", PI.dig, PI);
writeln("PI = ",t);

(By the way, you can shortcut that with writefln!"PI = 
%.*e"(PI.dig, PI);)


If you don't want to use scientific notation, you probably need 
to do some calculations to find out about the number of digits 
before the dot (you need abs to make it work for negative numbers 
too):


import std.conv: to;
auto x = to!int(log10(abs(PI)));
writefln!"%*.*f"(x,PI.dig-x,PI);



Re: Blog Post #77: Notebook, Part I

2019-10-08 Thread GreatSam4sure via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 10:00:00 UTC, Ron Tarrant wrote:
Today starts a new series on the Notebook widget. Over the next 
few weeks, we'll dig in deep, looking at single-tab and 
multiple-tab demos, customizing the look of the actual tabs, 
adding and removing tabs... a whole ton of stuff. Sounds like 
fun, right?


Come on over and check it out: 
https://gtkdcoding.com/2019/10/08/0077-notebook-i-basics.html


Good works

Is there any way to detect the size of my screen using gtkd? So 
that incan calculate the size of my screen and center my window 
on the screen using move(x, y).


Besides can you convert all your posts to a pdf that is easily 
downloadable for those with little internet access?


Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 8, 2019 2:42:22 PM MDT mipri via Digitalmars-d-learn 
wrote:
> On Tuesday, 8 October 2019 at 10:48:45 UTC, Jonathan M Davis
>
> wrote:
> > The result of this is that code like
> >
> > stack.popBack();
> > stack ~= foo;
> > stack ~= bar;
> > stack.popBack();
> > stack ~= baz;
> >
> > will end up allocating all over the place. Every time you
> > append to the array after shrinking it, you're going to end up
> > with the GC allocating a new block of memory instead of
> > appending in place.
>
> Thanks as well! I thought the code I posted would only allocate
> when
> the array ran out of capacity. And I see how, even if that's
> worked
> around with assumeSafeAppend, it becomes a bad idea to define the
> stack functions against `ref T[]` as this makes it too easy for
> other
> code to slice the array and cause the bugs that assumeSafeAppend
> allows.

Technically, it only does allocate when it runs out of capacity. However, if
you do something like

arr = arr[0 .. $ - 1];

then arr.capacity will either be the length of the array or 0 (I don't
remember which off the top of my head), because the capacity is calculated
based on how much space is available after the end of the dynamic array. How
much space is available at the end of the memory block is irrelevant unless
the dynamic array includes the last element that any dynamic array has ever
had within that memory block. If it's at the end, and the capacity is
greater than the length of the array, then the array can expand in place (up
to the difference between the length and the capacity). But if there is no
extra room on the end, or if the current dynamic array is not at the end,
then the capacity will reflect that. The same goes if the dynamic array is
actually a slice of memory that wasn't allocated by the GC for dynamic
arrays. IIRC, a dynamic array which is a slice of malloc-ed memory will
always give a capacity of 0, but regardless of whether it's actually 0, it's
never more than the length of the dynamic array, because there is no extra
capacity to grow into, since the memory was not allocated by the GC for use
by a dynamic array.

All of the various dynamic array functions work the same regardless of what
kind of memory is backing the dynamic array. It's just that if the memory
wasn't allocated by the GC for dynamic arrays, then when you call the
dynamic array function or property, then the GC treats it the same as it
would treat a dynamic array that referred to the end of the block of memory
(and thus wouldn't have any extra capacity).

You should always be able to call capacity on a dynamic array and see
whether appending to would then result in a reallocation or not.

Either way, because assumeSafeAppend resets the metadata so that the dynamic
array it's given is considered to be the farthest dynamic array within the
block of memory, after calling assumeSafeAppend, capacity will reflect that.

- Jonathan M Davis





Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Oct 08, 2019 at 08:42:22PM +, mipri via Digitalmars-d-learn wrote:
> On Tuesday, 8 October 2019 at 10:48:45 UTC, Jonathan M Davis wrote:
> > The result of this is that code like
> > 
> > stack.popBack();
> > stack ~= foo;
> > stack ~= bar;
> > stack.popBack();
> > stack ~= baz;
> > 
> > will end up allocating all over the place. Every time you
> > append to the array after shrinking it, you're going to end up
> > with the GC allocating a new block of memory instead of
> > appending in place.
> > 
> 
> Thanks as well! I thought the code I posted would only allocate when
> the array ran out of capacity. And I see how, even if that's worked
> around with assumeSafeAppend, it becomes a bad idea to define the
> stack functions against `ref T[]` as this makes it too easy for other
> code to slice the array and cause the bugs that assumeSafeAppend
> allows.

IMO, the best way to implement a stack using built-in arrays is to wrap
the array inside a struct that separately keeps track of the last
element. Something like this:

// Warning: untested code
struct Stack(T) {
private T[] impl;
private size_t idx;
...
void push(T t) {
if (impl.length <= idx)
impl.length = ...; //expand by some amount
impl[idx++] = t;
}
T pop() in(idx > 0) {
// ... optional: reduce impl.length if idx /
// .length ratio smalle than some threshold.

return impl[idx--];
}
}


T

-- 
LINUX = Lousy Interface for Nefarious Unix Xenophobes.


Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread mipri via Digitalmars-d-learn
On Tuesday, 8 October 2019 at 10:48:45 UTC, Jonathan M Davis 
wrote:

The result of this is that code like

stack.popBack();
stack ~= foo;
stack ~= bar;
stack.popBack();
stack ~= baz;

will end up allocating all over the place. Every time you
append to the array after shrinking it, you're going to end up
with the GC allocating a new block of memory instead of
appending in place.



Thanks as well! I thought the code I posted would only allocate 
when
the array ran out of capacity. And I see how, even if that's 
worked

around with assumeSafeAppend, it becomes a bad idea to define the
stack functions against `ref T[]` as this makes it too easy for 
other

code to slice the array and cause the bugs that assumeSafeAppend
allows.


formatting a float or double in a string with all significant digits kept

2019-10-08 Thread dan via Digitalmars-d-learn
I have a double precision number that i would like to print all 
significant digits of, but no more than what are actually present 
in the number.  Or more exactly, i want to print the minimum 
number of digits necessary to recover the original number to 
within 2 or 3 least significant bits in the stored, in-core, 
version of its bit pattern.


For example,


import std.string;
import std.stdio;
import std.math;

void main( ) {
  auto t = format("%3.30f", PI );
  writeln("Value of PI is: ", PI, " or, : ", t);
}

The default way writeln prints is 5 digits to the right of the 
decimal point.


I can format to print with any number of digits, such as 30 
above, but that's too many.


For pi, the correct number of digits to print looks to be about 
18 (and the extra 12 digits presumably are from the decimal 
expansion of the least significant bit?).


But i would like to be able to do this without knowing the 
expansion of pi, or writing too much code, especially if there's 
some d function like writeAllDigits or something similar.


Thanks in advance for any pointers!

dan


Re: On D's garbage collection

2019-10-08 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 16:28:51 UTC, Marcel wrote:
I'm been thinking about using D in conjunction with C11 to 
develop a set of applications with hard real-time requirements. 
While initially the goal was to use C++ instead, it has become 
clear that D's introspection facilities will offer significant 
advantages. Unfortunately, the project will heavily rely on 
custom memory allocators written in C, so the presence of 
garbage collection in the language is a problem. While I'm 
aware that the nogc attribute exists, I haven't actually seen a 
way to apply it to a whole project. Is this possible?


Do you want to write D code that just doesn't use the GC or the 
whole runtime?


If the former then use @nogc at the entry point of your D code 
(This means that - say - main cannot call anything non-@nogc and 
therefore guarantees the program is @nogc), if the latter then 
use -betterC


IMO, if the interface to your memory allocators is stable then 
just link with them and write the whole thing in D (Interfacing 
with C is a solved problem but C is just awful compared to the 
features you get for free in D)


Re: On D's garbage collection

2019-10-08 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Tuesday, 8 October 2019 at 16:43:23 UTC, Ferhat Kurtulmuş 
wrote:

On Tuesday, 8 October 2019 at 16:28:51 UTC, Marcel wrote:
I'm been thinking about using D in conjunction with C11 to 
develop a set of applications with hard real-time 
requirements. While initially the goal was to use C++ instead, 
it has become clear that D's introspection facilities will 
offer significant advantages. Unfortunately, the project will 
heavily rely on custom memory allocators written in C, so the 
presence of garbage collection in the language is a problem. 
While I'm aware that the nogc attribute exists, I haven't 
actually seen a way to apply it to a whole project. Is this 
possible?


I think you may find this interesting: 
https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime.html


And the code which is actually working: 
https://github.com/AuburnSounds/Dplug/blob/master/core/dplug/core/nogc.d





Re: On D's garbage collection

2019-10-08 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 16:28:51 UTC, Marcel wrote:
I'm been thinking about using D in conjunction with C11 to 
develop a set of applications with hard real-time requirements. 
While initially the goal was to use C++ instead, it has become 
clear that D's introspection facilities will offer significant 
advantages. Unfortunately, the project will heavily rely on 
custom memory allocators written in C, so the presence of 
garbage collection in the language is a problem. While I'm 
aware that the nogc attribute exists, I haven't actually seen a 
way to apply it to a whole project. Is this possible?


I think you may find this interesting: 
https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime.html




On D's garbage collection

2019-10-08 Thread Marcel via Digitalmars-d-learn
I'm been thinking about using D in conjunction with C11 to 
develop a set of applications with hard real-time requirements. 
While initially the goal was to use C++ instead, it has become 
clear that D's introspection facilities will offer significant 
advantages. Unfortunately, the project will heavily rely on 
custom memory allocators written in C, so the presence of garbage 
collection in the language is a problem. While I'm aware that the 
nogc attribute exists, I haven't actually seen a way to apply it 
to a whole project. Is this possible?


Re: Ranges to deal with corner cases and "random access"

2019-10-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 8, 2019 9:40:33 AM MDT Paul Backus via Digitalmars-d-
learn wrote:
> On Sunday, 6 October 2019 at 20:34:55 UTC, Brett wrote:
> > If it can be done and make to work well with ranges it would
> > allow many algorithms to be very easily expressed and make
> > ranges more powerful.
>
> You can make it a range by adding an "alias this" to the original
> array:
>
> struct ExtendedArray(T)
> {
>  T[] a;
>  alias a this;
>
>  T opIndex(int i)
>  {
>  if (i < 0) return a[0];
>  else if (i >= a.length) return a[$-1];
>  else return a[i];
>  }
> }
>
> Full example: https://run.dlang.io/is/2x6LKD

It would be less error-prone to just implement the appropriate functions on
the struct. alias this and generic code are a terrible combination. It's way
too easy for a type to pass a template constraint thanks to alias this and
then have trouble because it passed based on the implicit conversion, but
the conversion wasn't forced in the code using the type. You can get some
really subtle problems if the code converts to the alias in some cases but
not in others.

- Jonathan M Davis





Re: Ranges to deal with corner cases and "random access"

2019-10-08 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 6 October 2019 at 20:34:55 UTC, Brett wrote:
If it can be done and make to work well with ranges it would 
allow many algorithms to be very easily expressed and make 
ranges more powerful.


You can make it a range by adding an "alias this" to the original 
array:


struct ExtendedArray(T)
{
T[] a;
alias a this;

T opIndex(int i)
{
if (i < 0) return a[0];
else if (i >= a.length) return a[$-1];
else return a[i];
}
}

Full example: https://run.dlang.io/is/2x6LKD


Re: DUB mysql-native

2019-10-08 Thread Daniel Kozak via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 14:19:10 UTC, Vino wrote:

On Tuesday, 8 October 2019 at 14:02:03 UTC, Vino wrote:

On Tuesday, 8 October 2019 at 11:26:24 UTC, Daniel Kozak wrote:

[...]


Hi Daniel,

   As stated I have removed those line and ran the below 
command


[...]


Hi Daniel,

  Was able to resolve the dub test issue using the you tube 
video "https://www.youtube.com/watch?v=zvrf7nlq5Og;, but not 
able to execute the code


Code Execution
cd HUB/source
app.d(3): Error: module `mysql` is in file 'mysql.d' which 
cannot be read

import path[0] = .
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", 
"-o-","app.d", "-I."]


From,
Vino.B


You can not use rdmd on app.d with dependencies. You have to use 
dub. I am not sure what are you trying to do, but if you are able 
to build your app with dub, than you just can run it ./hub


Re: DUB mysql-native

2019-10-08 Thread Vino via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 14:02:03 UTC, Vino wrote:

On Tuesday, 8 October 2019 at 11:26:24 UTC, Daniel Kozak wrote:

[...]


Hi Daniel,

   As stated I have removed those line and ran the below command

[...]


Hi Daniel,

  Was able to resolve the dub test issue using the you tube video 
"https://www.youtube.com/watch?v=zvrf7nlq5Og;, but not able to 
execute the code


Code Execution
cd HUB/source
app.d(3): Error: module `mysql` is in file 'mysql.d' which cannot 
be read

import path[0] = .
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", 
"-o-","app.d", "-I."]


From,
Vino.B


Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread Just Dave via Digitalmars-d-learn
Thanks for the advice. I used a quick and dirty range solution as 
was suggested. It allowed me to move on as I really wasn't 
looking to fully implement a queue or stack. Just get something 
that semantically behaved as such. I'll return later and optimize 
it with the later suggestions if it's a major issue.


Re: DUB mysql-native

2019-10-08 Thread Vino via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 11:26:24 UTC, Daniel Kozak wrote:
On Tue, Oct 8, 2019 at 1:15 PM Daniel Kozak  
wrote:

...
It is known issue, you can not use mysql-native right now with
anything else than libevent

https://github.com/mysql-d/mysql-native/issues/199

Or you can just remove

"subConfigurations": {
"vibe-d:core": "libevent"
},

from ~/.dub/packages/mysql-native-2.3.0/mysql-native/dub.json


https://github.com/mysql-d/mysql-native/commit/ec851956e579bab8d00b1ae89a5344a2babce2e3


Hi Daniel,

   As stated I have removed those line and ran the below command

dub build --force - No issue
dub test - failed with the below error

dub build --force
Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe for 
x86_64.

dub 1.17.0: building configuration "library"...
Serializing composite type BuildRequirements which has no 
serializable fields
Serializing composite type BuildOptions which has no serializable 
fields

taggedalgebraic 0.11.4: building configuration "library"...
eventcore 0.8.46: building configuration "winapi"...
stdx-allocator 2.77.5: building configuration "library"...
vibe-core 1.7.0: building configuration "winapi"...
vibe-d:utils 0.8.6: building configuration "library"...
vibe-d:data 0.8.6: building configuration "library"...
mysql-native 2.3.0: building configuration "library"...
..\..\..\AppData\Local\dub\packages\mysql-native-2.3.0\mysql-native\source\mysql\connection.d(1192,27):
 Deprecation: function std.typecons.Nullable!(PreparedServerInfo).Nullable.get_ 
is deprecated - Implicit conversion with alias Nullable.get this will be 
removed after 2.096. Please use .get explicitly.
..\..\..\AppData\Local\dub\packages\mysql-native-2.3.0\mysql-native\source\mysql\pool.d(115,13):
 Deprecation: constructor 
vibe.core.connectionpool.ConnectionPool!(Connection).ConnectionPool.this is 
deprecated - Use an @safe callback instead
..\..\..\AppData\Local\dub\packages\mysql-native-2.3.0\mysql-native\source\mysql\pool.d(361,27):
 Deprecation: function std.typecons.Nullable!(PreparedInfo).Nullable.get_ is 
deprecated - Implicit conversion with alias Nullable.get this will be removed 
after 2.096. Please use .get explicitly.
..\..\..\AppData\Local\dub\packages\mysql-native-2.3.0\mysql-native\source\mysql\pool.d(378,26):
 Deprecation: function std.typecons.Nullable!(PreparedInfo).Nullable.get_ is 
deprecated - Implicit conversion with alias Nullable.get this will be removed 
after 2.096. Please use .get explicitly.
..\..\..\AppData\Local\dub\packages\mysql-native-2.3.0\mysql-native\source\mysql\protocol\comms.d(418,3):
 Deprecation: foreach: loop index implicitly converted from size_t to ushort
hub ~master: building configuration "application"...
Linking...
Copying files for dub...

dub test
No source files found in configuration 'library'. Falling back to 
"dub -b unittest".
Performing "unittest" build using C:\D\dmd2\windows\bin\dmd.exe 
for x86_64.

dub 1.17.0: target for configuration "library" is up to date.
taggedalgebraic 0.11.4: target for configuration "library" is up 
to date.

eventcore 0.8.46: target for configuration "winapi" is up to date.
stdx-allocator 2.77.5: target for configuration "library" is up 
to date.

vibe-core 1.7.0: target for configuration "winapi" is up to date.
vibe-d:utils 0.8.6: target for configuration "library" is up to 
date.
vibe-d:data 0.8.6: target for configuration "library" is up to 
date.
mysql-native 2.3.0: target for configuration "library" is up to 
date.

hub ~master: target for configuration "application" is up to date.
To force a rebuild of up-to-date targets, run again with --force.
Copying files for dub...
Running .\hub.exe
Program exited with code -1073741515

Error:
The code execution cannot proceed because msvcr100.dll was not 
found. Reinstalling the program  may fix this problem.


Execution HUB/source/app.d
import std.array : array;
import std.variant;
import mysql;
import std.stdio;

void main(string[] args)
{
	auto connectionStr = 
"host=dev.server1.com;port=3910;user=testuser;pwd=#;db=test";

if(args.length > 1)
connectionStr = args[1];
Connection conn = new Connection(connectionStr);
scope(exit) conn.close();

	ResultRange range = conn.query("SELECT host_name FROM 
`devservers`");

Row row = range.front;
Variant host_name = row[0];
writeln(host_name);
}

Code Execution
cd HUB/source
rdmd app.d

app.d(3): Error: module `mysql` is in file 'mysql.d' which cannot 
be read

import path[0] = .
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", "-o-", 
"app.d", "-I."]


From,
Vino.B



Re: DUB mysql-native

2019-10-08 Thread Daniel Kozak via Digitalmars-d-learn
On Tue, Oct 8, 2019 at 1:15 PM Daniel Kozak  wrote:
> ...
> It is known issue, you can not use mysql-native right now with
> anything else than libevent
>
> https://github.com/mysql-d/mysql-native/issues/199
>
> Or you can just remove
>
> "subConfigurations": {
> "vibe-d:core": "libevent"
> },
>
> from ~/.dub/packages/mysql-native-2.3.0/mysql-native/dub.json

https://github.com/mysql-d/mysql-native/commit/ec851956e579bab8d00b1ae89a5344a2babce2e3


Re: DUB mysql-native

2019-10-08 Thread Daniel Kozak via Digitalmars-d-learn
On Tue, Oct 8, 2019 at 10:55 AM Vino via Digitalmars-d-learn
 wrote:
>
> Could not resolve configuration for package hub
>
> From,
> Vino.B

It is known issue, you can not use mysql-native right now with
anything else than libevent

https://github.com/mysql-d/mysql-native/issues/199

Or you can just remove

"subConfigurations": {
"vibe-d:core": "libevent"
},

from ~/.dub/packages/mysql-native-2.3.0/mysql-native/dub.json


Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, October 7, 2019 1:16:31 PM MDT IGotD- via Digitalmars-d-learn 
wrote:
> On Monday, 7 October 2019 at 17:36:09 UTC, Ferhat Kurtulmuş wrote:
> >> I'm not talking about memory deletion. I'm talking about push,
> >> pop, enqueue, and dequeue behavior. I'd assume in a garbage
> >> collected language letting the reference float off should be
> >> picked up by the GC.
> >
> > I'm sorry. Writing on my mobile phone. Maybe this is what you
> > are looking for
> > https://dlang.org/phobos/std_container_dlist.html
>
> I think what he is looking for are the general pop_front,
> push_front, pop_back and push_back that you would find in
> virtually any C++ STL container algorithms like list, vector or
> map.
>
> I think this is a good question as I don't really see any good
> example in the documentation of the dynamic arrays about this.
> This is very common use case for arrays. Is there any D
> equivalent?

Pushing and popping like you'd do with a stack or queue can certainly be
done with a dynamic array, but D's dynamic arrays don't work very well that
way without wrapping them. Shrinking them is easy enough. You just slice the
dynamic array to get another dynamic array which is a slice referring just
to the elements that were sliced. e.g.

auto arr2 = arr1[5 .. $];
assert(arr2.length == arr1.length - 5);
assert(arr2 == arr1[5 .. $]);

If you just want to pop off the first element, you can even use popFront
from std.range.primitives. e.g.

auto arr2 = arr1;
arr2.popFront();
assert(arr2 == arr1[1 .. $]);

Similarly, you could use popBack to pop off the last element. e.g.

auto arr2 = arr1;
arr2.popBack();
assert(arr2 == arr1[0 .. $ - 1]);

~= can be used to append to a dynamic array, but then we start getting into
some issues.

When a dynamic array is appended to, the GC determines whether it has the
capacity to put that element one past the end of that dynamic array within
the block of memory that that dynamic array is a slice of. If the dynamic
array is not a slice of GC-allocated memory for dynamic arrays, then the GC
determines that it can't append in place, so it allocates a new block of
memory, copies the elements to that new block of memory (including the
appendend elements), and then mutates that dynamic array to point to the new
block of memory. Similarly, if the dynamic array refers to a GC-allocated
block of memory with no extra space on the end, the GC will allocate a new
block of memory, copy the elements over, and mutate the dynamic array to
point to the new block of memory. On the other hand, if the memory block was
allocated by the GC for dynamic arrays, and it does have space beyond the
end of that dynamic array, then it will just append in place and adjust the
length member of the dynamic array accordingly. However, the real kicker for
this particular use case is what happens when the dynamic array's last
element is not the last element used within the memory block. e.g.

arr2 = arr1[0 .. $ - 1];
arr2 ~= 42;

or even

arr1 = arr1[0 .. $ - 1];
arr1 ~= 42;

Both of those examples will result in a new block of memory being allocated
when the dynamic array is appended to. That's because the GC is written to
avoid appending into another dynamic array which refers to the same block of
memory. In order for a dynamic array to have capacity to expand into, no
other dynamic array can ever have had any elements beyond the end of that
dynamic array (the metadata keeps track of the farthest that any array has
expanded into the block of memory). Even if there are currently no other
dynamic arrays which refer to that element, it doesn't matter. All the GC
cares about is whether any dynamic array has ever expanded that far into the
memory block.

The result of this is that code like

stack.popBack();
stack ~= foo;
stack ~= bar;
stack.popBack();
stack ~= baz;

will end up allocating all over the place. Every time you append to the
array after shrinking it, you're going to end up with the GC allocating a
new block of memory instead of appending in place.

The solution to this problem is to use the function assumeSafeAppend
(it's in object.d, so it's always available). e.g.

stack.popBack();
stack.assumeSafeAppend();
stack ~= foo;
stack ~= bar;
stack.popBack();
stack.assumeSafeAppend();
stack ~= baz;

This tells the GC to reset the metadata for the block of memory that that
dynamic array is a slice of so that however far the last element of that
dynamic array is is considered to be the last element curently used in the
memory block. That way, when you append to it, it won't think that there are
any other dynamic arrays using that memory, and it will expand the array in
place.

The problem with this (and the reason that assumeSafeAppend is @system) is
that if you ever use it when there are still other dynamic arrays in use
which are slices of that same block of memory and which refer to elements
beyond the end of the dynamic array that you call assumeSafeAppend on,
you're going to get some subtle and nasty 

Blog Post #77: Notebook, Part I

2019-10-08 Thread Ron Tarrant via Digitalmars-d-learn
Today starts a new series on the Notebook widget. Over the next 
few weeks, we'll dig in deep, looking at single-tab and 
multiple-tab demos, customizing the look of the actual tabs, 
adding and removing tabs... a whole ton of stuff. Sounds like 
fun, right?


Come on over and check it out: 
https://gtkdcoding.com/2019/10/08/0077-notebook-i-basics.html


DUB mysql-native

2019-10-08 Thread Vino via Digitalmars-d-learn

Hi All,

 Please correct me what is wrong as I am not able to build a 
project using dub getting the error "Could not resolve 
configuration for package hub".


dub init HUB
C:\Users\userp\Downloads\testhub>dub init HUB
Package recipe format (sdl/json) [json]:
Name [hub]:
Description [A minimal D application.]:
Author name [userp]:
License [proprietary]:
Copyright string [Copyright © 2019, userp]:
Add dependency (leave empty to skip) []:
Successfully created an empty project in 
'C:\Users\userp\Downloads\testhub\HUB'.

Package successfully created in HUB

cd HUB
dub add dub
Adding dependency dub ~>1.17.0

dub add mysql-native
Adding dependency mysql-native ~>2.3.0

dub build
Fetching libevent 2.0.2+2.0.16 (getting selected version)...
Fetching vibe-core 1.7.0 (getting selected version)...
Fetching taggedalgebraic 0.11.4 (getting selected version)...
Fetching dub 1.17.0 (getting selected version)...
Fetching vibe-d 0.8.6 (getting selected version)...
Fetching memutils 0.4.13 (getting selected version)...
Fetching stdx-allocator 2.77.5 (getting selected version)...
Fetching unit-threaded 0.7.55 (getting selected version)...
Fetching eventcore 0.8.46 (getting selected version)...
Fetching libasync 0.8.4 (getting selected version)...
Fetching mysql-native 2.3.0 (getting selected version)...
Fetching botan-math 1.0.3 (getting selected version)...
Fetching botan 1.12.10 (getting selected version)...
Fetching diet-ng 1.6.0 (getting selected version)...
Fetching openssl 1.1.6+1.0.1g (getting selected version)...
Fetching mir-linux-kernel 1.0.1 (getting selected version)...
Could not resolve configuration for package hub

From,
Vino.B