write multiple lines without \n with writeln

2014-11-20 Thread Suliman via Digitalmars-d-learn

In dco source code I have found:

void ShowUsage()
{
writeln(
dco build tool  ~ strVersion ~ 
written by FrankLIKE.
Usage:
dco [switches...] files...
for example: dco
or: dco app.d
build for dfl2: dco

}

I do not see here any \n. Why this code is output all one by 
line, and not in single line?


Why my code:
writeln(App name: ~ appName ~
App version: ~ appVersion ~
To see help please use -h\\-help option);
writeln(args);

output:
App name:CoolAppApp version:0.0.1To see help please use -h\-help 
option[.\\app1

.exe]



Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread anynomous via Digitalmars-d-learn

module a;

void init (ref string[] argv) { }

void main(string[] args) {
init(args);   //ok
auto y = args[1..$];
init(y);  //ok
auto x = args.dup;
init(x);  //ok
init(args[1..$]); // Error: function a.init (ref string[]
argv) is not callable using argument types (string[])
init(args.dup);   // Error: function a.init (ref string[] argv)
is not callable using argument types (string[])
}

tested with
dmd 2.066.0
ldc 0.15 beta (Frontend 2.066.1)

Similar Errors occure with int[].

Is this a bug that should be reported or is there some reason for
the errors?
I searched for an issue the bugtracker and did not find a
matching one.

The error messages are strange anyway.


Re: Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread Ali Çehreli via Digitalmars-d-learn

On 11/20/2014 12:37 AM, anynomous wrote:

 module a;

 void init (ref string[] argv) { }

 void main(string[] args) {
  init(args);   //ok
  auto y = args[1..$];
  init(y); //ok
  auto x = args.dup;
  init(x);  //ok
  init(args[1..$]); // Error: function a.init (ref string[]
 argv) is not callable using argument types (string[])
  init(args.dup);   // Error: function a.init (ref string[] argv)
 is not callable using argument types (string[])
 }

Both of the error cases are trying to pass rvalues as arguments to ref 
parameters. Not possible in D even for reference to const.


Judging from the name init(), I don't think you need ref anyway.

If you really want to write a function that should take both lvalues and 
rvalues, then a convenient solution is to use 'auto ref', which is 
available only to templates. For that reason, you have to stick an empty 
template parentheses just to make it a template:


void init()(auto ref string[] argv) { }

'auto ref' takes lvalues by reference and rvalues by copy. So, the code 
will work but then if you make modifications to the parameter and it was 
an rvalue to begin with, your changes will not be visible outside of the 
function anyway.


So, it is more natural to use 'auto ref' in cases where you don't want 
to modify the parameter. Then you use 'auto ref const':


void init()(auto ref const string[] argv) { }

Again though, you probably don't want 'ref' anyway. (?)

 The error messages are strange anyway.

Agreed. In the case of assignment to rvalue, dmd is better:

a + b = 42;  // Error: a + b is not an lvalue

Ali

P.S. Your question is incredibly timely. I've started writing a new 
chapter titled Lvalues and Rvalues just yesterday. :)




Re: write multiple lines without \n with writeln

2014-11-20 Thread uri via Digitalmars-d-learn

On Thursday, 20 November 2014 at 08:28:11 UTC, Suliman wrote:

In dco source code I have found:

void ShowUsage()
{
writeln(
dco build tool  ~ strVersion ~ 
written by FrankLIKE.
Usage:
dco [switches...] files...
for example: dco
or: dco app.d
build for dfl2: dco

}

I do not see here any \n. Why this code is output all one by 
line, and not in single line?


Why my code:
writeln(App name: ~ appName ~
App version: ~ appVersion ~
To see help please use -h\\-help option);
writeln(args);

output:
App name:CoolAppApp version:0.0.1To see help please use 
-h\-help option[.\\app1

.exe]


In all string literal forms, an EndOfLine is regarded as a 
single \n character.


http://dlang.org/lex.html



It's by design but is *really* annoying and should be limited to 
WysiwygString literals IMO.


Auto-formatting with clang-format or astyle can mess up writeln 
formatting completely.


Cheers,
uri


Re: write multiple lines without \n with writeln

2014-11-20 Thread bearophile via Digitalmars-d-learn

uri:


It's by design


And it's a nice handy design.

Bye,
bearophile


dco how to specify Jpath?

2014-11-20 Thread Suliman via Digitalmars-d-learn
I am playing with dco. And it's look very helpful for tiny 
projects.


I can't understand is it's possible to add to dco.ini Jpath?
I am talking about something like:
dflags=-JD:\code\d\App1\source\

but when I am trying to compile code with dco it's can't find 
import.
I looked at source code and it's look like it's do not support 
dflags...


https://github.com/FrankLIKE/dco/tree/master/source


Re: Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread bearophile via Digitalmars-d-learn

Ali Çehreli:


 The error messages are strange anyway.

Agreed. In the case of assignment to rvalue, dmd is better:

a + b = 42;  // Error: a + b is not an lvalue


The error messages in the OP case can and should be improved. I 
suggest to take a look in Bugzilla to see if there's already an 
ER for it, and otherwise to add the little diagnostic ER it to 
Bugzilla.


Bye,
bearophile


Re: write multiple lines without \n with writeln

2014-11-20 Thread Suliman via Digitalmars-d-learn

I understand it.

I expect what concatenation symbol will stay new line in new line 
and not append it's to current:


writeln(
first string
second ~
string
);

I expect:
first string
second
string

but not:
first   stringsecondstring


Re: help

2014-11-20 Thread Mike Parker via Digitalmars-d-learn

On 11/20/2014 3:38 PM, michael via Digitalmars-d-learn wrote:

Hello All:
when i am using std.net.curl to download a file that dosent exist on ftp
sever,my code
will get an execption like this
 ‍std.net.curl.CurlException@std\net\curl.d(3605
mailto:‍std.net.curl.CurlException@std\net\curl.d(3605): Remote file
not found on handle
  1A8E038

0x0042EC05
0x004027F6
and then it exits,however what i want when it occers is that my
code shows an error message  like
please check the url you typed blablabla not the one above,can anyone
give a hand on me ?

 thanks a lot.

 Michael.


try {
// call curl
} catch( CurlException ) {
// print error message
}


Re: write multiple lines without \n with writeln

2014-11-20 Thread bearophile via Digitalmars-d-learn

Suliman:


I understand it.

I expect what concatenation symbol will stay new line in new 
line and not append it's to current:


writeln(
first string
second ~
string
);

I expect:
first string
second
string

but not:
first   stringsecondstring


If I compile and run this program:

void main() {
import std.stdio;
writeln(
first string
second ~
string
);
}


I see this output (and it's correct, as expected):

first stringsecondstring

Bye,
bearophile


Re: write multiple lines without \n with writeln

2014-11-20 Thread uri via Digitalmars-d-learn

On Thursday, 20 November 2014 at 10:41:24 UTC, bearophile wrote:

uri:


It's by design


And it's a nice handy design.

Bye,
bearophile


For Wysiwyg strings I agree that it's great but I prefer 
C/C++/Python like behaviour for double quoted strings. I guess 
it's what I'm used to :)





Cheers,
uri




Re: Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread anonymous via Digitalmars-d-learn

On Thursday, 20 November 2014 at 09:20:34 UTC, Ali Çehreli wrote:

Judging from the name init(), I don't think you need ref anyway.


thanks for the explanation. I can't remove the ref because it is  
Main.init in gtkd.

A simple solution is init(args)
I just tried to remove a non-GTK argument (a filename) via 
slicing. This is not necessary at all...


Re: write multiple lines without \n with writeln

2014-11-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 20 November 2014 at 11:08:24 UTC, Suliman wrote:

writeln(
first string
second ~
string
);

I expect:
first string
second
string


There's no quoted newline there. You could do:

writeln(
first string
second
string);

with the newlines enclosed in the quotes, then you'd get what you 
want. (There's no extra indenting in my example because the 
indent would show up too when inside quotes)


But if it isn't in the quotes, newlines are basically ignored by 
the compiler like any other whitespace in the code.


print yyyy-mm-dd

2014-11-20 Thread Suliman via Digitalmars-d-learn
I can't understand how to get date in format -MM-dd from 
Clock.currTime

auto time = Clock.currTime;

And what next? Could anybody give any examples?


Re: Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/20/14 7:28 AM, anonymous wrote:

On Thursday, 20 November 2014 at 09:20:34 UTC, Ali Çehreli wrote:

Judging from the name init(), I don't think you need ref anyway.


thanks for the explanation. I can't remove the ref because it is
Main.init in gtkd.
A simple solution is init(args)
I just tried to remove a non-GTK argument (a filename) via slicing. This
is not necessary at all...


To further explain:

The only reason you would need to pass a string[] by ref is if you 
wanted to modify the order/size of the array. A string[]'s data is 
already passed by reference, so there is no need to ref it otherwise.


What I am guessing gtkd does, is that it processes GTK-specific 
parameters from the argument string array, then *removes* those from the 
arguments.


If you did *not* pass by reference, these modifications would be corrupt 
when you looked at the original.


This is best illustrated by an example:

void foo()(auto ref string[] x)
{
// remove all blanks
size_t i, j;
for(i = 0, j = 0; i  x.length; ++i)
{
if(x[i].length)
x[j++] = x[i];
}
x.length = j;
}

void main()
{
auto arr = [a, b, null, c, d];
foo(arr[0..$]); // pass by value
assert(arr == [a, b, c, d, d]); // oops, extra d
arr = [a, b, null, c, d];
foo(arr); // now by reference
assert(arr == [a, b, c, d]);
}

So the compiler is saving you from a mistake :)

-Steve


Re: TKd handling of custom events

2014-11-20 Thread univacc via Digitalmars-d-learn

On Wednesday, 19 November 2014 at 23:44:00 UTC, univacc wrote:

Hello,

I am using TKd to dray my linux/windows app which works very 
good!


I would like to add a global Hotkey to my program via the Win32 
API function RegisterGlobalHotkey.
Is there a possibility to access Tk's event loop so that you 
can Handle the WM_HOTKEY message, the WinAPI sends you?


Regards,
univacc


Nevermind.
If someone has the same issue: Just create another Win32 API 
Window, that is hidden and let that one receive your hotkey 
message!


Regards


Clock.currTime

2014-11-20 Thread Kent via Digitalmars-d-learn

I am reading the book 《D Cookbook》
In Page 36,this code can't be complied:

auto filtered =  filter!((a) = Clock.currTime() -
a.timeLastModified  14.days)(sorted);

sorted's type is DirEntry[].

The errors :
Error   1   Error:
'currTime(cast(immutable(TimeZone))opCall()).opBinary(a.timeLastModified())'
is not of integral type, it is a Duration   

I don't know what's the problem?


Re: Clock.currTime

2014-11-20 Thread bearophile via Digitalmars-d-learn

Kent:


I am reading the book 《D Cookbook》
In Page 36,this code can't be complied:

auto filtered =  filter!((a) = Clock.currTime() -
a.timeLastModified  14.days)(sorted);


Perhaps:

mySorted.filter!(a = Clock.currTime - a.timeLastModified  
14.days);


Bye,
bearophile


Re: Clock.currTime

2014-11-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 20 November 2014 at 15:33:37 UTC, Kent wrote:
mySorted.filter!(a = Clock.currTime - a.timeLastModified  
14.days);


Thank you for your reply.
But it still doesn't work.


Are you sure you saw the important difference? There was a  in 
the first post and it should be .


There's a few times throughout chapter one and two of the book 
where some symbols got messed up in the editing process and not 
fixed by the final draft. This looks like one of them.


Re: Clock.currTime

2014-11-20 Thread Kent via Digitalmars-d-learn

On Thursday, 20 November 2014 at 15:31:28 UTC, bearophile wrote:

Kent:


I am reading the book 《D Cookbook》
In Page 36,this code can't be complied:

auto filtered =  filter!((a) = Clock.currTime() -
a.timeLastModified  14.days)(sorted);


Perhaps:

mySorted.filter!(a = Clock.currTime - a.timeLastModified  
14.days);


Bye,
bearophile


Thank you for your reply.
But it still doesn't work.


Re: Clock.currTime

2014-11-20 Thread Kent via Digitalmars-d-learn

On Thursday, 20 November 2014 at 15:38:10 UTC, Adam D. Ruppe
wrote:

On Thursday, 20 November 2014 at 15:33:37 UTC, Kent wrote:
mySorted.filter!(a = Clock.currTime - a.timeLastModified  
14.days);


Thank you for your reply.
But it still doesn't work.


Are you sure you saw the important difference? There was a  
in the first post and it should be .


There's a few times throughout chapter one and two of the book 
where some symbols got messed up in the editing process and not 
fixed by the final draft. This looks like one of them.


I have notice the difference.Changed the  to .

void main(string[] argv)
{
 import std.file, std.algorithm, std.datetime,
std.range,std.stdio;
 DirEntry[] allFiles;
foreach(DirEntry entry; dirEntries(E:/TDDOWNLOAD,
SpanMode.depth))
allFiles ~= entry;
auto sorted = sort!((a,b) = a.size  b.size)(allFiles);
auto filtered =  filter!(a = Clock.currTime() -
a.timeLastModified  14.days)(sorted);
foreach(file; filtered.take!(10))
writeln(fiel.name);
}


Re: Clock.currTime

2014-11-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 20 November 2014 at 15:45:55 UTC, Kent wrote:

foreach(file; filtered.take!(10))


Oh, there shouldn't be a ! there either. Should be take(10), not 
take!(10).


That's my mistake too...


writeln(fiel.name);


also file is misspelled.



Re: Clock.currTime

2014-11-20 Thread Kent via Digitalmars-d-learn

On Thursday, 20 November 2014 at 15:56:34 UTC, Adam D. Ruppe
wrote:

On Thursday, 20 November 2014 at 15:45:55 UTC, Kent wrote:

   foreach(file; filtered.take!(10))


Oh, there shouldn't be a ! there either. Should be take(10), 
not take!(10).


That's my mistake too...


   writeln(fiel.name);


also file is misspelled.


Thank you very much for help me.
I notice another problem:
  foreach(file; filtered.take!(10)){...}
This line has errors.I read the stdlib reference, and change the
line to:
  foreach(file; take(filtered, 10)){...}
It becomes right.

Thank you again.


Re: help

2014-11-20 Thread MachineCode via Digitalmars-d-learn

On Thursday, 20 November 2014 at 07:25:45 UTC, Suliman wrote:
You need to check if remote file exist of server and only after 
it download шею


is this software name written in russian language?


Re: write multiple lines without \n with writeln

2014-11-20 Thread Ary Borenszweig via Digitalmars-d-learn

On 11/20/14, 9:05 AM, uri wrote:

On Thursday, 20 November 2014 at 10:41:24 UTC, bearophile wrote:

uri:


It's by design


And it's a nice handy design.

Bye,
bearophile


For Wysiwyg strings I agree that it's great but I prefer C/C++/Python
like behaviour for double quoted strings. I guess it's what I'm used to :)




