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

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

Good day,

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

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

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

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

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

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

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

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

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

Or doing so, will eventually lead to subtle bugs?

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


Thanks.


How would overloading work?

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


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


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



Re: MSVC path on windows

2017-01-12 Thread Ryan via Digitalmars-d-learn

On Friday, 13 January 2017 at 03:21:52 UTC, Jeremy DeHaan wrote:

I've also thought of parsing the PATH to figure out where dmd 
is installed and extract the info from the sc.ini file.


I have both VS2013 and VS2015 installed. I have to use a project 
specific sc.ini file to match each project to the VS 
compiler/linker that was used to generate the C libraries it 
depends on.


The point is, people may have several versions of MSVC installed, 
so I think parsing the sc.ini file would be the safest way to go, 
because it will tell you the linker DMD will ultimately use. The 
other variables will just tell you what is installed. Also, 
depending on the shell you use, those environment variables may 
not be reliably set. Which is why Microsoft provides a few 
shortcuts for the different toolchain's (e.g.32 vs 64 bit) 
terminals.


DMD specify VS version in sc.ini

2016-12-27 Thread Ryan via Digitalmars-d-learn
I have a C library I want to link against that was compiled with 
VS 2013. I have VS2013 and VS2015 installed. I want DMD to use 
the 2013 version, since the C-runtime in 2015 is not backwards 
compatible.


Looking at sc.ini I see several entries that relate to the 
linker, CRT, and SDK. How do I edit these to force the use of VS 
2013?


I think I can figure out most of them except for UCRTVersion.

Thanks,


Re: Catch block not hit in unittest

2016-11-24 Thread Ryan via Digitalmars-d-learn

On Thursday, 24 November 2016 at 13:42:25 UTC, Kagamin wrote:

Linux? Probably another bug.
Try this:
unittest
{
import core.exception : UnicodeException;
void f()
{
string ret;
int i = -1;
ret ~= i;
}

try
{
f();
}
catch(UnicodeException e)
{
assert(e.msg == "Invalid UTF-8 sequence");
}
}


Both the OP's code and this code gives me a segfault on DMD 
2.071.1 on my Mac.


Re: Why double not? (!!)

2016-11-19 Thread Ryan via Digitalmars-d-learn

It's a more concise way of writing:
GetConsoleCP() != 0

You can do this in C/C++ as well (and presumably some other 
languages).


 Hmmm... thinking about it, it does make perfect sense. The 
first ! converts it to bool, the other inverts it back to 
it's positive/negative state.


Wouldn't this just be the same as
auto hasConsole = cast(bool) GetConsoleCP(); ?

I think the GetConsoleCP() != 0 code is the clearest about your 
intentions.





Why double not? (!!)

2016-11-18 Thread Ryan via Digitalmars-d-learn
Why do I see double `not` operators sometimes in D code? An 
example it the last post of this thread.


http://forum.dlang.org/thread/ktlpnikvdwgbvfaam...@forum.dlang.org


import core.sys.windows.windows : GetConsoleCP;
bool hasConsole = !!GetConsoleCP();


Thanks.


Any recent success with SWIG?

2016-10-19 Thread Ryan via Digitalmars-d-learn
I would like to use a library with a c interface from D (gdal 
actually), but I can't find any bindings. I've looked at htod, 
but I also see that gdal has project maintained SWIG interfaces 
and SWIG claims to work with D (both D1 and D2).


So I installed the latest SWIG, but I haven't been able to get 
the SWIG provided D examples to work. Everything compiles, but 
when I run an example I get an Access Violation, it looks like it 
is trying to use a null pointer somewhere.


Has anyone had success with this? Or maybe has SWIG support for D 
fallen behind?


I'm using windows 10 with MSYS2 as my shell environment to 
support the Autotools build files that came with SWIG, which is 
what they recommend on the website for building the examples.


Thanks,


Re: std.paralellism.Task value type problems

2016-10-19 Thread Ryan via Digitalmars-d-learn
On Wednesday, 19 October 2016 at 15:28:25 UTC, Benjamin Thaut 
wrote:
I would like to use std.paralellism.TaskPool to schedule 
various tasks I create. The problem however is that these tasks 
don't have a lifetime which is bound to any function scope I 
have. So I need to create a new task object on the heap and 
push it into a array for bookkeeping. The problem however is 
that std.paralellism.Task is a struct and the only way to 
instanciate it is to use std.paralelism.task which returns it 
as a value. I have no idea at the moment how to allocate a 
instance of std.paralellism.Task on the heap. Any suggestions?


Kind Regards
Benjamin Thaut


