Re: wstring double quotes to string double quotes

2018-04-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, April 19, 2018 23:24:05 Joel via Digitalmars-d-learn wrote:
> On Thursday, 19 April 2018 at 21:57:28 UTC, Adam D. Ruppe wrote:
> > On Thursday, 19 April 2018 at 21:53:52 UTC, Joel wrote:
> >> I have a program that uses string double quotes, but copies
> >> from wstring double quotes. The wstring double quotes are in
> >> string type (sourceTxt is a string with wstring double quotes).
> >
> > quotes are quotes, you don't need to convert to wstring here.
> >
> > I really don't think it should be throwing that error
> > regardless... but you also should be able to just do
> >
> > string[] sourceLines = sourceTxt.replace("”", `"`).split("\n");
> >
> > and skip the wstring part entirely.
>
> That worked! Thanks Adam.

Given that these functions really shouldn't be throw RangeErrors, please
create a bug report with example code that can someone can just run to
reproduce the issue (your example isn't runnable as-is). That way, the bug
can be fixed. Otherwise, it's probably just going to be lost, and someone
else may hit it in the future. Thanks.

https://issues.dlang.org

- Jonathan M Davis




Re: wstring double quotes to string double quotes

2018-04-19 Thread Joel via Digitalmars-d-learn

On Thursday, 19 April 2018 at 21:57:28 UTC, Adam D. Ruppe wrote:

On Thursday, 19 April 2018 at 21:53:52 UTC, Joel wrote:
I have a program that uses string double quotes, but copies 
from wstring double quotes. The wstring double quotes are in 
string type (sourceTxt is a string with wstring double quotes).


quotes are quotes, you don't need to convert to wstring here.

I really don't think it should be throwing that error 
regardless... but you also should be able to just do


string[] sourceLines = sourceTxt.replace("”", `"`).split("\n");

and skip the wstring part entirely.


That worked! Thanks Adam.


Re: wstring double quotes to string double quotes

2018-04-19 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 19 April 2018 at 21:53:52 UTC, Joel wrote:
I have a program that uses string double quotes, but copies 
from wstring double quotes. The wstring double quotes are in 
string type (sourceTxt is a string with wstring double quotes).


quotes are quotes, you don't need to convert to wstring here.

I really don't think it should be throwing that error 
regardless... but you also should be able to just do


string[] sourceLines = sourceTxt.replace("”", `"`).split("\n");

and skip the wstring part entirely.


wstring double quotes to string double quotes

2018-04-19 Thread Joel via Digitalmars-d-learn
I have a program that uses string double quotes, but copies from 
wstring double quotes. The wstring double quotes are in string 
type (sourceTxt is a string with wstring double quotes).


The following code crashes with an array.d(2211): Range violation 
error:


import std.conv : to;
import std.string : replace;

auto sourceTxtW = sourceTxt.to!wstring;
sourceTxtW.replace("”", `"`);
auto sourceTxtR = sourceTxtW.to!string;
sourceLines = sourceTxtR.split("\n");


Re: Getting the overload set of a template

2018-04-19 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 19 April 2018 at 13:57:04 UTC, Simen Kjærås wrote:

On Tuesday, 17 April 2018 at 14:22:27 UTC, Arafel wrote:

Hi!

Is there any way to get the full set of templates that are 
"overloaded" (in my case, based on constraints)?


Currently, there is no way (that I've found, at least) to do 
this. If you have a workaround, that's great, but there really 
should be a way - probably __traits(getOverloads). Having 
__traits(getOverloads) return templates as well should fix some 
of the issues __traits(getOverloads) has, as a bonus.


https://github.com/dlang/dmd/pull/8195

--
  Simen


Re: Getting the overload set of a template

2018-04-19 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 19 April 2018 at 14:16:21 UTC, Alex wrote:

On Thursday, 19 April 2018 at 13:57:04 UTC, Simen Kjærås wrote:
Currently, there is no way (that I've found, at least) to do 
this. If you have a workaround, that's great, but there really 
should be a way - probably __traits(getOverloads). Having 
__traits(getOverloads) return templates as well should fix 
some of the issues __traits(getOverloads) has, as a bonus.




Would it be possible at all? I mean, if the two following codes 
are equivalent

´´´
@S("Has foo_A") template foo(string s) if (s == "a") {
enum foo = "foo_A";
}
@S("Has foo_B") template foo(string s) if (s == "b") {
enum foo = "foo_B";
}
´´´


´´´
template foo(string s)
{
static if (s == "a")
{
   @S("Has foo_A") enum foo = "foo_A";
}
else static if (s == "b")
{
  @S("Has foo_B") enum foo = "foo_B";
}
}
´´´

How would you define a "template overload"?
And which "overloads" would you like to get if constraints are 
more general?
And last but not least, the getOverloads is defined on 
functions, which are callable, whereas templates are not, in 
general...


Your first example defines two templates (which are overloads of 
the same name), the second only one. There's no ambiguity there.


Since template instantiation is analogous to function calling 
(though one is a compile-time action, the other a run-time one), 
talking about template overloads in the same way as function 
overloads makes perfect sense. Whether the result of 
instantiating the template is a function, a value, or a type, 
it's an overload of the template.


--
  Simen


Re: get literal symbol name without base class/struct as string

2018-04-19 Thread Timoses via Digitalmars-d-learn

On Tuesday, 17 April 2018 at 21:45:45 UTC, Dr.No wrote:

give structs like this:

struct A
{
int a = 10;
string s = "haha";
}

struct B
{
A aDetails;
}

but neither fullyQualifiedName nor stringof give the symbol in 
the way I need.


I'd hint you towards

import std.traits;
pragma(msg, FieldNameTuple!B);
pragma(msg, FieldNameTuple!(Fields!B[0]));


Re: Getting the overload set of a template

2018-04-19 Thread Arafel via Digitalmars-d-learn
Well, if that's the lowering, then it's indeed hard. That doesn't mean 
it shouldn't happen, though... perhaps changing the lowering? I'm no 
compiles expert, so no idea how).


What I'd like to get is the same that I get using 
__traits(getMember,...), but repeated n times (AliasSeq perhaps?), like 
with regular overloads.


Then, whatever I can do with the first entry (the only one I can get 
currently) should also be possible with the rest. In my case, I'd like 
to access the UDAs, but I can imagine that the use case that allows us 
to get a template keeps being valid for all the "hidden" alternatives.


Also, I think that whether to use "getOverloads" or to add a new trait 
is rather an implementation detail


It's a bit frustrating being able to access only the first of a set...

A.

> Would it be possible at all? I mean, if the two following codes are 
equivalent

> ´´´
>  @S("Has foo_A") template foo(string s) if (s == "a") {
>  enum foo = "foo_A";
>  }
>  @S("Has foo_B") template foo(string s) if (s == "b") {
>  enum foo = "foo_B";
>  }
> ´´´
>
>
> ´´´
>  template foo(string s)
>  {
>  static if (s == "a")
>  {
> @S("Has foo_A") enum foo = "foo_A";
>  }
>  else static if (s == "b")
>  {
>@S("Has foo_B") enum foo = "foo_B";
>  }
>  }
> ´´´
>
> How would you define a "template overload"?
> And which "overloads" would you like to get if constraints are more 
general?
> And last but not least, the getOverloads is defined on functions, 
which are callable, whereas templates are not, in general...


getSymbolByUDA and inheritance

2018-04-19 Thread Arafel via Digitalmars-d-learn

Hi,

getSymbolsByUDA doesn't work when inheritance is involved [1]:

```
import std.traits;

void main()
{
pragma(msg, getSymbolsByUDA!(A,S).length);
pragma(msg, getSymbolsByUDA!(B,S).length);
}

class A {
@S("A") int a;
}

class B : A {
@S("B") int b;
}

struct S {
string name;
}
```

The error message seems a bit weird, and after some tinkering, I've been 
able to reproduce it when creating an AliasSeq with members of both the 
parent and the child class [2]:


```
import std.meta;

void main()
{
	pragma(msg, AliasSeq!(__traits(getMember, B, "a"), __traits(getMember, 
B, "b")));

}

class A {
int a;
}

class B : A {
int b;
}
```

It seems that using __traits(getMember,...) is introducing some kind of 
hidden context to the actual class that defines the member... It looks 
like a bug to me, but there might be a reason to do it this way.


Still, whatever the reason, it definitely breaks getSymbolsByUDA when 
inheritance is involved. According to the documentation [3] only nested 
members are excluded (and in any they are just excluded, but still 
compile), so is this a bug?


[1]: https://run.dlang.io/is/502CUB
[2]: https://run.dlang.io/is/9wOIsa
[3]: https://dlang.org/library/std/traits/get_symbols_byuda.html


Re: Store struct tuple of alias and access members through it?

2018-04-19 Thread Timoses via Digitalmars-d-learn

On Saturday, 7 April 2018 at 19:21:30 UTC, Simen Kjærås wrote:

import std.meta;
import std.traits;

// List all member functions, and wrap them such that 
myFoo.fun(3) can be called as 
AllMemberFunctions!(typeof(myFoo))[idx](myFoo, 3).

template AllMemberFunctions(T)
{
template createDg(alias fn)
{
static if (__traits(isStaticFunction, fn))
alias createDg = fn;
else
ReturnType!fn createDg(ref T ctx, Parameters!fn 
args)

{
ReturnType!fn delegate(Parameters!fn) fun;
fun.funcptr = 
fun.ptr = cast(void*)
return fun(args);
}
}

alias GetOverloads(string name) = 
AliasSeq!(__traits(getOverloads, T, name));


alias AllMemberFunctions = staticMap!(createDg, 
staticMap!(GetOverloads, __traits(allMembers, T)));

}

--
  Simen


Many thanks for this!!! Was really helpful.

I ended up unfolding the struct members into an array of member 
strings and mapping those to either the struct tuple members or 
the struct function members.
This way I can call all members (normal and bitfield members) in 
order.


Result:
https://gist.github.com/Timoses/c78e599e91b8d05be34aefaf75ca3739


This project is really teaching me some template actions : D.


Re: Rotate array in writefln?

2018-04-19 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/18/18 3:15 AM, Simen Kjærås wrote:

On Wednesday, 18 April 2018 at 06:54:29 UTC, Chris Katko wrote:
I need to rotate an array by 90 degrees, or have writefln figure that 
out.


I need, say:

0 4 5 6
0 0 0 0
0 0 0 0
0 0 0 0

But it's outputting:

0 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0

int [4][4] data;
file.writeln(format("%(%-(%d %)\n%)", data));


Generally, the solution would be std.range.transposed. However, since 
you're using a int[4][4], that's not a range-of-ranges, and transposed 
don't work out of the box. This helper function should help:


T[][] ror(T, size_t N1, size_t N2)(ref T[N1][N2] arr)
{
     T[][] result = new T[][N2];
     foreach (i, e; arr) {
     result[i] = e.dup;
     }
     return result;
}

unittest
{
     import std.stdio;
     import std.range;

     int [4][4] data;
     data[2][3] = 4;
     writefln("%(%-(%d %)\n%)", data);
     writefln("%(%-(%d %)\n%)", data.ror.transposed);
}



A version without allocating:

T[][N2] ror(T, size_t N1, size_t N2)(ref T[N1][N2] arr)
{
T[][N2] result;
foreach (i, ref e; arr) {
result[i] = e[];
}
return result;
}

...

writefln("%(%-(%d %)\n%)" data.ror[].transposed); // need the slice 
operator here


Keep in mind, you can't simply assign a variable to data.ror[], as the 
backing goes away immediately (OK to use as an rvalue though). And you 
must keep data in scope as long as you are using the result of data.ror.


-Steve


Re: Getting the overload set of a template

2018-04-19 Thread Alex via Digitalmars-d-learn

On Thursday, 19 April 2018 at 13:57:04 UTC, Simen Kjærås wrote:
Currently, there is no way (that I've found, at least) to do 
this. If you have a workaround, that's great, but there really 
should be a way - probably __traits(getOverloads). Having 
__traits(getOverloads) return templates as well should fix some 
of the issues __traits(getOverloads) has, as a bonus.




Would it be possible at all? I mean, if the two following codes 
are equivalent

´´´
@S("Has foo_A") template foo(string s) if (s == "a") {
enum foo = "foo_A";
}
@S("Has foo_B") template foo(string s) if (s == "b") {
enum foo = "foo_B";
}
´´´


´´´
template foo(string s)
{
static if (s == "a")
{
   @S("Has foo_A") enum foo = "foo_A";
}
else static if (s == "b")
{
  @S("Has foo_B") enum foo = "foo_B";
}
}
´´´

How would you define a "template overload"?
And which "overloads" would you like to get if constraints are 
more general?
And last but not least, the getOverloads is defined on functions, 
which are callable, whereas templates are not, in general...


Re: Getting the overload set of a template

2018-04-19 Thread Simen Kjærås via Digitalmars-d-learn

On Tuesday, 17 April 2018 at 14:22:27 UTC, Arafel wrote:

Hi!

Is there any way to get the full set of templates that are 
"overloaded" (in my case, based on constraints)?


Currently, there is no way (that I've found, at least) to do 
this. If you have a workaround, that's great, but there really 
should be a way - probably __traits(getOverloads). Having 
__traits(getOverloads) return templates as well should fix some 
of the issues __traits(getOverloads) has, as a bonus.


--
  Simen


Re: Program exited with code 1

2018-04-19 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 19 April 2018 at 11:21:52 UTC, Andrey wrote:

On Thursday, 19 April 2018 at 08:37:19 UTC, Andrey wrote:

What will be a solution?


It seems to me that I found a solution - just replace WinMain() 
with main().


That's fine when you want a console app, but it leaves you with a 
console window automatically popping up if you create a windowed 
app.


When using WinMain, you have to manually setup and tear down 
DRuntime as described in the wiki[1]. Alternatively, you can get 
rid of the console window with a main function via linker flags 
-- when using OPTLINK (the default):


-L/SUBSYSTEM:windows

should be enough. With -m32mscoff or -m64, which uses the MS 
linker, you'll both that and this:


-L/ENTRY:mainCRTStartup

[1] https://wiki.dlang.org/D_for_Win32


Re: making a struct an inputRange with free functions

2018-04-19 Thread jmh530 via Digitalmars-d-learn
On Thursday, 19 April 2018 at 01:33:26 UTC, Jonathan M Davis 
wrote:

[snip]

Occasionally, that aspect of importing and UFCS can be 
annoying, but on the whole, I don't really see why it matters 
much, particularly when actually having the free functions 
available would be an enormous change to how imports work and 
could cause a ton of other problems. It would mean that code 
would be affected by which code imported it rather than just 
the code that it imports, and at that point, you effectively 
lose control over what's going on. It would mean that just like 
C/C++ #includes, you couldn't rely on the module being the same 
every time you imported it. Even if the effect were limited to 
templated code, it would mean that two supposedly identical 
instantiations of a template would not necessarily be identical 
anymore, and they'd have to be recompiled in every module that 
used them. It would be a disaster in the making. mixins are the 
closest that we get to that, but in that case, the programmer 
is specifically stating that they want to reuse that code 
directly in their own module as if it were declared there 
rather than using stuff from other modules. Occasionally, that 
might be limiting, but without those restrictions, you 
basically don't have a module system anymore. And with mixins, 
you have control over what affects your module, whereas having 
the code that imports a module affect it would be more like 
mixing in code from the outside.


And in any case, IMHO, the range API functions aren't really 
functions that make much sense as free functions anyway. 
They're not generic, and they're very much tied to the type 
that they go with - just like opEquals, opAssign, or toString 
are tied to the type. They're inherently pretty much the 
opposite of generic. And even if the import rules somehow let 
you have them as free functions without it causing problems, 
what would it buy you? The only situation I can think of where 
it might be useful is if you're dealing with a type that you 
can't control and thus can't add the member functions to. But 
in that case, you can always just wrap that type in another 
type that does declare the range API. So, I don't think that 
much is lost by not being able to use UFCS to make something a 
range.


- Jonathan M Davis


I get that range functions are very much tied to the type. My 
default is almost always to include them as member functions, and 
I don't favor any big breaking changes.


With respect to this thread, my thinking had gone to that mention 
of anemic domain models on the announce board a few days ago [1]. 
If free functions can't fully replace member functions, then this 
anemic domain model approach would be limited in D.


[1] 
https://forum.dlang.org/thread/kawrnpsyjugwwtknq...@forum.dlang.org


Re: Program exited with code 1

2018-04-19 Thread Andrey via Digitalmars-d-learn

On Thursday, 19 April 2018 at 08:37:19 UTC, Andrey wrote:

What will be a solution?


It seems to me that I found a solution - just replace WinMain() 
with main().


Re: Rotate array in writefln?

2018-04-19 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 19 April 2018 at 10:10:41 UTC, Chris Katko wrote:

That makes sense why transpose wouldn't work for my arrays!

So you're saying if I used [][] (dynamic array) that's a range 
of ranges, and it would work?


Yup. Static arrays can't be ranges, since popFront must mutate 
the length, and the length is a part of the type for static 
arrays.



Why is it you have to rework your templates for static vs 
dynamic ranges? Thanks!


Unless I answered this question above, I'm not entirely sure what 
you're asking.


Anyways, for matrix work (which seems to be what you're doing), I 
would suggest taking a look at 
https://github.com/libmir/mir-algorithm. I haven't actually used 
it myself, but seems to be a very good and comprehensive library 
for this purpose.


--
  Simen


Re: Rotate array in writefln?

2018-04-19 Thread Chris Katko via Digitalmars-d-learn

On Wednesday, 18 April 2018 at 07:15:47 UTC, Simen Kjærås wrote:

On Wednesday, 18 April 2018 at 06:54:29 UTC, Chris Katko wrote:
I need to rotate an array by 90 degrees, or have writefln 
figure that out.


I need, say:

0 4 5 6
0 0 0 0
0 0 0 0
0 0 0 0

But it's outputting:

0 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0

int [4][4] data;
file.writeln(format("%(%-(%d %)\n%)", data));


Generally, the solution would be std.range.transposed. However, 
since you're using a int[4][4], that's not a range-of-ranges, 
and transposed don't work out of the box. This helper function 
should help:


T[][] ror(T, size_t N1, size_t N2)(ref T[N1][N2] arr)
{
T[][] result = new T[][N2];
foreach (i, e; arr) {
result[i] = e.dup;
}
return result;
}

unittest
{
import std.stdio;
import std.range;

int [4][4] data;
data[2][3] = 4;
writefln("%(%-(%d %)\n%)", data);
writefln("%(%-(%d %)\n%)", data.ror.transposed);
}

--
  Simen


That makes sense why transpose wouldn't work for my arrays!

So you're saying if I used [][] (dynamic array) that's a range of 
ranges, and it would work?


Why is it you have to rework your templates for static vs dynamic 
ranges? Thanks!


Re: Program exited with code 1

2018-04-19 Thread Andrey via Digitalmars-d-learn

On Thursday, 19 April 2018 at 08:24:55 UTC, user1234 wrote:
The run-time is not already initialized but the "new" operator 
relies on it.


What will be a solution?


Re: Program exited with code 1

2018-04-19 Thread user1234 via Digitalmars-d-learn

On Thursday, 19 April 2018 at 08:13:00 UTC, Andrey wrote:

Hello,
I wrote a small test code with WinApi:


import core.runtime;
import std.utf;

import core.sys.windows.windows;
import core.sys.windows.wingdi;

class Test
{
public this() nothrow
{

}
}

extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, 
int nCmdShow)

{
new Test(); // error is here
return 0;
}


When I run it, there is an error: Program exited with code 1.

If I comment "new Test();" - no error happens. Why?


The run-time is not already initialized but the "new" operator 
relies on it.


Program exited with code 1

2018-04-19 Thread Andrey via Digitalmars-d-learn

Hello,
I wrote a small test code with WinApi:


import core.runtime;
import std.utf;

import core.sys.windows.windows;
import core.sys.windows.wingdi;

class Test
{
public this() nothrow
{

}
}

extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int 
nCmdShow)

{
new Test(); // error is here
return 0;
}


When I run it, there is an error: Program exited with code 1.

If I comment "new Test();" - no error happens. Why? How to create 
instances of classes?


DMD v2.077.1, Windows 10.


Re: LockingTextWriter not an output range?

2018-04-19 Thread kookman via Digitalmars-d-learn
On Wednesday, 18 April 2018 at 06:40:15 UTC, rikki cattermole 
wrote:

On 18/04/2018 6:28 PM, kookman wrote:
The below static assert fails. Is this expected? Not the way I 
read the docs.


static assert (isOutputRange(typeof(stdout.lockingTextWriter), 
char));


static assert 
(isOutputRange!(typeof(stdout.lockingTextWriter()), char));


Ah - thank you! (slapping my forehead for wasted 2 hours)