Re: Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn

On Thursday, 24 January 2019 at 22:02:36 UTC, bauss wrote:

On Thursday, 24 January 2019 at 21:25:45 UTC, Paul Backus wrote:

So, I'm not sure what the best solution here is.


The best solution is just to pass a copy since there's no 
absolute need for a reference to be passed.


But if I will pass copy how I can create array of structures with 
correct response status?


Create new structures?


Re: What is the alternative to the setlocale function of c in D? Thank you.

2019-01-24 Thread FrankLike via Digitalmars-d-learn

On Thursday, 24 January 2019 at 12:19:44 UTC, Kagamin wrote:

Try workarounds here:
https://issues.dlang.org/show_bug.cgi?id=1448
https://issues.dlang.org/show_bug.cgi?id=2742


How do I set the font? Please.


Re: Am I missing with ref in this code?

2019-01-24 Thread bauss via Digitalmars-d-learn

On Thursday, 24 January 2019 at 21:25:45 UTC, Paul Backus wrote:

So, I'm not sure what the best solution here is.


The best solution is just to pass a copy since there's no 
absolute need for a reference to be passed.




Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 24 January 2019 at 16:04:06 UTC, Suliman wrote:
 Error: static assert:  "Cannot convert arguments '(MyUrl)' to 
function arguments '(MyUrl*)'."


You've forgotten to change the call site to pass a pointer.

However, it turns out that even if you do that, vibe will not 
allow you to pass a pointer in this situation:


.dub/packages/vibe-core-1.5.0/vibe-core/source/vibe/core/taskpool.d(114,21): Error: 
static assert:  "Argument type MyUrl* is not safe to pass between threads."

So, I'm not sure what the best solution here is.


Re: Is there something special required to use Appender.clear

2019-01-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/24/19 7:35 AM, FeepingCreature wrote:

On Tuesday, 27 March 2018 at 12:31:05 UTC, Simen Kjærås wrote:

On Tuesday, 27 March 2018 at 12:17:58 UTC, Ellie Harper wrote:
Sorry if this is a stupid question, but is there something special 
required to call Appender.clear?  When I attempt even just a simple 
use I am getting compile errors relating to `template object.clear`.


From the documentation for Appender:


Note
clear is disabled for immutable or const element types, due to the 
possibility that Appender might overwrite immutable data.


Since string is immutable(char)[], clear() is simply not available for 
appender!string.




Isn't this wrong, though? Appender controls the memory it references. It 
could just choose to allocate non-immutable memory internally. As long 
as any const data put into the appender is *returned* as const, there is 
no chance of immutable memory being overwritten.


Appender is sometimes given a starting array. clear isn't callable in 
that case, and we don't distinguish the difference in the type or at 
runtime.


I did a lot of work on std.array.Appender when I redid the array runtime 
to fix array appending back in 2010 to make it safe. e.g.: 
https://github.com/schveiguy/phobos/commit/7e89201cdae96e71e550afe72b59ef3db145916f


Disabling clear from appender for non-mutable element types was one of 
the changes: 
https://github.com/schveiguy/phobos/commit/6636569318d26545bf7c36ddac029830a6d52531


This is before we were on github, so I don't know if there's any 
discussion of the issues, perhaps on the mailing list, but looking at my 
emails around that time, there are some emails that aren't in the 
mailing list.


A funny thing I wrote to Walter/Sean around that time, when I was just 
getting druntime commit access:


"I don't mind helping out in druntime, but I'm not really any kind of 
expert on the runtime, and I doubt I'll make many contributions besides 
this one without assignment."


;)

FYI, this thread is almost a year old.

-Steve


Re: Is there something special required to use Appender.clear

2019-01-24 Thread Ali Çehreli via Digitalmars-d-learn

On 01/24/2019 04:35 AM, FeepingCreature wrote:
> On Tuesday, 27 March 2018 at 12:31:05 UTC, Simen Kjærås wrote:
>> On Tuesday, 27 March 2018 at 12:17:58 UTC, Ellie Harper wrote:
>>> Sorry if this is a stupid question, but is there something special
>>> required to call Appender.clear?  When I attempt even just a simple
>>> use I am getting compile errors relating to `template object.clear`.
>>
>> From the documentation for Appender:
>>
>>> Note
>>> clear is disabled for immutable or const element types, due to the
>>> possibility that Appender might overwrite immutable data.
>>
>> Since string is immutable(char)[], clear() is simply not available for
>> appender!string.
>>
>> --
>>   Simen
>
> Isn't this wrong, though? Appender controls the memory it references. It
> could just choose to allocate non-immutable memory internally. As long
> as any const data put into the appender is *returned* as const, there is
> no chance of immutable memory being overwritten.

