Re: betterC and noboundscheck

2017-11-24 Thread Michael V. Franklin via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:


import core.stdc.stdio;
import std.algorithm : min;

extern (C) void main()
{
char[256] buf;
buf[] = '\0';

auto str = "hello world";
auto ln = min(buf.length, str.length);
buf[0..ln] = str[0..ln];
printf("%s\n", buf.ptr);
}

rdmd -betterC bettercarray2.d

and get error:

/tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o:
 In function `main':
bettercarray2.d:(.text.main[main]+0xbc): undefined reference to 
`_d_arraycopy'

collect2: error: ld returned 1 exit status
Error: linker exited with status 1



Bug submitted here: https://issues.dlang.org/show_bug.cgi?id=18010


Re: behaviour of spawnProcess

2017-11-24 Thread Vladimir Panteleev via Digitalmars-d-learn

On Saturday, 25 November 2017 at 02:32:17 UTC, Fra Mecca wrote:
I have noticed that whenever j contains a string with a space 
in it, spawnprocess splits the string into another argument.


That shouldn't happen.

If you are on Windows, note that processes do not see the command 
line as an array of arguments, but as a single string. It is then 
the duty of the process to split up the string into individual 
arguments. If the program lets the C runtime do it by reading 
main's argc/argv, or uses CommandLineToArgvW (which is what C 
runtimes usually use under the hood), things generally work ask 
expected. However, some programs don't do that, and instead use 
their own custom logic for parsing the command line string (such 
as many built-in or standard DOS/Windows commands).


On POSIX, this can be caused if the program you're running is 
itself running another program, and isn't constructing its 
arguments correctly (e.g. it uses the system() function instead 
of an exec* function).


behaviour of spawnProcess

2017-11-24 Thread Fra Mecca via Digitalmars-d-learn

I have this snipper of code:

auto pid = spawnProcess([exe, j], po.readEnd, pi.writeEnd, 
std.stdio.stderr);


where exe is the executable name and j is argv[1].

I have noticed that whenever j contains a string with a space in 
it, spawnprocess splits the string into another argument.


In this way, even if I can use an array to index the arguments, 
the behaviour is very different from execvp or subprocess.call.


Is this desired? Or is it a bug?


Re: Floating point types default to NaN?

2017-11-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, November 24, 2017 20:43:14 A Guy With a Question via Digitalmars-
d-learn wrote:
> On Friday, 24 November 2017 at 14:43:24 UTC, Adam D. Ruppe wrote:
> > On Friday, 24 November 2017 at 14:30:44 UTC, A Guy With a
> >
> > Question wrote:
> >> I would have expected 0 to be the default value. What's the
> >> logic behind having them being NaN by default?
> >
> > It gives you a runtime error (sort of) if you use an
> > uninitialized variable.
> >
> > You ARE supposed to explicitly initialize variables to your own
> > values in D. The automatic init is to make errors stand out
> > more consistently if you don't do this as opposed to being
> > random.
> >
> > So pointers are initialized to null - an invalid value that
> > stands out if you try to use it. chars get \xFF - again,
> > invalid that will throw if you try to utf decode it. Floats get
> > NaN which is as close to invalid as they get.
> >
> > ints happen to get 0 not to be convenient, but because there is
> > no clearly-invalid int value so something had to be chosen, and
> > 0 was just easy to implement
>
> If thats the case why not just throw a compiler error? D has a
> way explicitly not set it right? Through void...So if the intent
> is to find erroneous code right away, just throw a compiler error
> no?

That requires data flow analysis, which the compiler doesn't do a lot of,
because it can be complicated. It also tends to result in the compiler
giving warnings or errors in cases where it's not actually true that the
variable is used before it's given a value, because it can't do it
perfectly. There was a recent discussion on this in the main newsgroup with
regards to guaranteeing with a pointer or reference was initialized to
something other than null.

