Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 11, 2019 1:44:51 PM MDT Stefanos Baziotis via Digitalmars-
d-learn wrote:
> On Thursday, 11 July 2019 at 19:37:38 UTC, Nathan S. wrote:
> > If you know that what you're doing cannot result in memory
> > corruption but the compiler cannot automatically infer @safe,
> > it is appropriate to use @trusted. (For this case make sure
> > you're not returning the byte slices, since if the arguments
> > were allocated on the stack you could end up with a pointer to
> > an invalid stack frame. If it's the caller's responsibility to
> > ensure the slice doesn't outlive the struct then it is the
> > caller that should be @trusted or not.)
>
> Yes, @trusted is an option. I mean it's a good solution, but from
> the standpoint of the language user, it seems unfortunate that
> for the case static types
> @trusted has to be used while the array one can be @safe:
>
> int memcmp(T)(const T[] s1, const T[] s2) @safe
> {
>  const byte[] s1b = (cast(const(byte[]))s1)[0 .. s1.length *
> T.sizeof];
>  const byte[] s2b = (cast(const(byte[]))s2)[0 .. s2.length *
> T.sizeof];
> }

The compiler would have to have a deeper understanding of what you're doing
in order to know that it's @safe, and it's just not that smart. That's why
@trusted is needed in general. Just because something is obvious to the
programmer doesn't mean that it's at all obvious to the compiler. In some
cases, the compiler could be improved, but in many, it really just comes
down to the programmer verifying that the code is doing the correct thing
and using @trusted.

BTW, if you're implementing memcmp, why are you using byte instead of ubyte?
byte is signed. Unless you're explicitly trying to do arithmetic on integral
values from -127 to 127, odds are, you shouldn't be using byte. If you're
doing something like breaking an integer into its 8-bit parts, then ubyte is
what's appropriate, not byte.

- Jonathan M Davis





Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 11 July 2019 at 19:44:51 UTC, Stefanos Baziotis 
wrote:

On Thursday, 11 July 2019 at 19:37:38 UTC, Nathan S. wrote:


If you know that what you're doing cannot result in memory 
corruption but the compiler cannot automatically infer @safe, 
it is appropriate to use @trusted. (For this case make sure 
you're not returning the byte slices, since if the arguments 
were allocated on the stack you could end up with a pointer to 
an invalid stack frame. If it's the caller's responsibility to 
ensure the slice doesn't outlive the struct then it is the 
caller that should be @trusted or not.)


Yes, @trusted is an option. I mean it's a good solution, but 
from the standpoint of the language user, it seems unfortunate 
that for the case static types

@trusted has to be used while the array one can be @safe:

int memcmp(T)(const T[] s1, const T[] s2) @safe
{
const byte[] s1b = (cast(const(byte[]))s1)[0 .. s1.length * 
T.sizeof];
const byte[] s2b = (cast(const(byte[]))s2)[0 .. s2.length * 
T.sizeof];

}


You can use a union:

int foo(T)(ref T s1, ref T s2) {
import std.stdio;

union U {
T val;
byte[T.sizeof] bytes;
}

const U s1u = { val: s1 };
const U s2u = { val: s2 };

writeln("s1 bytes: ", s1u.bytes);
writeln("s2 bytes: ", s2u.bytes);

return 0;
}

@safe void main() {
double a = 12.345, b = 67.890;
foo(a, b);
}

However, accessing the `bytes` member will still be considered 
@system if T is or contains a pointer. To fix this, you can use a 
@trusted nested function to do the union access; e.g.,


@trusted ref const(ubyte[T.sizeof]) getBytes(ref U u) { 
return u.bytes; }

// ...
writeln("s1 bytes: ", getBytes(s1u));


Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Stefanos Baziotis via Digitalmars-d-learn

On Thursday, 11 July 2019 at 19:37:38 UTC, Nathan S. wrote:


If you know that what you're doing cannot result in memory 
corruption but the compiler cannot automatically infer @safe, 
it is appropriate to use @trusted. (For this case make sure 
you're not returning the byte slices, since if the arguments 
were allocated on the stack you could end up with a pointer to 
an invalid stack frame. If it's the caller's responsibility to 
ensure the slice doesn't outlive the struct then it is the 
caller that should be @trusted or not.)


Yes, @trusted is an option. I mean it's a good solution, but from 
the standpoint of the language user, it seems unfortunate that 
for the case static types

@trusted has to be used while the array one can be @safe:

int memcmp(T)(const T[] s1, const T[] s2) @safe
{
const byte[] s1b = (cast(const(byte[]))s1)[0 .. s1.length * 
T.sizeof];
const byte[] s2b = (cast(const(byte[]))s2)[0 .. s2.length * 
T.sizeof];

}




Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Nathan S. via Digitalmars-d-learn
On Thursday, 11 July 2019 at 16:31:58 UTC, Stefanos Baziotis 
wrote:

I searched the forum but did not find something.

I want to do this:

int foo(T)(ref T s1, ref T s2)
{
const byte[] s1b = (cast(const(byte)*))[0 .. T.sizeof];
const byte[] s2b = (cast(const(byte)*))[0 .. T.sizeof];
}

Which is to create a byte array from the bytes of the value 
given, no matter

the type. The above works, but it's not @safe.

Thanks,
Stefanos


If you know that what you're doing cannot result in memory 
corruption but the compiler cannot automatically infer @safe, it 
is appropriate to use @trusted. (For this case make sure you're 
not returning the byte slices, since if the arguments were 
allocated on the stack you could end up with a pointer to an 
invalid stack frame. If it's the caller's responsibility to 
ensure the slice doesn't outlive the struct then it is the caller 
that should be @trusted or not.)


Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Stefanos Baziotis via Digitalmars-d-learn

On Thursday, 11 July 2019 at 18:46:57 UTC, Paul Backus wrote:


Casting from one type of pointer to another and slicing a 
pointer are both @system, by design.


Yes, I'm aware, there are no pointers in the code. The pointer 
was used
here because it was the only way to solve the problem (but not in 
@safe).


What's the actual problem you're trying to solve? There may be 
a different way to do it that's @safe.


I want to make an array of bytes that has the bytes of the value 
passed.
For example, if T = int, then I want an array of 4 bytes that has 
the 4
individual bytes of `s1` let's say. For long, an array of 8 bytes 
etc.
Ideally, that would work with `ref` (i.e. the bytes of where the 
ref points to).


Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Stefanos Baziotis via Digitalmars-d-learn

On Thursday, 11 July 2019 at 18:46:57 UTC, Paul Backus wrote:
What's the actual problem you're trying to solve? There may be 
a different way to do it that's @safe.


I hope I answered the "what". In case the "why" helps too, it is 
because

I'm implementing memcmp().


Re: "lld-link: error: could not open libcmt.lib: no such file or directory"

2019-07-11 Thread moth via Digitalmars-d-learn
in case anyone else suffers from this in the future and is 
looking for a solution - redownloading visual studio 2017 and 
making sure the c++ workload was enabled fixed this for me. i'm 
not really sure why that worked, but i'm just happy to be able to 
learn again.


trans rights!
- moth



Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 11 July 2019 at 16:31:58 UTC, Stefanos Baziotis 
wrote:

I searched the forum but did not find something.

I want to do this:

int foo(T)(ref T s1, ref T s2)
{
const byte[] s1b = (cast(const(byte)*))[0 .. T.sizeof];
const byte[] s2b = (cast(const(byte)*))[0 .. T.sizeof];
}

Which is to create a byte array from the bytes of the value 
given, no matter

the type. The above works, but it's not @safe.

Thanks,
Stefanos


Casting from one type of pointer to another and slicing a pointer 
are both @system, by design. What's the actual problem you're 
trying to solve? There may be a different way to do it that's 
@safe.


Is there a way to slice non-array type in @safe?

2019-07-11 Thread Stefanos Baziotis via Digitalmars-d-learn

I searched the forum but did not find something.

I want to do this:

int foo(T)(ref T s1, ref T s2)
{
const byte[] s1b = (cast(const(byte)*))[0 .. T.sizeof];
const byte[] s2b = (cast(const(byte)*))[0 .. T.sizeof];
}

Which is to create a byte array from the bytes of the value 
given, no matter

the type. The above works, but it's not @safe.

Thanks,
Stefanos


Re: Blog Post #0051: MVC IV - ComboBox with Text

2019-07-11 Thread Ron Tarrant via Digitalmars-d-learn

On Wednesday, 10 July 2019 at 20:31:36 UTC, Greatsam4sure wrote:

Is it possible to get all the tutorials in a pdf file for 
offline work? Thanks in advance


Just found out about this on FB. Packt is having a sale right 
now, 3 ebooks for $30 and they have three books about D.


https://www.packtpub.com/catalogsearch/result/?q=d+programming=IwAR3pAZYU8uLmLJIdxrOiptVrBbikGbsDGBFvAPFyiYqW08pI0EMP3mjnc7k


Re: LDC won't find ld linker -why?

2019-07-11 Thread Dukc via Digitalmars-d-learn

On Wednesday, 10 July 2019 at 23:15:38 UTC, Johan Engelen wrote:

On Tuesday, 9 July 2019 at 15:25:17 UTC, Dukc wrote:
I just downloaded ldc 1.15.0 for Linux from GH releases. 
Testing it, it will make the object file out of a hello world 
application, but then complain:

```
collect2: fatal error: cannot find ‘ld’
compilation terminated.
```


Run LDC with "-v" and check what the linker command is 
(somewhere at the bottom, LDC invokes `gcc` for linking). You 
might see something like `-Wl,-fuse-ld=...` in that gcc command 
line. Perhaps that ld.gold / ld.bfd is not installed and thus 
GCC complains that it cannot find it.
See e.g. 
https://askubuntu.com/questions/798453/collect2-fatal-error-cannot-find-ld-compilation-terminated


-Johan


Thanks, that worked, I needed ld.gold. There was a catch, 
however: one would think that `llvm8-gold` is the .rpm package in 
question, but installing it did change nothing. Good that you 
provided that link, otherwise I would never have quessed that I 
have to install `binutils-gold` instead.


Re: Is it possible to execute a function inside .d script that has no main function?

2019-07-11 Thread Alex via Digitalmars-d-learn

On Thursday, 11 July 2019 at 09:43:55 UTC, BoQsc wrote:

Here I have a file named: module.d


import std.stdio   : writeln;

void interestingFunction(){
writeln("Testing");
}


There is no main() function since, I want to import this 
module, into another .d file.


( If I try to import and module.d does have main() function I 
get this error: )
otherFile.d(13): Error: only one main, WinMain, or DllMain 
allowed. Previously found main at module.d(3)






I would like to launch function "interestingFunction()" 
directly using rdmd.

Is it possible to achieve that by any way?





I tried to launch the whole file, but it gave me some weird 
output:

C:\Users\User\Desktop\Environment variables>rdmd module.d
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 134: No Start Address


If you are trying to test a function separately from other 
functions, e.g. for testing, then you are approaching unit 
testing.
You could write a unit test block just under the function to test 
like

unittest
{
  interestingFunction;
}

and launch it via rdmd... don't know the syntax right now. But 
here is something:

https://stackoverflow.com/questions/10694994/rdmd-is-not-running-unit-tests

So, I assume something like
rdmd -unittest --main module.d


Is it possible to execute a function inside .d script that has no main function?

2019-07-11 Thread BoQsc via Digitalmars-d-learn

Here I have a file named: module.d


import std.stdio   : writeln;

void interestingFunction(){
writeln("Testing");
}


There is no main() function since, I want to import this module, 
into another .d file.


( If I try to import and module.d does have main() function I get 
this error: )
otherFile.d(13): Error: only one main, WinMain, or DllMain 
allowed. Previously found main at module.d(3)






I would like to launch function "interestingFunction()" directly 
using rdmd.

Is it possible to achieve that by any way?





I tried to launch the whole file, but it gave me some weird 
output:

C:\Users\User\Desktop\Environment variables>rdmd module.d
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 134: No Start Address


Re: How to use std.windows.registry, there are no documentations.

2019-07-11 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 11 July 2019 at 08:53:35 UTC, BoQsc wrote:

https://dlang.org/phobos/std_windows_registry.html
https://github.com/dlang/phobos/blob/master/std/windows/registry.d

Can someone provide some examples on how to:
set, change, receive something from the Windows registry using 
Phobos std.windows.registry library?


I didn't know that existed, but the source code is documented but 
not in the style that the doc generated recognizes as far as I 
can tell.


How to use std.windows.registry, there are no documentations.

2019-07-11 Thread BoQsc via Digitalmars-d-learn

https://dlang.org/phobos/std_windows_registry.html
https://github.com/dlang/phobos/blob/master/std/windows/registry.d

Can someone provide some examples on how to:
set, change, receive something from the Windows registry using 
Phobos std.windows.registry library?


Re: Blog Post #0051: MVC IV - ComboBox with Text

2019-07-11 Thread Ron Tarrant via Digitalmars-d-learn

On Wednesday, 10 July 2019 at 20:31:36 UTC, Greatsam4sure wrote:

On Tuesday, 9 July 2019 at 12:08:04 UTC, Ron Tarrant wrote:
Today's post starts a mini series-within-a-series on dressing 
up the ComboBox using a ListStore. Essentially, it's ListStore 
basics leading up to how this type of model is used with a 
TreeView.


You can find it here: 
https://gtkdcoding.com/2019/07/09/0051-mvc-iv-combobox-text.html


Great work! I see this series of tutorial becoming a book for 
gtkd. Is it possible to get all the tutorials in a pdf file for 
offline work? Thanks in advance


I do have this planned, but I want to get through the basic stuff 
first. So, there may be a bit of a wait.