I think Appender is trying to protect previous data from Appender's 
later use. If it handed out immutable data, the user is expecting it to 
not change. So, Appender cannot clear it for later use.


Ali



Re: Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn
It's because runWorkerTask internally passes its arguments 
along to the function by value:


https://github.com/vibe-d/vibe.d/blob/master/core/vibe/core/core.d#L364

The workaround is to pass a pointer instead:

void getServiceStatus(MyUrl* url) {
// ...
}

// ...

runWorkerTask(&getServiceStatus, &url);



void getServiceStatus(MyUrl* url)
{
...
}


 Error: static assert:  "Cannot convert arguments '(MyUrl)' to 
function arguments '(MyUrl*)'."


Re: What is the alternative to the setlocale function of c in D? Thank you.

2019-01-24 Thread FrankLike via Digitalmars-d-learn

On Thursday, 24 January 2019 at 12:19:44 UTC, Kagamin wrote:

Try workarounds here:
https://issues.dlang.org/show_bug.cgi?id=1448
https://issues.dlang.org/show_bug.cgi?id=2742


Ok,thank you.

import std.stdio;
import core.sys.windows.windows;
import std.process:executeShell;
extern(Windows) bool SetConsoleOutputCP(uint);

void main()
{
SetConsoleOutputCP(65001);
writeln("字符");
executeShell("pause");
}



Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 24 January 2019 at 15:28:19 UTC, Suliman wrote:
I am doing very small link-checker. Here is' code 
https://run.dlang.io/is/p8whrA


I am expecting that on line:
writefln("url: %s, status: %s", url.url, url.status);

I will print link and it's status. But I am getting only:
url: http://127.0.0.1:8081/hck, status:
url: http://127.0.0.1:8081/hck2, status:
url: http://127.0.0.1:8081/hck3, status:

It's seems that I missed something with refs? Could you help me 
find error?


It's because runWorkerTask internally passes its arguments along 
to the function by value:


https://github.com/vibe-d/vibe.d/blob/master/core/vibe/core/core.d#L364

The workaround is to pass a pointer instead:

void getServiceStatus(MyUrl* url) {
// ...
}

// ...

runWorkerTask(&getServiceStatus, &url);


Am I missing with ref in this code?

2019-01-24 Thread Suliman via Digitalmars-d-learn
I am doing very small link-checker. Here is' code 
https://run.dlang.io/is/p8whrA


I am expecting that on line:
writefln("url: %s, status: %s", url.url, url.status);

I will print link and it's status. But I am getting only:
url: http://127.0.0.1:8081/hck, status:
url: http://127.0.0.1:8081/hck2, status:
url: http://127.0.0.1:8081/hck3, status:

It's seems that I missed something with refs? Could you help me 
find error?




Re: How to disable/hide constructor when using factory method?

2019-01-24 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 24 January 2019 at 12:58:15 UTC, JN wrote:



Doh. Of course. I feel so dumb. I just had it at @disable 
this();, then replaced @disable with private without thinking 
to add {}


Give me a nickel for every time I've made an edit like that...!


Re: How to disable/hide constructor when using factory method?

2019-01-24 Thread JN via Digitalmars-d-learn

On Thursday, 24 January 2019 at 12:52:47 UTC, Arafel wrote:
You are declaring the constructor, but not defining it, i.e. 
you're telling the compiler that it's in some other compilation 
unit.


The compiler won't complain, but the linker will.

If you replace:


[...]


with:


[...]


it should work.

A.

On 1/24/19 1:48 PM, JN wrote:

[...]


Doh. Of course. I feel so dumb. I just had it at @disable 
this();, then replaced @disable with private without thinking to 
add {}


Re: How to disable/hide constructor when using factory method?

2019-01-24 Thread Arafel via Digitalmars-d-learn
You are declaring the constructor, but not defining it, i.e. you're 
telling the compiler that it's in some other compilation unit.


The compiler won't complain, but the linker will.

If you replace:


private this();


with:


private this() {}


it should work.

A.

On 1/24/19 1:48 PM, JN wrote:


I expected that too, but it doesn't even work in the same module.

class Foo
{
     private this();

     static Foo makeFoo()
     {
     Foo f = new Foo();
     return f;
     }
}

void main() {
}

fails with:

onlineapp.o:onlineapp.d:_D9onlineapp3Foo7__ClassZ: error: undefined 
reference to '_D9onlineapp3Foo6__ctorMFZCQzQr'
onlineapp.d:7: error: undefined reference to 
'_D9onlineapp3Foo6__ctorMFZCQzQr'

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