Also, when you start having stuff like arrays, having a default initializer
is a big boon, since then all of the elements have a default value. It's
going to work even worse to try and have the compiler detect when you've
properly initialized every element in an array than have it detect when
you've properly initialized just a variable. And stuff like the out
attribute relies on there being a default initializer. Just all around,
having default initializers for types in general makes things cleaner and
less error-prone.

And NaN math is something that's built into CPU. Operations other than
default initialization can result in a floating point value being NaN. So,
if you're looking to have NaN not be a thing for floating point values, then
you're out of luck. And Walter is a big fan of NaN being a thing, since when
you hit it, you know that you've found a bug, and tracking it down tends to
be fairly straightforward, which is not the case with many other types of
bugs.

- Jonathan M Davis



Re: Floating point types default to NaN?

2017-11-24 Thread A Guy With a Question via Digitalmars-d-learn

On Friday, 24 November 2017 at 14:43:24 UTC, Adam D. Ruppe wrote:
On Friday, 24 November 2017 at 14:30:44 UTC, A Guy With a 
Question wrote:
I would have expected 0 to be the default value. What's the 
logic behind having them being NaN by default?



It gives you a runtime error (sort of) if you use an 
uninitialized variable.


You ARE supposed to explicitly initialize variables to your own 
values in D. The automatic init is to make errors stand out 
more consistently if you don't do this as opposed to being 
random.


So pointers are initialized to null - an invalid value that 
stands out if you try to use it. chars get \xFF - again, 
invalid that will throw if you try to utf decode it. Floats get 
NaN which is as close to invalid as they get.


ints happen to get 0 not to be convenient, but because there is 
no clearly-invalid int value so something had to be chosen, and 
0 was just easy to implement


If thats the case why not just throw a compiler error? D has a 
way explicitly not set it right? Through void...So if the intent 
is to find erroneous code right away, just throw a compiler error 
no?




Re: Error: 'this' is only defined in non-static member functions

2017-11-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/22/17 7:35 PM, Jonathan M Davis wrote:

On Thursday, November 23, 2017 00:17:46 A Guy With a Question via
Digitalmars-d-learn wrote:

here as non-static, nested class is associated with a specific
instance of the class and has access to that class instance via
its outer member.

- Jonathan M Davis


Hmmm...now you have me very intrigued. What is a use-case where
you'd want to use a non-static embedded class? Sorry if I'm
asking too many questions. But there's a lot to dig into with
this language.


It would make sense with something like the nodes of a linked list if they
needed access to the container for some reason. Pretty much any case where a
an instance of a nested class is going to be associated with a specific
instance of its parent class and needs access to it would be a canditate.
It's not that uncommon to see cases in C++ or Java where you'd pass a
pointer to the "parent" to an instance of a nested class when it's created,
and having outer built-in is kind of like that.


The intent is exactly for porting Java code that uses either anonymous 
classes or inner classes.


-Steve


Re: Passing this to void *

2017-11-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/23/17 12:57 AM, Nicholas Wilson wrote:

On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
    this() {
    Pizza newone = this;
    // works but newone is actually not this pizza.
    foo();
    // this does not work..
    foo(this);
    }
}

void main() {
    Pizza pizza = new Pizza();
    // this works...
    foo();
}


Note that all the examples and advice in this thread apply to _classes_, 
not to structs.


A further way to look at it, a class is really not the same as what you 
normally think as references (either ref parameter in D or & type 
constructor in C++). It's really simply a pointer that cannot be used 
with pointer math.