Cheers,
uri




In Crystal we chose the following: you can have two consecutive string 
literals but only if they are separated by a `\` at the end of the line.


So this is a syntax error:

foo(bar baz)

But this is ok:

foo(bar \
baz)

Likewise, this is an error:

[foo, bar baz, qux] # most probably we forgot to add a comma

But this is ok:

[foo, bar \
 baz, qux]

This way you avoid silly typing mistakes while at the same time you 
allow splitting a string across several lines without having to 
concatenate them at runtime.


Re: TKd handling of custom events

2014-11-20 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 19 November 2014 at 23:44:00 UTC, univacc wrote:

Hello,

I am using TKd to dray my linux/windows app which works very 
good!


I would like to add a global Hotkey to my program via the Win32 
API function RegisterGlobalHotkey.
Is there a possibility to access Tk's event loop so that you 
can Handle the WM_HOTKEY message, the WinAPI sends you?


Regards,
univacc


I don't think this is possible in Tcl/Tk without installing an 
extension as discussed here:

http://computer-programming-forum.com/57-tcl/c9af2b9def3914c2.htm

Tkd doesn't rely on any extensions and is just using vanilla 
Tcl/Tk.


Re: Compiler error with slices, .dup and ref Parameter

2014-11-20 Thread Ali Çehreli via Digitalmars-d-learn

On 11/20/2014 02:49 AM, bearophile wrote:

 in Bugzilla to see if there's already an ER

Yep, existing ER:

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

Ali



Question about Vectors

2014-11-20 Thread Charles via Digitalmars-d-learn
So I was reading the documentation page: 
http://dlang.org/simd.html and noticed what appears to be a typo:


int4 v;
(cast(int*)v)[3] = 2;   // set 3rd element of the 4 int vector
(cast(int[4])v)[3] = 2;  // set 3rd element of the 4 int vector
v.array[3] = 2;  // set 3rd element of the 4 int vector
v.ptr[3] = 2;// set 3rd element of the 4 int vector

v.array[3] = 2; and v.ptr[3] = 2; set the fourth element, and not 
the third.


As I was verifying this, I realized I had to compile it in 64 bit 
code. The 32 bit code produced the error SIMD vector types not 
supported on this platform.


My test code is:

void main() {
import std.stdio;
import core.simd;

int4 v = 7;
v.ptr[3] = 2;
writeln(v.array[]);
}


Is that related to me compiling while using a 64 bit OS, or is 
that true of any 32 bit OS, and thus, vectors can't be used in 
programs intended to be run on 32 bit OSs?


Thanks,
Charles


Re: help

2014-11-20 Thread MrSmith via Digitalmars-d-learn

On Thursday, 20 November 2014 at 16:48:29 UTC, MachineCode wrote:

On Thursday, 20 November 2014 at 07:25:45 UTC, Suliman wrote:
You need to check if remote file exist of server and only 
after it download шею


is this software name written in russian language?


шею means it. on russian keyboard layout


Re: print yyyy-mm-dd

2014-11-20 Thread MrSmith via Digitalmars-d-learn

On Thursday, 20 November 2014 at 13:50:49 UTC, Suliman wrote:
I can't understand how to get date in format -MM-dd from 
Clock.currTime

auto time = Clock.currTime;

And what next? Could anybody give any examples?


http://dpaste.dzfl.pl/73c0438f9d1e

currTime returns SysTime;
You can then cast is to Date.
And Date has toISOExtString which converts Date to a string with 
the format -MM-DD.


Re: Dub / Derelict confusion

2014-11-20 Thread Paul via Digitalmars-d-learn

On Wednesday, 19 November 2014 at 10:07:56 UTC, Mike Parker wrote:

On 11/19/2014 6:12 PM, Paul wrote:
I would like to create a simple program using SDL. I've read 
this page
http://dblog.aldacron.net/derelict-help/using-derelict/ and 
this one
http://code.dlang.org/about and decided that using 'dub' would 
be the
sensible option for a beginner so I downloaded the dub 
executable and

put it in the same directory as my source file 'test.d'.


It doesn't need to be in the same directory as the source. Put 
it somewhere on the global path.




As per the example at the above url, I created a dub.json file 
in the

same directory with contents as so:

dependencies: {
derelict-sdl2:~1.0.0,
derelict-gl3:~master,
}


You shouldn't be depending on ~master for anything anymore. The 
newest version of dub will complain about dependencies on git 
branches. For derelict-gl3, you can use =1.0.0.




If I have the relevant import statements in test.d, am I right 
in
thinking I can use dub to build my program (not just download 
and build

the derelict libraries)?


Yes, assuming that your dub.json is properly configured.


The command dub build test.d gives me a 'failed
to load package' error.


That's the right command, but the wrong parameters. If you are 
going to pass a parameter to the build command, it should be 
the name of a dub package, not the name of a file. Typically, 
you don't need to pass it a package name if all is properly 
configured.


Try this:

1 -- make sure dub is on the global path.

2 --
mkdir dubtest
cd dubtest
dub init

You should see the following output in side the dubtest dir

-dub.json
---source
-app.d

3 --
Edit dub.json and add the following dependencies:

derelict-sdl2:~1.0.0,
derelict-gl3:=1.0.0

4 --
Edit app.d to look like this:

import derelict.opengl3.gl3,
   derelict.sdl2.sdl;

void main()
{
DerelictGL3.load();
DerelictSDL2.load();

import std.stdio : writeln;
writeln( Success!);
}

5--
Execute the following command:
dub

You'll see some messages about fetching and building the 
Derelict packages and then, assuming you have SDL2 and OpenGL 
on your system library search path, you should see it print 
Success!. Otherwise, you'll get a DerelictException of one 
kind or another.


Note that I didn't give dub a command option. Executing 'dub' 
with no options will cause it to build and run whatever package 
is in the current directory. By default, dub will look for 
source/app.d if you don't configure something differently in 
dub.json.


Executing 'dub build' will build the executable, but will not 
run it.


This is a tad off topic now but I'm struggling to get a window on 
screen as SDL_CreateWindow is failing, here's what I've got:


try{
DerelictSDL2.load();
}catch(Exception e){
writeln(Failed to load DerelictSDL2 :( %s, e.msg);
return;
}

try{
SDL_Init(SDL_INIT_EVERYTHING);
}catch(Exception e){
 writeln(Can't init SDL :( %s, e.msg);
 return;
}


SDL_Window *testWindow;
//temporary magic numbers ahoy
testWindow = SDL_CreateWindow( Test Window, 
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 
SDL_WINDOW_RESIZABLE );

if( testWindow == null ) {
writeln(Window could not be created!);
return;
}   else {
SDL_Surface* testSurface;
testSurface = SDL_GetWindowSurface(testWindow);
}

Everything seems to initialise ok but SDL_CreateWindow always 
returns null. For some reason I can't use DerelictException which 
I guess might help me dig deeper. AFAIK there are no issues with 
this machine's hardware - I can certainly run other SDL-based 
apps on it.







Operator Overloading in an Abstract Base Class

2014-11-20 Thread Nordlöw

In my module symbolic.d at

https://github.com/nordlow/justd/blob/master/symbolic.d

is it somehow possible to overload binary operator ~ on pairs of 
instances SPatt?


I'm asking because my current try at

https://github.com/nordlow/justd/blob/master/symbolic.d#L332

is not called in the unittest at

https://github.com/nordlow/justd/blob/master/symbolic.d#L347

Isn't it possible to do operator overloading in an abstract 
base-class?


Re: Operator Overloading in an Abstract Base Class

2014-11-20 Thread Nordlöw

On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:

In my module symbolic.d at

https://github.com/nordlow/justd/blob/master/symbolic.d

is it somehow possible to overload binary operator ~ on pairs 
of instances SPatt?


As usual I had done a couple of mistakes...which are solved know 
at


https://github.com/nordlow/justd/blob/master/symbolic.d#L63

Why can't I find these bugs before I do my posts? :)


Re: print yyyy-mm-dd

2014-11-20 Thread uri via Digitalmars-d-learn

On Thursday, 20 November 2014 at 20:38:08 UTC, MrSmith wrote:

On Thursday, 20 November 2014 at 13:50:49 UTC, Suliman wrote:
I can't understand how to get date in format -MM-dd from 
Clock.currTime

auto time = Clock.currTime;

And what next? Could anybody give any examples?


http://dpaste.dzfl.pl/73c0438f9d1e

currTime returns SysTime;
You can then cast is to Date.
And Date has toISOExtString which converts Date to a string 
with the format -MM-DD.


There's no need to cast, SysTime supports conversion to string

http://dlang.org/library/std/datetime/SysTime.toISOExtString.html
http://dlang.org/library/std/datetime/SysTime.toISOString.html

If you want just the -MM-DD slice it

Clock.currTime.toISOExtString[0..10].writeln;
Clock.currTime.toISOString[0..8].writeln;

Cheers,
uri



Re: Operator Overloading in an Abstract Base Class

2014-11-20 Thread Ali Çehreli via Digitalmars-d-learn

On 11/20/2014 02:18 PM, Nordlöw wrote:

On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:

In my module symbolic.d at

https://github.com/nordlow/justd/blob/master/symbolic.d

is it somehow possible to overload binary operator ~ on pairs of
instances SPatt?


As usual I had done a couple of mistakes...which are solved know at

https://github.com/nordlow/justd/blob/master/symbolic.d#L63

Why can't I find these bugs before I do my posts? :)


Glad that it works. :)

Not directly related to your post but just because your post reminded 
me, there is the following feature that simplifies matters when one 
needs virtual operators:


  template opOpAssign(string op) if (op == ~)
  { alias concatAssign opOpAssign; }

Here is the enhancement request:

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

And here is the implementation including the tests:


https://github.com/D-Programming-Language/dmd/pull/989/files#diff-51d0a1ca6214e6a916212fcbf93d7e40R246

Ali



Re: Operator Overloading in an Abstract Base Class

2014-11-20 Thread IgorStepanov via Digitalmars-d-learn

On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:

In my module symbolic.d at

https://github.com/nordlow/justd/blob/master/symbolic.d

is it somehow possible to overload binary operator ~ on pairs 
of instances SPatt?


I'm asking because my current try at

https://github.com/nordlow/justd/blob/master/symbolic.d#L332

is not called in the unittest at

https://github.com/nordlow/justd/blob/master/symbolic.d#L347

Isn't it possible to do operator overloading in an abstract 
base-class?


Seq opBinary(string op)(Patt rhs)
{
static if (op == ~)
{
return seq(this, rhs);
}
else
{
static assert(false, Unsupported binary operator  ~ op);
}
}

As far I understood your question, you want to override opBinary 
in a derived class?
You can't do it directly, because opBinary is template and 
template can not be overriten. However, you may declare virtual 
non-template method for ~ and call it from opBinary.


And `static if (op == ~)` is not good. Use template constraint 
instead:


class Patt
{
// It is better then your static if.
// Compiler will raise better error message for incorrect 
operator.

Seq opBinary(string op)(Patt rhs) if (op == ~)
{
return opCatImpl(rhs);
}

protected Seq opCatImpl(Patt rhs) //you may override it in 
derived class

{
return seq(this, rhs);
}
}


Re: Dub / Derelict confusion

2014-11-20 Thread Mike Parker via Digitalmars-d-learn

On 11/21/2014 5:57 AM, Paul wrote:



This is a tad off topic now but I'm struggling to get a window on screen
as SDL_CreateWindow is failing, here's what I've got:

try{
 DerelictSDL2.load();
}catch(Exception e){
 writeln(Failed to load DerelictSDL2 :( %s, e.msg);
 return;
}


Before getting to your issue, I'd like to point out that this is a 
needless redundancy. You are adding anything by catching an exception 
here and printing to the console that the library didn't load. The 
exception message will tell you that already. I suggest you just remove 
the try..catch and let the exception propagate. The only reason to catch 
a DerelictException here is if you want to do something other than print 
the message to the console, like display a message box or write to a log 
file.





try{
 SDL_Init(SDL_INIT_EVERYTHING);
}catch(Exception e){
  writeln(Can't init SDL :( %s, e.msg);
  return;
}



SDL_Init isn't goint to throw an exception. It can't. It's a function in 
a C library and C doesn't know anything about D exceptions. If you look 
at the documentation for SDL_Init[1], you will find that it returns a 
negative number on failure and 0 on success.


if( SDL_Init( SDL_INIT_EVERYTHING )  0 ) {
 // See below for how to handle this
}



SDL_Window *testWindow;
//temporary magic numbers ahoy
testWindow = SDL_CreateWindow( Test Window, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE );
if( testWindow == null ) {
 writeln(Window could not be created!);
 return;
} else {
 SDL_Surface* testSurface;
 testSurface = SDL_GetWindowSurface(testWindow);
}

Everything seems to initialise ok but SDL_CreateWindow always returns
null. For some reason I can't use DerelictException which I guess might
help me dig deeper. AFAIK there are no issues with this machine's
hardware - I can certainly run other SDL-based apps on it.



Again, DerelictExceptions aren't going to help you here. They are only 
thrown when the load function fails (to manipulate a DerelictException 
directly, import derelict.util.exception.) To get an error message out 
of SDL, you need to call SDL_GetError, which returns a const( char )*.


void printSDLError( string msg ) {
  import std.conv : to;
  import std.stdio : writefln;

  writefln(%s: %s, msg, to!string( SDL_GetError() ));
}

...
if( testWindow == null )
{
  printSDLError( Failed to create window. );
  return;
}

I prefer to wrap it up in a subclass of Error. Others may prefer 
Exception, but when I throw an SDLError I don't intend to catch it. I 
really want the app to exit.


class SDLError : Error
{
public static string errStr() @property
{
import std.conv : to;
import derelict.sdl2.sdl : SDL_GetError;
return to!string( SDL_GetError() );
}

public this( string msg, string file = __FILE__, size_t line = 
__LINE__ )

{
import std.string : format;

auto fmsg = format( %s: %s, msg, errStr );
super( fmsg, file, line );
}
}

Now it becomes:

DerelictSDL2.load();

if( SDL_Init( SDL_INIT_EVERYTHING )  0 )
{
  throw new SDLError( Failed to initialize SDL );
}

testWindow = SDL_CreateWindow( ... );
if( !testWindow )
{
  throw new SDLError( Failed to create window );
}

Whichever approach you choose, SDL_GetError will shed light on why any 
SDL call failed as long as you call it immediately upon failure.


[1] 
https://wiki.libsdl.org/SDL_Init?highlight=%28%5CbCategoryInit%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29


Re: Dub / Derelict confusion

2014-11-20 Thread Mike Parker via Digitalmars-d-learn

On 11/21/2014 10:22 AM, Mike Parker wrote:


You are adding anything


You /aren't/