I don't understand why is this a linker problem. My understanding is 
that for some reason static methods don't have access to the private 
constructor (they're not considered same module?). But even though, it 
should error with something like "Foo.makeFoo() cannot access private 
Foo.this()" rather than fail at linking.




Re: How to disable/hide constructor when using factory method?

2019-01-24 Thread JN via Digitalmars-d-learn

On Wednesday, 23 January 2019 at 19:41:44 UTC, Alex wrote:

On Wednesday, 23 January 2019 at 19:26:37 UTC, JN wrote:

class Foo
{
static Foo makeFoo()
{
Foo f = new Foo();
return f;
}
}

void main() {
Foo f = Foo.makeFoo();
}

For a code like this. I'd like all users of the class to be 
forced to create instances using the static method makeFoo. I 
want to disallow "new Foo()". But I don't know if it's 
possible to disable the constructor, while still making it 
available in the makeFoo static method.


@disable this doesn't work, private this(); doesn't work 
either.


private should work, if the class and the main are in different 
modules, no?


I expected that too, but it doesn't even work in the same module.

class Foo
{
private this();

static Foo makeFoo()
{
Foo f = new Foo();
return f;
}
}

void main() {
}

fails with:

onlineapp.o:onlineapp.d:_D9onlineapp3Foo7__ClassZ: error: 
undefined reference to '_D9onlineapp3Foo6__ctorMFZCQzQr'
onlineapp.d:7: error: undefined reference to 
'_D9onlineapp3Foo6__ctorMFZCQzQr'

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

I don't understand why is this a linker problem. My understanding 
is that for some reason static methods don't have access to the 
private constructor (they're not considered same module?). But 
even though, it should error with something like "Foo.makeFoo() 
cannot access private Foo.this()" rather than fail at linking.


Re: Is there something special required to use Appender.clear

2019-01-24 Thread FeepingCreature via Digitalmars-d-learn

On Tuesday, 27 March 2018 at 12:31:05 UTC, Simen Kjærås wrote:

On Tuesday, 27 March 2018 at 12:17:58 UTC, Ellie Harper wrote:
Sorry if this is a stupid question, but is there something 
special required to call Appender.clear?  When I attempt even 
just a simple use I am getting compile errors relating to 
`template object.clear`.


From the documentation for Appender:


Note
clear is disabled for immutable or const element types, due to 
the possibility that Appender might overwrite immutable data.


Since string is immutable(char)[], clear() is simply not 
available for appender!string.


--
  Simen


Isn't this wrong, though? Appender controls the memory it 
references. It could just choose to allocate non-immutable memory 
internally. As long as any const data put into the appender is 
*returned* as const, there is no chance of immutable memory being 
overwritten.


Re: What is the alternative to the setlocale function of c in D? Thank you.

2019-01-24 Thread Kagamin via Digitalmars-d-learn

Try workarounds here:
https://issues.dlang.org/show_bug.cgi?id=1448
https://issues.dlang.org/show_bug.cgi?id=2742


Re: opEquals() non-standard return type

2019-01-24 Thread Jacob Shtokolov via Digitalmars-d-learn

On Wednesday, 23 January 2019 at 17:28:37 UTC, H. S. Teoh wrote:
The best way to do this is to use a string DSL or a delegate as 
template argument. For example:


auto result = User.filter!q{ User.name == "John" };

or:

auto result = User.filter!(u => u.name == "John");



I didn't know about q{} token strings! This looks very cool for 
implementing different DSLs.


Thank you!


Re: opEquals() non-standard return type

2019-01-24 Thread Jacob Shtokolov via Digitalmars-d-learn

On Thursday, 24 January 2019 at 00:47:37 UTC, Ali Çehreli wrote:
Yeah, that can't work. Remove the bool-returning one and your 
code works with the 'alias this' above.


Wow, this is an amazing workaround! I didn't think about it in 
that way.

It perfectly solves the issue.

Thank you!


Re: What is the alternative to the setlocale function of c in D? Thank you.

2019-01-24 Thread FrankLike via Digitalmars-d-learn

On Thursday, 24 January 2019 at 07:48:44 UTC, FrankLike wrote:

Hi,everyone,

for example:

import std.stdio;
import std.process:executeShell;

extern(C) int setlocale(int,char*);

static this()
{
import core.stdc.wchar_;
import core.stdc.stdio;
fwide(core.stdc.stdio.stdout,1);
setlocale(0,cast(char*)"china");
}

void main()
{
writeln("字符");
executeShell("pause");
}
///code end/

In D2.078.1 It can display Chinese characters correctly!
But After D2.080 , but it can't be used now.
Who can help me?
Thank you.