Re: wstring comparison is failing

2019-09-24 Thread Vladimir Panteleev via Digitalmars-d-learn

On Tuesday, 24 September 2019 at 21:40:47 UTC, Brett wrote:
The only issue is that buggy dynamic code can result if someone 
compares the two and it will fail silently.


But, you don't know if the static array actually contains a 
null-terminated string (in which case the comparison is a bug) or 
an actual meaningful string of that length, such as a string 
literal represented as a static array (in which case the 
comparison is completely valid).



It could be quite complex bug that destroys the space station.


The worst kind of bugs are the ones for which the program works 
correctly 99.9% of the time (including in all unit tests), but 
0.1% of the time things explode. This doesn't seem to me like 
that kind of a bug, if only that a NUL character will always be 
presented in a zero-terminated string embedded in a static array, 
but practically never in a D string.


If I convert a static array to a string, I want the "string" 
portion of the array.


Zero-terminated strings are kind of a C thing. D isn't C, it has 
its own string type. Even though many libraries use a C ABI for 
interop, and thus use C strings, this isn't a requirement for a D 
program - after all, you can write your own kernel and operating 
system in D, all fully without zero-terminated strings.




Re: wstring comparison is failing

2019-09-24 Thread Brett via Digitalmars-d-learn
On Tuesday, 24 September 2019 at 00:29:05 UTC, Vladimir Panteleev 
wrote:

On Monday, 23 September 2019 at 23:22:14 UTC, Brett wrote:
I guess you are probably right... I was thinking that it would 
compare up to a null terminator. Seems kinda buggy... maybe 
the compiler needs to give a warning? After all, compared a 
fixed size array with a dynamic array then will almost always 
fail since it is unlikely the sizes will match...


...rather than failing silently.


It might still be useful for generic code, so a compiler 
warning would probably not be suitable. However, it might be a 
good idea for a linter such as Dscanner, which, unlike the 
compiler, sees types as they are written in the code, not as 
they are calculated through metaprogramming.


The only issue is that buggy dynamic code can result if someone 
compares the two and it will fail silently. It could be quite 
complex bug that destroys the space station.  Warnings never hurt 
and with the ability to disable specific ones one can bypass such 
issues. (disable then enable)


But if it behaves in a non-standard way based on some somewhat 
arbitrary limitation(fixed arrays are only arbitrary because of 
the limitations of computers) then it's going to bit people and 
the more D is used the more people that will get bitten.


If I convert a static array to a string, I want the "string" 
portion of the array. Strings are special, they are meant to have 
specific representation. Strings do not have 0 characters in them 
normally.


So upon conversion, to!string would detect this and either error 
out or stop the conversion at that point. The only time one would 
want to retain junk values is for displaying bytes, but then one 
should just convert to a byte or char array.


But of course those people that have be using to!string to 
convert arbitrary bytes will have their code broken... but I 
imagine few have done this(there really is no point).


The same thing should happen when converting anything to a 
string, it should detect malformed strings and either error or 
terminate(if zero terminated).


This makes for a more consistent ecosystem.

if, for example, I stick null terminated strings in a buffer then 
to!string will automatically recover them(the first one, assuming 
one each time).


There is probably no win-win situation though and so I suggest 
just a warning when converting arrays to strings.








Inspecting __traits(isDeprecated) and deprecation warnings

2019-09-24 Thread Anonymouse via Digitalmars-d-learn
I want to write a piece of code that reflects on the names of 
members of a passed struct, where some are depreacted.


https://run.dlang.io/is/P9EtRG

struct Foo
{
string s;
int ii;
bool bbb;

deprecated("Use `s`")
string ;
}

template longestMemberLength(T)
{
enum longestMemberLength = ()
{
size_t maxLength;

foreach (immutable i, immutable name; 
__traits(allMembers, T))

{
static if (!__traits(isDeprecated, 
__traits(getMember, T, name)))

{
maxLength = max(maxLength, name.length);
}
}

return maxLength;
}();
}

static assert (longestMemberLength!Foo == "bbb".length);

onlineapp.d(23): Deprecation: variable `onlineapp.Foo.` is 
deprecated - Use s


Is there any way to inspect the deprecated-ness of a member this 
way? I only have what __traits(allMembers) gives me.


Blog Post #73: The Frame, Part II

2019-09-24 Thread Ron Tarrant via Digitalmars-d-learn
Today we cover how to decorate the Frame... or UN-decorate it. 
Frames can be turned off or dressed up with CSS. To find out 
more, follow this link: 
https://gtkdcoding.com/2019/09/24/0073-frame-part-ii.html


Re: Why dynamic array is InputRange but static array not.

2019-09-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, September 24, 2019 1:35:24 AM MDT lili via Digitalmars-d-learn 
wrote:
> Hi:
>in phobos/std/range/primitives.d has this code
>   ```
> static assert( isInputRange!(int[]));
> static assert( isInputRange!(char[]));
> static assert(!isInputRange!(char[4]));
> static assert( isInputRange!(inout(int)[]));
>  ```
>   but the dynamic array and static array neither not has
> popFront/front/empty.
> https://dlang.org/spec/arrays.html#array-properties properties

Because for something to be a range, it must be possible for it to shrink.
popFront works with a dynamic array, because dynamicy arrays have a dynamic
size. It's basically just

void popFront(T)(ref T[] a)
{
a = a[1 .. $];
}

However, static arrays have a fixed size, so it's not possible to implement
popFront for them. If you want to use a static array as a range, then you
need to slice it to get a dynamic array - though when you do that, make sure
that the dynamic array is not around longer than the static array, because
it's just a slice of the static array, and if the static array goes out of
scope, then the dynamic array will then refer to invalid memory.

- Jonathan M Davis





Why dynamic array is InputRange but static array not.

2019-09-24 Thread lili via Digitalmars-d-learn

Hi:
  in phobos/std/range/primitives.d has this code
 ```
   static assert( isInputRange!(int[]));
   static assert( isInputRange!(char[]));
   static assert(!isInputRange!(char[4]));
   static assert( isInputRange!(inout(int)[]));
```
 but the dynamic array and static array neither not has 
popFront/front/empty.

https://dlang.org/spec/arrays.html#array-properties properties