Try the link below for the documentation. There are 2 versions of 
task, one returns a struct on the stack, the second returns a 
struct on the GC heap.


https://dlang.org/phobos/std_parallelism.html#.task


Re: Should I brush up on my C before plunging fully into D?

2016-10-15 Thread Ryan via Digitalmars-d-learn

On Saturday, 15 October 2016 at 01:46:52 UTC, Chris Nelson wrote:
I'm mainly a scripting language, .NET, and SQL programmer. I've 
been looking for a good programming language for Linux/BSD 
other than Python. I've surveyed the options and D appears to 
be a sane modern choice for me. (Thanks Ali Çehreli and others!)


The only hitch is that many of the projects and libraries I'm 
interested in using or maybe porting are mainly C based. (My 
overall C-fu is weak...) Should I review a good C book or 
tutorial before jumping in to fully learning D? Or should I 
just eschew any C exposure until I master D?


(As a side note, many of the C libraries I'm interested in seem 
to be confusing messes of header files and "organic" code. But 
who am I to judge?)


I came from a python/java background when I started to learn D. I 
had a programming class many-many years ago in C, and I only knew 
it well enough to do small programs. I started learning D almost 
2 years ago, and then started in on C++ after that.


I think D is by far the easiest compiled language to "learn". You 
can get into to it pretty easy (easier than C++) and write 
something useful pretty quick. Then there's always more to learn.




Really easy optimization with std.experimental.allocator

2016-09-17 Thread Ryan via Digitalmars-d-learn
I've been learning about allocators in D. Much easier than C++, 
and this little program shows a really easy optimization. Just 
use the IAllocator interface with the GC.

---
import std.datetime;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import std.stdio;

enum sizes = [10,100, 1_000, 10_000];

// A function that creates several arrays on the GC heap with 
new, and assigns

// a single value, so it doesn't get compiled out
void GCNew()
{

  byte[] b1;

  foreach(sz; sizes)
  {
b1 = new byte[](sz);
b1[1] = 10;
assert(b1[1] == 10);
assert(b1[0] == 0);
  }
}

// Same as the function above, but with an IAllocator interface
void IAlloc(IAllocator alloc = theAllocator)
{
  byte[] b1;

  foreach(sz; sizes)
  {
b1 = alloc.makeArray!byte(sz);
b1[1] = 10;
assert(b1[1] == 10);
assert(b1[0] == 0);
alloc.dispose(b1);
  }
}

void main()
{
  enum iterations = 1_000;

  writefln("GCNew: %d", 
benchmark!GCNew(iterations)[0].usecs);


  writefln("IAlloc GC: %d", 
benchmark!IAlloc(iterations)[0].usecs);


  writefln("IAlloc malloc: %d", benchmark!(
()
{
  IAlloc(allocatorObject(Mallocator.instance));
})
  (iterations)[0].usecs);
}

---
Results on my iMac with rdmd -release -boundscheck=off -O 
allocTest.d

GCNew: 7467
IAlloc GC: 1657
IAlloc malloc: 1575

I think it works because each time you call dispose it tells the 
GC to mark that memory as available, without the GC needing to do 
a collection sweep. This could be a really useful tip in the 
allocators section, as I see converting to IAllocator with the GC 
as the first step in testing optimizations with allocators.


Re: seg fault, now what?

2016-09-17 Thread Ryan via Digitalmars-d-learn

On Saturday, 17 September 2016 at 22:48:49 UTC, ag0aep6g wrote:

On 09/17/2016 11:58 PM, Ryan wrote:
On Saturday, 17 September 2016 at 21:44:22 UTC, Stefan Koch 
wrote:

[...]

Post the program somewhere otherwise we cannot help.

[... code ...]

Reduced and filed: 
https://issues.dlang.org/show_bug.cgi?id=16506


Wow. Thank you.


Re: seg fault, now what?

2016-09-17 Thread Ryan via Digitalmars-d-learn

On Saturday, 17 September 2016 at 21:44:22 UTC, Stefan Koch wrote:

On Saturday, 17 September 2016 at 21:12:08 UTC, Ryan wrote:

Is there an alternative to reporting bugs via bugzilla?

I tried to open an account, but they recommend not using your 
main e-mail address because it will be posted to the web for 
all the spammers to find. But I don't have another e-mail 
address, and it seems a bit much to create a fake e-mail 
account for a few bug reports.


I also tried to post to the threads about bugs, but they are 
all bugzilla archives.


I've narrowed it down to a pretty simple little program that 
demonstrates the bug, and even tried it on DMD/LDC2, for mac, 
ubuntu, and windows 10.


Thanks,