ref parameters:
* cannot be rebound (you can't make it point at another value)
* don't act any differently than auto-storage variables. Making a copy 
does not make a new reference, but rather copies the whole thing.


class references:
* CAN be rebound.
* copying to another variable does not copy the data, ever (you can't 
make a variable that represents the class data)


-Steve


Re: GUI program on Mac OS in D?

2017-11-24 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-11-24 16:09, Adam D. Ruppe wrote:


Thanks, this gets me started.

Do you happen to know if there is anything like "pragma(lib)" for the 
-framework argument? (I don't use dub, so I took your config there to 
make my own command line, but it would be nice if I didn't have to 
specify the framework. I use pragma(lib) on Windows and Linux so infer 
it automatically.)


There's not [1].

So my plan here is to get more of my libs working on Mac: update the old 
simpledisplay.d port so it runs on the native api again (currently it 
just uses XQuartz),


I think you should start by deciding if you want to use any .nib files 
or not. If you do use .nib files there is quite a lot of code that can 
be removed from my sample application.


then make my simpleaudio.d use OpenAL which I hear 
has been included with the mac os for some time


Yes, it's included. Apple has several audio frameworks [2], where OpenAL 
is one of them, for different purposes and different levels.


, then maybe even move on 
to minigui.d and have it use some native controls too, if reasonable
But the first step is just creating a basic window and your code worked, 
so thanks, I am on the path now...


BTW, the following line [3] of the Dub file will embed the Info.plist 
file in the executable, which can be handy if you don't want to use 
application bundles. The Info.plist file is not always necessary, I 
think my sample application will work without it, but for some things is 
necessary.


I also have a Dockerfile [4] with a cross-compiler setup that targets macOS.

You should also know that there's a bug in DMD, which I haven't manged 
to fix, that occurs when returning certain structs from Objective-C 
methods. I'm pretty sure if works correctly in LDC, since it's using the 
LLVM backend that already knows about Objective-C.


Finally, since you're using D you'll not have ARC (Automatic Reference 
Counting) which these days are preformed by the Objective-C and Swift 
compilers. You'll need to resort to traditional release/retain calls 
where appropriate. I have not included those calls in my sample application.


If you decide to use DStep, please file bugs and let me know how it 
works. It has not been battle tested for Objective-C code.


[1] https://issues.dlang.org/show_bug.cgi?id=2968

[2] 
https://developer.apple.com/library/content/documentation/MusicAudio/Conceptual/CoreAudioOverview/CoreAudioFrameworks/CoreAudioFrameworks.html


[3] https://github.com/jacob-carlborg/d_webkit_test/blob/master/dub.sdl#L12

[4] 
https://github.com/jacob-carlborg/docker-ldc-darwin/blob/master/Dockerfile


--
/Jacob Carlborg


Re: GUI program on Mac OS in D?

2017-11-24 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 23 November 2017 at 17:28:43 UTC, Jacob Carlborg 
wrote:
I have a simple example [2] of an application that shows a 
window with a WebKit view, i.e. and embedded browser.


Thanks, this gets me started.

Do you happen to know if there is anything like "pragma(lib)" for 
the -framework argument? (I don't use dub, so I took your config 
there to make my own command line, but it would be nice if I 
didn't have to specify the framework. I use pragma(lib) on 
Windows and Linux so infer it automatically.)


As you'll see there you need to use some parts of the 
Objective-C runtime to create class instances and subclasses. 
Also some gymnastics are required for class/static methods.


Yes, indeed. The old mac code I had here used all the extern(C) 
calls (it was contributed to me by a forum user, I didn't write 
it myself, but did look at it), so some of that is actually 
familiar.



So my plan here is to get more of my libs working on Mac: update 
the old simpledisplay.d port so it runs on the native api again 
(currently it just uses XQuartz), then make my simpleaudio.d use 
OpenAL which I hear has been included with the mac os for some 
time, then maybe even move on to minigui.d and have it use some 
native controls too, if reasonable.


But the first step is just creating a basic window and your code 
worked, so thanks, I am on the path now...




Re: Floating point types default to NaN?

2017-11-24 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 24 November 2017 at 14:30:44 UTC, A Guy With a 
Question wrote:
I would have expected 0 to be the default value. What's the 
logic behind having them being NaN by default?



It gives you a runtime error (sort of) if you use an 
uninitialized variable.


You ARE supposed to explicitly initialize variables to your own 
values in D. The automatic init is to make errors stand out more 
consistently if you don't do this as opposed to being random.


So pointers are initialized to null - an invalid value that 
stands out if you try to use it. chars get \xFF - again, invalid 
that will throw if you try to utf decode it. Floats get NaN which 
is as close to invalid as they get.


ints happen to get 0 not to be convenient, but because there is 
no clearly-invalid int value so something had to be chosen, and 0 
was just easy to implement


Floating point types default to NaN?

2017-11-24 Thread A Guy With a Question via Digitalmars-d-learn
I would have expected 0 to be the default value. What's the logic 
behind having them being NaN by default?


https://dlang.org/spec/type.html


Re: dirEntries() and exceptions

2017-11-24 Thread rjframe via Digitalmars-d-learn
On Fri, 24 Nov 2017 12:02:47 +, doc wrote:

> I'm trying recursively find files, and have some trouble to catch
> exceptions if have no permission to read directory.
> 
...
> 
> std.file.FileException@std/file.d(3798):
> /tmp/systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-
timesyncd.service-3374MK:
> Permission denied 
> 
> [code]
> void main() {
> import std.stdio, std.file;
> 
> try {
> auto farray = dirEntries("/tmp", "*.{d,py,ph,sh}",
> SpanMode.breadth);
> foreach (f; farray) {
> writeln(f);
> }
> catch (FileException e) {
> writeln(e.msg);
> }
> }
> [/code]
> 
> This not work, after writeln(e.msg); program is exit.

The exception for dirEntries was reported as a bug here[1]; there is a 
workaround listed on that page that silently skips directories it can't 
read, but you'll need to modify it a bit to maintain a breadth search.

As to why your code stops after catching the exception, the try/catch 
block does not place you back in the block that threw the exception, but 
drops you off in the outer scope. You would need to place the code in some 
sort of loop to run until all input was consumed in order continue past 
the exception.


[1]: https://issues.dlang.org/show_bug.cgi?id=12391


Re: Concurrency send immutable

2017-11-24 Thread SrMordred via Digitalmars-d-learn

Nice, thank you!


Re: Concurrency send immutable

2017-11-24 Thread drug via Digitalmars-d-learn

24.11.2017 15:53, SrMordred пишет:

On Friday, 24 November 2017 at 12:36:42 UTC, Daniel Kozak wrote:
Should print something like this: 
std.concurrency.OwnerTerminated@std/concurrency.d(223): Owner terminated


Yes, it was, I was aware of this and put some sleep after that too.


(immutable (int)[] v)


OK that parenteshis was the problem, thanks :)

But its a little confusing i think, since the syntax for creating the 
array is different from the syntax on the arguments.


and:

immutable int[];
immutable (int)[];
immutable (int[]);

(differences? what/when use)

Yes, it's different.
immutable int[] is equal to immutable (int[]) and means immutable slice 
of immutable int. on the other hand immutable (int)[] means _mutable_ 
slice of immutable int.


And other question:

When you send immutable data , i'm getting a copied data on the other 
thread, or its the same data?


Like:

send(thread1, arr);
send(thread2, arr);
send(thread3, arr);
send(thread4, arr);

(multiple copies, or all threads are seeing the same addresses ? )

the same data, it's the purpose of immutable



Re: Concurrency send immutable

2017-11-24 Thread SrMordred via Digitalmars-d-learn

On Friday, 24 November 2017 at 12:36:42 UTC, Daniel Kozak wrote:
Should print something like this: 
std.concurrency.OwnerTerminated@std/concurrency.d(223): Owner 
terminated


Yes, it was, I was aware of this and put some sleep after that 
too.



(immutable (int)[] v)


OK that parenteshis was the problem, thanks :)

