idup class

2014-05-16 Thread Joshua Niehus via Digitalmars-d-learn

trying to follow:
http://ddili.org/ders/d.en/class.html

//--- OSX 10.9 DMD 2.065
module test;

class Foo {
 int num;

 this(int num) {
 this.num = num;
 }

 Foo dup() const {
 return new Foo(this.num);
 }

 immutable(Foo) idup() const {
 return new immutable(Foo)(this.num);
 }
}

void main() {
 auto  foo = new Foo(1);
 auto mfoo = foo.dup();
 auto ifoo = foo.idup();
}

* test.d(15): Error: mutable method test.Foo.this is not callable
using a immutable object
* test.d(15): Error: no constructor for Foo
* Failed: [dmd, -v, -o-, test.d, -I.]
//---

What am i missing?


Re: idup class

2014-05-16 Thread Joshua Niehus via Digitalmars-d-learn

On Friday, 16 May 2014 at 20:36:37 UTC, Ali Çehreli wrote:


My apologies. The code was written for an older version of dmd. 
The simplest fix is to define the constructor as pure:


 pure this(int num) {
 this.num = num;
 }

Ali


Ahh great thanks guys.
No worries Ali, great book, i refer to it all the time :)


Re: core.sync.rwmutex example

2014-05-09 Thread Joshua Niehus via Digitalmars-d-learn

Hi Charles,

would the following work (just a shot in the dark) ?

//---
module test;

import std.stdio;
import std.concurrency;

void spawnedFuncFoo(Tid tid, Tid tidBar) {
 receive(
 (int i) {
 writeln(Foo Received the number , i);
 send(tidBar, i, thisTid);
 auto barSuccessful = receiveOnly!(string);
 writeln(Bar got my (Foo) message);
 }
 );

 send(tid, true);
}

void spawnedFuncBar(Tid tid) {
 receive(
 (int i, Tid tidFoo) {
 writeln(Foo passed me (Bar) the number , i);
 send(tidFoo, done);
 }
 );

 receive(
 (string sig) {
 writeln(Main says I'm (Bar) done.);
 send(tid, 42);
 }
 );
}

void main() {
 auto tidBar = spawn(spawnedFuncBar, thisTid);
 auto tidFoo = spawn(spawnedFuncFoo, thisTid, tidBar);
 send(tidFoo, 42);
 auto fooWasSuccessful = receiveOnly!(bool);
 assert(fooWasSuccessful);

 send(tidBar, your done);
 auto barWasSuccessful = receiveOnly!(int);
 assert(barWasSuccessful == 42);
 writeln(Successfully had two separate threads communicate
with each other);
}
//---


Atom text editor

2014-05-06 Thread Joshua Niehus via Digitalmars-d-learn

FYI:

If anyone is using GitHub's text editor Atom and would like
basic D syntax highlighting:

apm init --package ~/.atom/packages/language-d --convert
https://github.com/textmate/d.tmbundle

https://atom.io/docs/v0.94.0/converting-a-text-mate-bundle


fedora libcurl-gnutls issue

2013-09-27 Thread Joshua Niehus

http://d.puremagic.com/issues/show_bug.cgi?id=10710

Does anyone know if there is there a workaround for this issue?
Unfortunately Im stuck with fedora and cant jump to another OS...

Thanks


Re: fedora libcurl-gnutls issue

2013-09-27 Thread Joshua Niehus

On Friday, 27 September 2013 at 16:52:49 UTC, Dicebot wrote:
Simply building dmd/phobos from git tag should result in proper 
linkage.


Nothing is ever simple with me :)

Thanks Dejan and Dicebot



shared std.signals

2013-01-22 Thread Joshua Niehus

Is it possible to create a shared signal class?
I  would like to create a shared signal class so some other 
process that knows certain things can come along emit its info to 
any observer:


import std.stdio, std.signals;

class Observer {
void watch(string msg) {
writeln(msg);
}
}

class Foo {
string value() {
return _value;
}

string value(string v) {
if (v != _value) {
_value = v;
emit(_value);
}
return v;
}

mixin Signal!(string);

private:
string _value;
}

shared Foo a;
void main() {
a = new shared Foo();
Observer o1 = new Observer();
a.connect(o1.watch);
}


Re: shared std.signals