Post the program somewhere otherwise we cannot help.


import std.experimental.allocator;
import std.experimental.allocator.gc_allocator;
import std.experimental.allocator.mallocator;
import std.experimental.allocator.building_blocks;
import std.stdio;
import std.range;

struct S
{

  // The allocator used for this instance
  private IAllocator myAlloc;

  // My payload
  private double[] _payload;

  // No empty payloads allowed
  @disable this();

  // Create an initialized payload
  this(size_t sz, double init, IAllocator alloc = theAllocator)
  {
// Set the allocator first!
myAlloc = alloc;

_payload = myAlloc.makeArray!double(sz, init);

assert(_payload, "Failed allocation...");
assert(_payload.length == sz, "Something is awry.");

assert(_payload[0] == init, "Copy failed.");
assert(_payload[$-1] == init, "Copy failed.");
  }

  // postblit - deep copy payload - I have an implementation for 
this, but it

  // is not needed to demo seg fault
  @disable this(this);

  // Destructor to return memory
  ~this()
  {
// No way _payload is null
assert(_payload, "_payload is null");

// So give it back, I'm done with it for now
myAlloc.dispose(_payload);
  }

  // Something to do with the object
  void ack()
  {
writeln("ack"); stdout.flush();
  }

  // Lots of other stuff.
  // .
  // .
  // .
}

void main()
{
  // Always works
  foreach(i; iota(1,5_000))
  {
writef("Doing default %d...", i); stdout.flush();
S s = S(i, i*i);
s.ack();
  }

  // Always works
  IAllocator alloc = allocatorObject(GCAllocator.instance);
  foreach(i; iota(1,5_000))
  {
writef("Doing GCAllocator %d...", i); stdout.flush();
S s = S(i, i*i, alloc);
s.ack();
  }

  // Always works
  alloc = allocatorObject(Mallocator.instance);
  foreach(i; iota(1,5_000))
  {
writef("Doing Mallocator %d...", i); stdout.flush();
S s = S(i, i*i, alloc);
s.ack();
  }

  // This one will seg fault on linux/mac/ldc2 win, not dmd 
windows. Why?

  auto ft = FreeTree!Mallocator();
  alloc = allocatorObject();
  foreach(i; iota(1,5_000))
  {
writef("Doing FreeTree!Mallocator %d...", i); stdout.flush();
S s = S(i, i*i, alloc);
s.ack();
  }

  // This one will seg fault on linux/mac/ldc2 win, not dmd 
windows. Why?

  auto ft2 = FreeTree!GCAllocator();
  alloc = allocatorObject();
  foreach(i; iota(1,5_000))
  {
writef("Doing FreeTree!GCAllocator %d...", i); stdout.flush();
S s = S(i, i*i, alloc);
s.ack();
  }
}


seg fault, now what?

2016-09-17 Thread Ryan via Digitalmars-d-learn

Is there an alternative to reporting bugs via bugzilla?

I tried to open an account, but they recommend not using your 
main e-mail address because it will be posted to the web for all 
the spammers to find. But I don't have another e-mail address, 
and it seems a bit much to create a fake e-mail account for a few 
bug reports.


I also tried to post to the threads about bugs, but they are all 
bugzilla archives.


I've narrowed it down to a pretty simple little program that 
demonstrates the bug, and even tried it on DMD/LDC2, for mac, 
ubuntu, and windows 10.


Thanks,


Fast matrix struct

2014-12-14 Thread Ryan via Digitalmars-d-learn
I'm writing a Matrix struct that I plan to use for some number 
crunching, which will be intensive at times. So I want it to be 
fast.


Will I do better by manually managing the memory myself with 
malloc and free in the constructors/destructor or just using 
plain D arrays?


The manual way I could bypass the GC and bounds checks. I don't 
need (or want) any of the fancy appending concatenation and the 
potential slowdowns from the heap allocations that comes with the 
builtin arrays.


But I have to wonder, if I just don't use those features, will it 
be just as fast with the GC (assuming I compile out the bounds 
checks with the -release compiler option)?


Right now my strategy is to malloc one large chunk of memory in 
the constructor (and postblit) and treat the 2-d matrix as a 1-d 
array internally. Then just use pointer arithmetic to access the 
elements.


I know the best way to know is just to test. But writing both 
versions would take a LOT of work, and I'm looking for guidance 
about which path will probably be most fruitful.


Thanks,


Re: Learning D

2014-08-27 Thread Ryan via Digitalmars-d-learn
Thanks again for all the responses.  I've made tremendous 
progress understanding the D build process.