But its a little confusing i think, since the syntax for creating 
the array is different from the syntax on the arguments.


and:

immutable int[];
immutable (int)[];
immutable (int[]);

(differences? what/when use)

And other question:

When you send immutable data , i'm getting a copied data on the 
other thread, or its the same data?


Like:

send(thread1, arr);
send(thread2, arr);
send(thread3, arr);
send(thread4, arr);

(multiple copies, or all threads are seeing the same addresses ? )


Re: Concurrency send immutable

2017-11-24 Thread Daniel Kozak via Digitalmars-d-learn
import std.stdio;
import std.traits;

int main(string[] args)
{
immutable int[] arr = [1,2,3,4,5];
writeln(ImplicitConversionTargets!(typeof(arr)).stringof);
return 0;
}

On Fri, Nov 24, 2017 at 1:36 PM, Daniel Kozak  wrote:

> Should print something like this:
> std.concurrency.OwnerTerminated@std/concurrency.d(223): Owner terminated
>
> Because main thread is terminated, because types do not match
>
> this will work
> import std.stdio;
> import std.concurrency;
>
> void fun()
> {
> receive( (immutable (int)[] v) => writeln(v) );
> }
>
> int main(string[] args)
> {
> immutable int[] arr = [1,2,3,4,5];
> auto t = spawn();
> t.send(arr);
> return 0;
> }
>
> On Fri, Nov 24, 2017 at 1:06 PM, SrMordred via Digitalmars-d-learn <
> digitalmars-d-learn@puremagic.com> wrote:
>
>> On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:
>>
>>> immutable int[] arr = [1,2,3,4,5];
>>> auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
>>> t.send(arr);
>>>
>>> whats the problem here?
>>>
>>
>> Nothing prints out
>>
>
>