2013-01-22 Thread Joshua Niehus
On Wednesday, 23 January 2013 at 07:11:59 UTC, Joshua Niehus 
wrote:

Is it possible to create a shared signal class?


oh god... dont tell me __gshared !
Think i answered my own question, it got me to the next step.  
going to have to read through those giant shared threads again




Re: Help me write saveAll

2012-12-21 Thread Joshua Niehus

On Friday, 21 December 2012 at 17:01:14 UTC, monarch_dodra wrote:
There are a lot of algorithms in std.algorithm that operate on 
foo(Range, Needles...)(Range range, Needles needles).


Needles can be anything, in particular, either an element or 
a range.


The thing is that every now and then, you want to save the 
entirety of (the ranges) inside needles. EG, I want to be able 
to write:

foo(range, needles.saveAll);
[...snip...]
Any thought on how do get this working?
size_t r = startsWith!pred(haystack, needles.saveAll);


Sorry if im misunderstanding, but doesn't filter do this?
Example:
import std.stdio, std.algorithm, std.range;
void main() {
auto x = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
];
//haystack   // needle  // 
needle
auto y = x.filter!(a = (a == [4, 5, 6] || a == [7, 8, 
9])).array;

y.writeln;
}


Re: prune with dirEntries

2012-11-30 Thread Joshua Niehus

On Friday, 30 November 2012 at 12:02:51 UTC, Dan wrote:
Good idea, thanks. I could not get original to compile as is - 
but the concept is just what was needed. I got an error on line 
8:
Error: not a property dirEntries(path, cast(SpanMode)0, 
true).filter!(__lambda2)

I'm using a quite recent version of dmd and phobos.


hmm strange... I'm using 2.060 (on a mac),

But, I pulled the lamda out into a function and it works great. 
I assume the parallel is for performance, and it actually runs 
significantly slower than without on my test case - but no work 
is being done other than build the list of files, so that is 
probably normal. For my case the breakdown is:


No Pruning: 11 sec
Pruning Parallel: 4.78 sec
Pruning Serial: 0.377 sec


Thats cool.
Yea I thought parallel would make a big difference (in the 
positive sense) for large directories, but I guess if we are 
recursively spawning parallel tasks, the overhead involved starts 
accumulating, resulting in worse performance (my best guess 
anyway).





Re: prune with dirEntries

2012-11-29 Thread Joshua Niehus

On Friday, 30 November 2012 at 01:57:21 UTC, Dan wrote:
That will do the filtering correctly - but what I was hoping 
was to actually prune at the directory level and not drill down 
to the files in of an unwanted directory (e.g. .git). The 
problem with this and what I'm trying to overcome is accessing 
lots of files and directories recursively all of which I want 
to skip. Much like there is a *followSymlink* it would be nice 
if a predicate were accepted to *followDirectory* in general or 
some way to cause that.


what about the following?

import std.algorithm, std.array, std.regex;
import std.stdio, std.file;
void main()
{
  auto exclude = regex(r\.git, g);
  dirEntries(/path/GIT, SpanMode.breadth)
.filter!(a = match(a.name, exclude).empty)
.writeln();
}

I think if you go breadth first, you can filter out the unwanted 
directories before it delves into them




Re: prune with dirEntries

2012-11-29 Thread Joshua Niehus

On Friday, 30 November 2012 at 06:29:01 UTC, Joshua Niehus wrote:
I think if you go breadth first, you can filter out the 
unwanted directories before it delves into them


oh wait... it probably still looks through all those dir's.
What about this?

import std.algorithm, std.regex, std.stdio, std.file;
import std.parallelism;
DirEntry[] prune(string path, ref DirEntry[] files)
{
  auto exclude = regex(r\.git|\.DS_Store, g);
  foreach(_path; taskPool.parallel(dirEntries(path, 
SpanMode.shallow)

.filter!(a = match(a.name, exclude).empty)))
  {
files ~= _path;
if (isDir(_path.name)) { prune(_path.name, files); }
  }
return files;
}

void main()
{
  DirEntry[] files;
  prune(/path, files);
  foreach(file;files) { writeln(file.name); }
}



Re: path matching problem

2012-11-27 Thread Joshua Niehus
On Tuesday, 27 November 2012 at 19:40:56 UTC, Charles Hixson 
wrote:
Is there a better way to do this?  (I want to find files that 
match any of some extensions and don't match any of several 
other strings, or are not in some directories.):


 import std.file;