I'm thinking I will probably create a more in depth GTK+ hello 
world that attempts to covers some of the current D landscape.  
For instance I now understand how DMD and RDMD work and how they 
are different from one another. But I do not yet understand how 
to use DUB, or why I would choose it over one of the many other 
build systems such as CMakeD2, DSSS, XfBuild, Bud, RDMD, or 
premake4.  Especially DSSS vs DUB. Is DUB a replacement for DSSS?


I also don't understand why there are multiple code repositories 
and where to find what (http://code.dlang.org vs 
http://www.dsource.org/projects/).  I understand that Open Source 
is decentralized, but when you're talking about a relatively 
small user group like D it seems very confusing.


Last, I've been in the Java/C# world so long that I don't 
understand why I need to provide the source code to statically 
link a *.lib file.  I started exploring dynamic link libraries, 
and this too seems very confusing to me.  Perhaps there is a 
better way to do this?  Perhaps I just don't understand it well 
enough yet?


Next on my list is to attempt to create a Glade hello world in D.

Can I host a tutorial here on dlang.org?  If so does it belong 
under How-tos or Articles?







Learning D

2014-08-25 Thread Ryan via Digitalmars-d-learn

Me: Software developer for 30 years.

So perhaps this is old fashion, but I wanted to start using D by 
whipping together nice little personal utilities.


I tried installing MonoDevelop and Mono-D.  I can't even figure 
out the basics, such as adding references to a project.  There 
are no options in the context menus, and although it looks like 
drag an drop might work (a '+' sign appears by the cursor), 
dropping a file from the filesystem doesn't work either.


Although I dream of someday being able to add a reference to a 
project, I'm not really sure what I might drag in.  I managed to 
download and compile GtkD, since it seems like a GUI would be a 
nice place to start (again, old fashion).  I got three *.lib 
files out of it... H... Maybe these are references??


I had installed the Visual Studio plugin, but I don't want to use 
this since I would like to eventually migrate away from Windows.


Let me cut to the chase.  I have no friggin' clue how to start, 
and I can't seem to find a tutorial anywhere...


What IDE should I use? I'm not big fan of Eclipse, although if I 
had to use it this wouldn't be a dealbreaker.  Give me something 
easy and lightweight, unless you've got a GUI builder (this is 
why I started with MonoDevelop, though this isn't working so well 
for me).


What Widget library should I use?  I started with GTKD, but since 
there are no tutorials does this mean nobody actually does this?  
Should I use DWT?  What about QT?


I just want something simple and mainstream to start learning D 
with.


Any thoughts?



Re: Learning D

2014-08-25 Thread Ryan via Digitalmars-d-learn
Thanks for both responses.  This is the information I was looking 
for.


I have DMD, GTK# (For MonoDevelop), MonoDevelop, MonoD, dubs, and 
GTKD installed.


I've got some things to compile... So the crux of my issue is 
that I can't figure out how to link lib files in MonoDevelop.  I 
wonder if there is a problem in the latest version or something.


Re: Learning D

2014-08-25 Thread Ryan via Digitalmars-d-learn

Anyone know MonoDevelop?

Why is the Edit References context menu item missing.  I have 
it at the top (Project-Edit References...) but when I click it 
nothing happens. Grrr.




Re: Learning D

2014-08-25 Thread Ryan via Digitalmars-d-learn

On Monday, 25 August 2014 at 18:12:25 UTC, Colin wrote:

On Monday, 25 August 2014 at 17:57:54 UTC, Ryan wrote:

Anyone know MonoDevelop?

Why is the Edit References context menu item missing.  I 
have it at the top (Project-Edit References...) but when I 
click it nothing happens. Grrr.


I couldnt figure it out either tbh (creating dub projects using 
MonoD)


I just fire up a command line, go to my workspace folder, and do
dub init project name here

Then, in monoD, File - Open - C:\Path\To\Project\dub.json

Then your good to go regarding MonoD.



Yeah, I gave up on MonoD to try and figure out what is going on 
behind the scenes by compiling with text files... I had a little 
success compiling, followed by a failure to run because I don't 
have the right version of GTK+ on my system...


Then I thought I'd learn dub.  Well, this is NOT going well... I 
did a git clone of gtk-d, then tried to build with dub (renamed 
the package.json to dub.json), and it told me Conflicting 
package multi-reference I have no clue and I've tried 
removing package references


I tried dub remove gtk-d --version=* but it just lists out 
excuses why it can't work... no retrieval journal found for..., 
Untracked file found


So then I try every variation of dub remove-local and dub 
remove-path I can think of.


I give up.  Why not just have a dub 
-IFuckedUpSoLetsStartOverCleanSlated option?