Re: Concurrency send immutable

2017-11-24 Thread Daniel Kozak via Digitalmars-d-learn
Should print something like this:
std.concurrency.OwnerTerminated@std/concurrency.d(223): Owner terminated

Because main thread is terminated, because types do not match

this will work
import std.stdio;
import std.concurrency;

void fun()
{
receive( (immutable (int)[] v) => writeln(v) );
}

int main(string[] args)
{
immutable int[] arr = [1,2,3,4,5];
auto t = spawn();
t.send(arr);
return 0;
}

On Fri, Nov 24, 2017 at 1:06 PM, SrMordred via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:
>
>> immutable int[] arr = [1,2,3,4,5];
>> auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
>> t.send(arr);
>>
>> whats the problem here?
>>
>
> Nothing prints out
>


Re: Concurrency send immutable

2017-11-24 Thread SrMordred via Digitalmars-d-learn

On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:

immutable int[] arr = [1,2,3,4,5];
auto t = spawn({ receive( (immutable int[] v) => writeln(v) 
);});

t.send(arr);

whats the problem here?


Nothing prints out


Concurrency send immutable

2017-11-24 Thread SrMordred via Digitalmars-d-learn

immutable int[] arr = [1,2,3,4,5];
auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
t.send(arr);

whats the problem here?




dirEntries() and exceptions

2017-11-24 Thread doc via Digitalmars-d-learn
I'm trying recursively find files, and have some trouble to catch 
exceptions if have no permission to read directory.


[code]
void main() {
import std.file,std.stdio;
auto farray = dirEntries("/tmp", "*.{d,py,pl,sh}", 
SpanMode.breadth);

foreach (f; farray){writeln(f);}}
[/code]

std.file.FileException@std/file.d(3798): 
/tmp/systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK: Permission denied


??:? @safe core.sys.posix.dirent.DIR* 
std.file.cenforce!(core.sys.posix.dirent.DIR*).cenforce(core.sys.posix.dirent.DIR*, lazy const(char)[], immutable(char)[], ulong) [0xd31f7ce7]
??:? bool std.file.DirIteratorImpl.stepIn(immutable(char)[]) 
[0xd31f7119]

??:? void std.file.DirIteratorImpl.popFront() [0xd31f7641]
??:? void std.file.DirIterator.popFront() [0xd31f78dc]
??:? _Dmain [0xd31f1ff2]


Example structure /tmp