...

 string  exts  =  *.{txt,utf8,utf-8,TXT,UTF8,UTF-8};
 string[]  exclude  =  [/template/,  biblio.txt,  
categories.txt,

subjects.txt,  /toCDROM/]

 int  limit  =  1
 //  Iterate  a  directory  in  depth
 foreach  (string  name;  dirEntries(sDir,  exts,  
SpanMode.depth))

 {  bool  excl  =  false;
foreach  (string  part;  exclude)
{  if  (part  in  name)
   {  excl  =  true;
  break;
   }
}
if  (excl)  break;
etc.


maybe this:?

import std.algorithm, std.array, std.regex;
import std.stdio, std.file;
void main()
{
enum string[] exts  =  [`.txt`, `.utf8`, `.utf-8`, 
`.TXT`, `.UTF8`, `.UTF-8`];
enum string exclude = 
`r/template/|biblio\.txt|categories\.txt|subjects\.txt|/toCDROM/`;


auto x = dirEntries(/path, SpanMode.depth)
.filter!(`endsWith(a.name,` ~ exts.join(,) ~ `)`)
.filter!(`std.regex.match(a.name,` ~ exclude ~ 
`).empty`);;


writeln(x);
}


Re: path matching problem

2012-11-27 Thread Joshua Niehus
On Tuesday, 27 November 2012 at 23:43:43 UTC, Charles Hixson 
wrote:
But why the chained filters, rather than using the option 
provided by dirEntries for one of them?  Is it faster?  Just 
the way you usually do things? (Which I accept as a legitimate 
answer.  I can see that that approach would be more flexible.)


Ignorance...
Your right, I didn't realize that dirEntries had that filter 
option, you should use that.  I doubt the double .filter would 
effect performance at all (might even slow it down for all i know 
:)


//update:
import std.algorithm, std.array, std.regex;
import std.stdio, std.file;
void main()
{
  string exts = *.{txt,utf8,utf-8,TXT,UTF8,UTF-8};
  enum string exclude =

`r/template/|biblio\.txt|categories\.txt|subjects\.txt|/toCDROM/`;


  dirEntries(/path, exts, SpanMode.depth)
.filter!(` std.regex.match(a.name,` ~ exclude ~ `).empty `)
.writeln();
}



Re: Converting a number to complex

2012-11-25 Thread Joshua Niehus
On Saturday, 24 November 2012 at 07:27:18 UTC, Philippe Sigaud 
wrote:
It's an is() expression (you cited my tutorial, there is an 
appendix on
it). It recently became even more powerful, so the tutorial is 
not accurate

any more.


its time for another read through:)
Template constraints might make for a good talk at the conf...



Re: Converting a number to complex

2012-11-23 Thread Joshua Niehus
On Friday, 23 November 2012 at 12:39:59 UTC, Frederik Vagner 
wrote:

Now do it for complex number please ^^


touche!

import std.stdio, std.conv, std.traits, std.complex;

template isComplexNumeric(T)
{
static if(is(NumericTypeOf!T)) {
enum bool isComplexNumeric = is(NumericTypeOf!T);
}
else static if (is(T == Complex!double))
{
enum bool isComplexNumeric = is(Complex!double);
}
// fillin real and float here... (e.g. is(Complex!real); 
etc...

}

class Example(T) if (isComplexNumeric!T)
{
T k = to!T(1);
}

void main()
{
auto x = new Example!(Complex!double)();
writeln(x.k);
auto y = new Example!double();
writeln(y.k);
auto z = new Example!string(); // fail
writeln(z.k);
}

A bit messy, but im sure there is some room for cleanup 
somewhere...





Re: Converting a number to complex

2012-11-23 Thread Joshua Niehus

On Friday, 23 November 2012 at 16:11:25 UTC, Joshua Niehus wrote:
A bit messy, but im sure there is some room for cleanup 
somewhere...


Errata... (what i get for copy/pasting)

import std.stdio, std.conv, std.traits, std.complex;
template isComplexNumeric(T)
{
static if(isNumeric!T) {
enum bool isComplexNumeric = true;
}
else static if (is(T == Complex!double))
{
enum bool isComplexNumeric = true;
}
else {
enum bool isComplexNumeric = false;
}
}

class Example(T) if (isComplexNumeric!T)
{
T k = to!T(1);
}

void main()
{
auto x = new Example!(Complex!double)();
writeln(x.k);
auto y = new Example!double();
writeln(y.k);
auto z = new Example!string();
writeln(z.k);
}

i did have to reference Philippe Sigaud's excellent book on 
Templates several times:

https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf



Re: Converting a number to complex

2012-11-23 Thread Joshua Niehus

meh, couldn't resist:

import std.stdio, std.conv, std.traits, std.complex;
template isComplex(T)
{
static if (is(T == Complex!double))
{
enum bool isComplex = true;
}
else static if (is(T == Complex!float))
{
enum bool isComplex = true;
}
else static if (is(T == Complex!real))
{
enum bool isComplex = true;
}
else {
enum bool isComplex = false;
}
}

template isComplexOrNumeric(T)
{
enum bool isComplexOrNumeric = (isComplex!T || isNumeric!T);
}

class Example(T) if (isComplexOrNumeric!T)
{
T k = to!T(1);
}



Re: Converting a number to complex

2012-11-23 Thread Joshua Niehus

On Friday, 23 November 2012 at 18:45:53 UTC, Artur Skawina wrote:

   template isComplex(T) {
  static if (is(T _ == Complex!CT, CT))
 enum isComplex = true;
  else
 enum isComplex = false;
   }

artur


oh wow didnt know u could do that. much nicer.



Re: Converting a number to complex

2012-11-22 Thread Joshua Niehus
On Thursday, 22 November 2012 at 15:47:11 UTC, Frederik Vagner 
wrote:
I am trying to make a templated class to accept any numeric 
type:


class example(Type) if (isNumeric(Type))
{
Type k = to!Type(1);

}

however I always get a compiler erro stating I cannot make that 
conversion. How do I fix it?


// phobos
import std.stdio, std.conv, std.traits;
class Example(T) if (isNumeric!T)
{
T k = to!T(1);
}

void main()
{
auto x = new Example!double();
writeln(x.k);
}


std.concurrency msg passing

2012-10-18 Thread Joshua Niehus

Is the following snippet a bug?

---
import core.thread;
import std.stdio, std.concurrency;

void foo(Tid tid) {
send(tid, true);
}

void main() {
auto fooTid = spawn(foo, thisTid);
auto receiveInt = receiveTimeout(dur!seconds(10), (int 
isInt) {

writeln(I should not be here);
});
}
// output: I should not be here
---

If not, is there some way I could achieve 
receiveOnlyTimeout!(int)(dur, fun); ?


Thanks,
Josh


Re: std.concurrency msg passing

2012-10-18 Thread Joshua Niehus

On Thursday, 18 October 2012 at 17:33:04 UTC, cal wrote:

I can't see the bug? The receiver accepts a bool as an int,
same way a normal function does. The timeout is long enough 
that foo gets a chance to send.  If you want to stop the int 
receiver getting a bool, you could add another receiver with 
(bool) { // do nothing } or whatever before the (int) one, 
which will be a better match for the send.


I got myself into a situation where I dont know which will be 
called first, the int or the bool and when the call happens 
matters.


It was my assumption that receiveTimeout(dur, (type){}); would 
distinguish between a bool and an int like it does for float and 
int.  But I can see why it would treat true/false as 0/1, so not 
a bug.


Anyway my current workaround is to define a few Signal structs 
and use those instead:

struct SignalReady { blah }
struct SignalXDone { blah }
struct SignalYDone { blah }

Thanks for the reply.





Re: regex issue

2012-03-16 Thread Joshua Niehus

On Friday, 16 March 2012 at 08:34:18 UTC, Dmitry Olshansky wrote:
Ehm, because they have different engines that _should_ give 
identical results. And the default one apparently has a bug, 
that I'm looking into.

Fill the bug report plz.


Ok, submitted: id 7718

Thanks,
Josh


Re: Compiling DMD on MAC OS X

2012-02-29 Thread Joshua Niehus
On Monday, 20 February 2012 at 11:18:34 UTC, Tyro[a.c.edwards] 
wrote:

...

and I doubt you want me to put all of what dmd -v spits out 
for this little script.


Thanks,
Andrew


Hi Andrew,

I ran into this problem as well and here is how I fixed/hacked it:
OSX Lion, and soon to be Mountain Lion, no longer come with GCC 
installed for the Command Line (/usr/bin/gcc)
What you need to do is Install Xcode from the app store, which is 
free, and then:

  * Launch your Xcode 4.1
  * Go to preferences  Downloads
  * Click on the install button near the Command line tools

This will put gcc in your /usr/bin directory.

Then try to recompile your code.

-- the new mac installer on the website should probably come with 
gcc or check for dependencies


Josh


Reflection

2012-02-27 Thread Joshua Niehus

Hello,

I dont understand the following snippet's output:

import std.stdio, std.traits;
void main() {
writeln(isSomeFunction!(writeln));
writeln(isCallable!(writeln));
writeln(Yes I am...);
}

/* OUTPUT */
false
false
Yes I am...

If 'writeln' isn't a method/function and it's not callable, then 
what is it?


Thanks,
Josh


Re: Reflection

2012-02-27 Thread Joshua Niehus
On Tuesday, 28 February 2012 at 06:10:11 UTC, Jesse Phillips 
wrote:

It is a template.


I see, thanks.
And I bet its not possible to figure out if a template is a 
function template or a class template etc...





Syntax highlighting for CodeRunner

2012-02-26 Thread Joshua Niehus
In the off chance that some of you are running a Mac and using 
CodeRunner to play around with D, I cooked up the files you need 
for CodeRunner to highlight D's syntax:

https://github.com/jniehus/Dlang-for-CodeRunner


Waiting around

2012-01-09 Thread Joshua Niehus
Hello,

I need to connect to a network location and read a file but I also need
some way of waiting around until the connection is established.  Currently
I use the following snippet to do this:

while (!std.file.exists(/Volumes/mountedDir/myfile.txt)  timeout  30)
{
core.thread.Thread.sleep(10_000_000);  // core.thread.Thread
conflicts with std.regex.Thread, hence the full path reference
timeout++;
}

with the recent changes to std.regex it stopped compiling due to the
Thread conflict.  Is there a more obvious way to do what I'm doing that
avoids importing core.thread?

Thanks,
Josh


File append limit?

2011-08-05 Thread Joshua Niehus
Hello,

I am running a script that creates a file which lists all the folders in a
directory:

foreach (string name; dirEntries(/Users/josh/, SpanMode.shallow)) {
append(/Users/dirList.txt, name ~ \n);
}

But it seems to stop appending after 255 lines (this particular folder has
350 folders in all).
When trying to read the created file:

   auto f = File(/Users/dirList.txt, r);
   foreach (string line; lines(f)) {
   writeln(line);
   }
   f.close();

It writes out the lines, but after the last one I get Bus error: 10

Any thoughts on what im missing or doing wrong?

I am currently using D 2.054 on Mac OSX Lion
The script was working with D 2.053 on Mac OSX Snow Leopard

Thanks,
Josh


Re: File append Limit

2011-08-05 Thread Joshua Niehus
@Kagamin

 What if

 foreach(i;0..512) {
  append(/Users/dirList.txt, text(line ,i,'\n'));
 }

That works, but I misrepresented the problem and found that the following
may be the issue (this looks more like the code im using):

import std.conv, std.stdio;

void main()
{
string[] strArr;
foreach(int i; 0 .. 257) {
strArr ~= text(Line:  ~ to!string(i));
}

foreach(string e; strArr) {
writeln(e);
}
}

// OUTPUT for first 87 lines
Line: 2
?O
?O
`O
@O
 O

?N
?N
...
ect
...
?@
?@
`@
0@

Line: 88
 /* rest of output is as expected */

Changing 257 to 256 gives you what you would expect.

Josh


Interfacing to C

2011-06-28 Thread Joshua Niehus
Hello,

I was trying to run the example on the Interfacing to C page (
http://www.d-programming-language.org/interfaceToC.html) and ran into few
issues. To get it to work as is i was .dup(ing) strings into new chars
with defined size and passing those with .ptr. Anyway it seemed like quite a
bit of work for something simple so I added const in the signatures and
things worked much more smoothly:

import std.stdio, std.string;

extern (C) int strcmp(const char* string1, const char* string2);

void main()
{
writeln(myDfunction(foo));
}

int myDfunction(const char[] s) {
return strcmp(std.string.toStringz(s), foo);
}

Was there a reason why the consts were left out?
if this is a typo I'd be happy to update the web page, would be good
experience for a noob like me. (never used GIT before and first time
participating in a community)

Thanks,
Josh


RE: dmdscript osx.mak

2011-06-19 Thread Joshua Niehus
Hi Robert and Dmitry,

Thanks for your replies and the heads up on the current status of DMDScript!

Josh


dmdscript osx.mak

2011-06-18 Thread Joshua Niehus
Hello,

I apologize if this is the wrong forum to post this question, but I couldn't
find a corresponding learn mailing list for DMDScript.

I wanted to play around with DMDScript but cant seem to get started.  I
downloaded the source and attempted to make it via:
$ make -f osx.mak

But I get the following error:
textgen.d(132): Error: cannot implicitly convert expression ~ some
string ~ of type string to char[]

Am I doing the right thing? Or how do I go about getting building 'ds' so I
can run simpleton scripts?

Josh


Re: dmd vs rdmd

2011-06-13 Thread Joshua Niehus
Thanks Jonathan, that cleared things up for me.

Josh

On Sat, Jun 11, 2011 at 12:00 PM, digitalmars-d-learn-requ...@puremagic.com
 wrote:

 Send Digitalmars-d-learn mailing list submissions to
digitalmars-d-learn@puremagic.com

 To subscribe or unsubscribe via the World Wide Web, visit

 http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn

 or, via email, send a message with subject or body 'help' to
digitalmars-d-learn-requ...@puremagic.com

 You can reach the person managing the list at
digitalmars-d-learn-ow...@puremagic.com

 When replying, please edit your Subject line so it is more specific
 than Re: Contents of Digitalmars-d-learn digest...


 Today's Topics:

   1. dmd vs rdmd (Joshua Niehus)
   2. Re: dmd vs rdmd (Jonathan M Davis)
   3. Re: dmd vs rdmd (Andrej Mitrovic)
   4. char[] to string (Jonathan Sternberg)
   5. Re: char[] to string (Jonathan M Davis)
   6. Re: DMD Backend: Deciding instructions to use/avoid?
  (Nick Sabalausky)
   7. Re: char[] to string (bearophile)


 --

 Message: 1
 Date: Fri, 10 Jun 2011 14:28:41 -0700
 From: Joshua Niehus jm.nie...@gmail.com
 To: digitalmars-d-learn@puremagic.com
 Subject: dmd vs rdmd
 Message-ID: BANLkTi=TYnN+UuxCr8wj8UwFRjS=ivz...@mail.gmail.com
 Content-Type: text/plain; charset=iso-8859-1

 Hello,

 I am trying to compile code which is composed of two modules (in the same
 directory):

 main.d
 foo.d

 foo.d just declares a class Foo which has a string variable bar which i
 set to hello and main.d just prints bar's value to the console:

 // - main.d ---
 import std.stdio, foo;

 void main() {
Foo f = new Foo;
writeln(f.bar);
 }

 When i attempt to compile main.d via:
 $dmd main.d
 I get undefined symbol errors.

 But when I run:
 $rdmd main.d
 it works as expected.

 The only way I could get main.d to compile with just dmd was to compile foo
 as a lib first and then compile main.d and passing the foo.a file as an
 argument:
 $dmd -lib foo.d
 $dmd main.d foo.a

 Is this normal?
 I got the impression from TDPL (Alexandrescu) that the dmd compiler would
 automatically search the root directory, find foo.d, work its magic, and
 create all the symbols that were defined in foo.d for main.d to compile...

 Thanks,
 Josh
 -- next part --
 An HTML attachment was scrubbed...
 URL: 
 http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110610/dccf485f/attachment-0001.html
 

 --

 Message: 2
 Date: Fri, 10 Jun 2011 21:50:37 +
 From: Jonathan M Davis jmdavisp...@gmx.com
 To: digitalmars.D.learn digitalmars-d-learn@puremagic.com
 Subject: Re: dmd vs rdmd
 Message-ID: 20110610215037.56...@gmx.com
 Content-Type: text/plain; charset=utf-8

 On 2011-06-10 14:28, Joshua Niehus wrote:
  Hello,
 
  I am trying to compile code which is composed of two modules (in the same
  directory):
 
  main.d
  foo.d
 
  foo.d just declares a class Foo which has a string variable bar which i
  set to hello and main.d just prints bar's value to the console:
 
  // - main.d ---
  import std.stdio, foo;
 
  void main() {
  Foo f = new Foo;
  writeln(f.bar);
  }
 
  When i attempt to compile main.d via:
  $dmd main.d
  I get undefined symbol errors.
 
  But when I run:
  $rdmd main.d
  it works as expected.
 
  The only way I could get main.d to compile with just dmd was to compile
 foo
  as a lib first and then compile main.d and passing the foo.a file as an
  argument:
  $dmd -lib foo.d
  $dmd main.d foo.a
 
  Is this normal?
  I got the impression from TDPL (Alexandrescu) that the dmd compiler would
  automatically search the root directory, find foo.d, work its magic, and
  create all the symbols that were defined in foo.d for main.d to
 compile...

 With dmd, you must list every file that you're compiling. The only
 exceptions
 are that when dealing with libraries, dmd to have the root directory where
 the
 source is passed to -I, and it needs to have the root directory where the
 library is given with -L and -l with the library name (or just the library
 directly if you don't want it to search for the library). It's like gcc and
 dmc in that respect. It does nothing extra to track down files to compile
 for
 you. It'll find the source for importing thanks to -I, but it won't compile
 it. You must still compile it. You don't normally need -I or -L however,
 because dmd.conf (or sc.ini on Windows) already adds the appropriate flags
 for
 Phobos for you. You only need too specify them yourself when using other
 libraries.

 rdmd does extra magic to automatically track down all of the files that
 main.d
 imports and compile them. dmd doesn't do that.

 - Jonathan M Davis


 --

 Message: 3
 Date: Sat, 11 Jun 2011 01:08:50 +0200
 From: Andrej Mitrovic andrej.mitrov...@gmail.com
 To: digitalmars.D.learn digitalmars-d-learn

Re: simple syntax issue with template

2011-06-13 Thread Joshua Niehus
 I'm trying to create 2 extra method for arrays (range would be
 better, though I don't quite understand what is a range)
 Although I have some indecipherable (to me) compiler error...

 What's wrong with the code below?
 ==
 import std.algorithm;

 public:

 void remove(T)(ref T[] array, T element)
 {
auto index = array.countUntil!(a == b, T[], T)(array, element);
removeAt(index);
 }

 void removeAt(T)(ref T[] array, sizediff_t index)
 {
if(index  0 || index = array.length)
return;
array.replaceInPlace(index, index + 1, []);
 }


 unittest
 {
auto a = [1, 3, 4];
a.remove(3);
assert(a == [1, 4]);
 }
 ==

Hi Lloyd,

why not just use the built in functionality of arrays?

//--
import std.stdio;

void main() {
auto a = [1, 2, 3, 4, 5, 6, 7, 8];
remove(a, 3);

foreach (i; a) {
write(i,  );
}
writeln();
}

void remove(T) (ref T[] myarray, int element) {
auto front = myarray[0 .. (element -1)];
auto back =  myarray[element .. $];
myarray = front ~ back;
// or simply: myarray = myarray[0 .. (element - 1)] ~ myarray[element ..
$];
}
//--

Josh


dmd vs rdmd

2011-06-10 Thread Joshua Niehus
Hello,

I am trying to compile code which is composed of two modules (in the same
directory):

main.d
foo.d

foo.d just declares a class Foo which has a string variable bar which i
set to hello and main.d just prints bar's value to the console:

// - main.d ---
import std.stdio, foo;

void main() {
Foo f = new Foo;
writeln(f.bar);
}

When i attempt to compile main.d via:
$dmd main.d
I get undefined symbol errors.

But when I run:
$rdmd main.d
it works as expected.

The only way I could get main.d to compile with just dmd was to compile foo
as a lib first and then compile main.d and passing the foo.a file as an
argument:
$dmd -lib foo.d
$dmd main.d foo.a

Is this normal?
I got the impression from TDPL (Alexandrescu) that the dmd compiler would
automatically search the root directory, find foo.d, work its magic, and
create all the symbols that were defined in foo.d for main.d to compile...

Thanks,
Josh