.
├── 374e60aa-296f-4499-9d12-36626a105069
├── 6787feed-013f-4907-bf86-b18a5a7c966f
├── dub_platform_probe-0d2aed9d-4a4f-4da3-a9fe-af705fb4914c.d
├── dub_platform_probe-3e5246ea-7158-4184-865e-8f15ec149b6f.d
├── dub_platform_probe-724ee4cf-bdc9-4137-b25a-4893323922ac.d
├── dub_platform_probe-9a0c98e4-3077-460c-9309-b8066be1b353.d
├── dub_platform_probe-be30fcdc-3eb2-4a0c-88bd-49a8507eb48b.d
├── dub_platform_probe-dc6d01e2-4f5d-4e71-adde-70131b8a0587.d
├── dub_platform_probe-dcd282e3-1b9c-486a-84ee-5eee5c60790f.d
├── dub_platform_probe-e2dfd83f-5f21-401c-a450-20bf5e73a678.d
├── ls
├── ls.d
├── ls.o
├── 
systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK [error opening dir]

├── vscode-e6da53a1363a02042f56966c5464593959886044.sock

drwx-- 3 root root60 ноя 24 08:32  
systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK


This dir have permission 0700, only root can read in this dir. 
Also, in /tmp I'm create at root a new dir /tmp/www with 0700 
perm. But my program has exit when exception on 
/tmp/systed-private... and can't write any messages when can't 
read /tmp/www.


So, how I can skip any exceptions and continue program?

[code]
void main() {
import std.stdio, std.file;

try {
auto farray = dirEntries("/tmp", "*.{d,py,ph,sh}", 
SpanMode.breadth);

foreach (f; farray) {
writeln(f);
}
catch (FileException e) {
writeln(e.msg);
}
}
[/code]

This not work, after writeln(e.msg); program is exit.


Re: Range violation

2017-11-24 Thread Mike Parker via Digitalmars-d-learn

On Friday, 24 November 2017 at 09:59:13 UTC, Vino wrote:


if (args.length < 1 || args.length > 2) {
writeln(args[0].baseName, ":No Arguments Provided");
exit(-1);
}


When you pass no arguments, this won't execute as args.length 
will still be 1, the only argument being the path to the 
executable -- args[0], of which you are apparently already aware. 
The if conditional should be this to test for no args:


if(args.length == 1)


string op = args[1];


Because the above conditional passes when you pass no args, this 
causes a range violation since args[1] doesn't exist.


Re: Range violation

2017-11-24 Thread Timoses via Digitalmars-d-learn

On Friday, 24 November 2017 at 09:59:13 UTC, Vino wrote:


if (args.length < 1 || args.length > 2) {


If args.length is 1 it will call


string op = args[1];


However, args[1] accesses the second element. Due to above if 
statement args[1] can be called even though only args[0] exists.


Range violation

2017-11-24 Thread Vino via Digitalmars-d-learn

Hi All,

   Request your help on the below program, when I execute the 
program without passing any arguments it is throwing Range 
violation errors


Error:
core.exception.RangeError@test.d(21): Range violation

0x004068CC
0x00408CF3
0x00408CB7
0x00408BB8
0x0040558F
0x74FE336A in BaseThreadInitThunk
0x770F98F2 in RtlInitializeExceptionChain
0x770F98C5 in RtlInitializeExceptionChain

Program:
import std.stdio;
import std.getopt;
import std.path;
import core.stdc.stdlib: exit;

void Test1() {
writeln("This is Test1");
}
void Test2() {
writeln("This is Test2");
}
void Test3() {
writeln("This is Help");
}

void main (string[] args) {
if (args.length < 1 || args.length > 2) {
writeln(args[0].baseName, ":No Arguments Provided");
exit(-1);
}

string op = args[1];
getopt(args, std.getopt.config.caseInsensitive, 
std.getopt.config.stopOnFirstNonOption);


switch (op) {
case "dryrun" , "run" :
Test1;
Test2;
break;
case "help" :
Test3;
break;
default :
 writefln("Unknown operation");
 }
}

From,
Vino.B