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

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

On Friday, 25 January 2019 at 16:14:56 UTC, Kagamin wrote:
also 
http://blogs.microsoft.co.il/pavely/2009/07/23/changing-console-fonts/


That's so much code than next code!

/
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");
}
/
After D2.078.1,it's not work.
why?
thank you.


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

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

On Friday, 25 January 2019 at 15:05:50 UTC, John Chapman wrote:

On Friday, 25 January 2019 at 14:23:15 UTC, FrankLike wrote:
I need to set the font by the code now, because I need to do 
the installer, can't let this installer set the properties on 
each computer?


SetCurrentConsoleFontEx perhaps?

https://docs.microsoft.com/en-us/windows/console/setcurrentconsolefontex


That's so much code than next code.

///
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");
}
///
But After D2.078.1,it's not work.
Why?
Thank you.


Re: How is this code supposed to work?

2019-01-25 Thread Neia Neutuladh via Digitalmars-d-learn
On Fri, 25 Jan 2019 19:14:51 -0500, Steven Schveighoffer wrote:
> Interestingly, It's not possible to do the second form, so it's a bit
> curious why we don't just drop the requirements for nested
> parentheses...

The error messages there are really hideous, too:

template Just(alias s) { alias Just = s; }
alias f = (Just!Just)!int;

scratch.d(16): Error: basic type expected, not (
scratch.d(16): Error: function declaration without return type. (Note that 
constructors are always named this)
scratch.d(16): Error: semicolon expected to close alias declaration
scratch.d(16): Error: found ; when expecting . following int
scratch.d(17): Error: found } when expecting identifier following int.
scratch.d(18): Error: found End of File when expecting ; following 
statement
scratch.d(18): Error: found End of File when expecting } following 
compound statement

So I guess this is just to make people less confused about order of 
operations, since it would be unexpectedly right-associative in a language 
where most things are left-associative?


Re: How is this code supposed to work?

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

On 1/25/19 11:19 AM, Neia Neutuladh wrote:

On Fri, 25 Jan 2019 09:34:47 +, AndreasDavour wrote:

auto point3 = getResponse!Point!int("What's the point? ");


This could be:

 getResponse!(Point!int)

or:

 (getResponse!Point)!int

D requires this to be disambiguated at the parsing stage, before the
compiler works out what getResponse might be.


Interestingly, It's not possible to do the second form, so it's a bit 
curious why we don't just drop the requirements for nested parentheses...


-Steve


Re: code-d and serve-d fail to load after vs update

2019-01-25 Thread WebFreak001 via Digitalmars-d-learn

On Friday, 25 January 2019 at 19:47:59 UTC, greatsam4sure wrote:
Just update vs code and since then I cannot use code-d and 
serve-d.


below is dmd 2.084 complains. I will appreciate any help



Installing into C:\Users\Greatsam\AppData\Roaming\code-d\bin
git clone --recursive https://github.com/Pure-D/serve-d.git 
serve-d

Cloning into 'serve-d'...

dub upgrade
Upgrading project in 
C:\Users\Greatsam\AppData\Roaming\code-d\bin\serve-d

dub build --arch=x86_mscoff
Invalid source/import path: 
C:\Users\Greatsam\AppData\Local\dub\packages\dfmt-0.9.0\dfmt\bin

Running pre-generate commands for dfmt...
Access is denied.
Command failed with exit code 1: rdmd 
C:\Users\Greatsam\AppData\Local\dub\packages\dfmt-0.9.0\dfmt/dubhash.d

Failed to install serve-d (Error code 2)


hi, can you check if you have write permissions to 
C:\Users\Greatsam\AppData\Local\dub\packages\dfmt-0.9.0\dfmt\bin ?


Are you using d.betaStream: true? If you set it to false again 
you will get precompiled binaries (though not the newest 
features, bugs and fixes)


Re: Converting a type to a char

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

On Friday, 25 January 2019 at 15:53:10 UTC, Thomas Gregory wrote:
I would like to check that a char (unknown at compile time) 
matches a particular type but I would like to do so without if 
statements or a hash map as I would like it to be as fast as 
possible.


Ideally I would choose something like this:

enum typeIndex{
byte = 0,
ubyte,
short,
ushort,
int,
uint
}

char[6] typechars="cCsSiI";

bool check(T)(char c){
   return typechars[typeIndex(typeof(T))]==c;
}

Though I know this is implementation is not remotely correct D.


You can do this with AliasSeq and staticIndexOf:

import std.meta;

alias Types = AliasSeq!(byte, ubyte, short, ushort, int, uint);
enum typeIndex(T) = staticIndexOf!(T, Types);
char[6] typechars="cCsSiI";

bool check(T)(char c) {
return typechars[typeIndex!T] == c;
}

Runnable version: https://run.dlang.io/is/Tps38K


Re: Syntax for Pointer to Class

2019-01-25 Thread rikki cattermole via Digitalmars-d-learn

On 26/01/2019 9:52 AM, Q. Schroll wrote:

     C* ptr = &[ new C(...) ][0];


C* ptr = [ new C(...) ].ptr;

Should work.


Re: Syntax for Pointer to Class

2019-01-25 Thread Q. Schroll via Digitalmars-d-learn

On Friday, 25 January 2019 at 20:31:29 UTC, H. S. Teoh wrote:
On Fri, Jan 25, 2019 at 08:12:33PM +, Q. Schroll via 
Digitalmars-d-learn wrote:

Say I have a class C and I want a pointer to a C handle.


Note that taking the address of `C` will actually give you a 
pointer to the reference, not the pointer to the class instance 
itself.


I know. This is precisely the thing I asked for: A pointer to a 
handle. I called it handle for exactly this reason.


For that to be valid, you'll need to store your class reference 
somewhere first, since it's invalid to take the address of an 
rvalue


Yes. ` C(...)` looked to wrong for me to even consider trying.


C c = new C(...);
C* ptrToRef = 


So the answer is no. At least not that simple.

Be warned that the pointer will become invalid when the 
reference `c` goes out of scope, even if the object itself is 
still live (via another reference), because the pointer is 
pointing to the class reference rather than the actual object.


That's kind of obvious if you get remembered, but thanks. I 
wasn't considering this as I wanted the handle to live on the 
heap anyway --- the same way for a struct `S`, the pointer 
pointed to by `S** ptr = new S*(new S(...));` lies on the heap.


I'll use

C* ptr = &[ new C(...) ][0];

for now. It looks ugly, but it does the job.


Re: Syntax for Pointer to Class

2019-01-25 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 25, 2019 at 08:12:33PM +, Q. Schroll via Digitalmars-d-learn 
wrote:
> Say I have a class C and I want a pointer to a C handle.
> 
> I tried the following pieces of syntax:
> 
>   C* obj = new C(); // gives me a C
>   C* obj = new C*(); // gives me a C**
>   C* obj = C*(); // refuses to compile
> 
> Is it even possible? This sounds so newbie...
> I know it's fairly simple with structs: S() gives me a struct
> instance, new S() gives me a ptr to a struct instance.

Classes in D are inherently by-reference, meaning that if you have a
class:

class C { ... }

then the type `C` is implicitly a pointer to the class instance. Of
course, this is hidden by the language syntax so most of the time you
don't have to care that it's a pointer, just that it has reference
semantics.

If you want the raw pointer to the class instance, just cast the
reference to void*, for example:

C c = new C(...);
void* ptr = cast(void*) c;

Note that taking the address of `C` will actually give you a pointer to
the reference, not the pointer to the class instance itself. For that to
be valid, you'll need to store your class reference somewhere first,
since it's invalid to take the address of an rvalue:

C c = new C(...);
C* ptrToRef = 

// And you'll need to explicitly dereference the pointer to get
// to the class instance:
(*ptrToRef).method(...);

Be warned that the pointer will become invalid when the reference `c`
goes out of scope, even if the object itself is still live (via another
reference), because the pointer is pointing to the class reference
rather than the actual object.


T

-- 
Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei 
Alexandrescu


Re: How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn

On Friday, 25 January 2019 at 16:19:38 UTC, Neia Neutuladh wrote:

On Fri, 25 Jan 2019 09:34:47 +, AndreasDavour wrote:

   auto point3 = getResponse!Point!int("What's the point? ");


This could be:

getResponse!(Point!int)

or:

(getResponse!Point)!int

D requires this to be disambiguated at the parsing stage, 
before the compiler works out what getResponse might be.


An example of the latter:

template getResponse(alias n)
{
alias getResponse = n;
}

getResponse!Point is just Point.

I'm not sure why that tutorial has it wrong, but the way to get 
it fixed is to contact the author.


Thanks! I had some issues with the syntax, and getting that part 
correct got me to the conceptual bits, which is where I got lost 
for real. Syntax errors are such a drag, though.


Appreciated.


Re: How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn

On Friday, 25 January 2019 at 14:25:33 UTC, AndreasDavour wrote:

https://run.dlang.io/is/a4oDFZ is an example of how this 
looks. I feel like there's more to these templates than meet 
the eye.


To clarify. I really don't understand the thinking behind these 
templates, and wonder a bit about why a tutorial text like that 
contains examples that doesn't compile. There must be a 
conceptual unclarity somewhere I feel, not just where the 
parenthesis goes.


Finally the penny dropped. So for the poor sods who will find 
this in a web search in the future, I will add to my my monologue 
and show off both my stupidity and maybe some hints on how to 
understand what was going on. I know nothing of C++ and if 
templates are a concept from there, so maybe that was why I did 
not get it.


The chapter from "Programming D" I was referring to in my first 
post did start to talk about function templates, and then moved 
on to step by step show templating of structs, and functions to 
work upon those. What I did not grasp was that these were all 
part of the code. The author made me think, by his choice of 
words or my preconceptions I do not know, that he showed 
incremental additions to the function template and sequentially 
the struct template, when he was in fact showing new templates 
all together. So, if I included all the templates, specializing 
on Point, and then string, and only T, and so on, it worked as 
intended.


What I did not grasp with the code of this template:

Point!T getResponse(T : Point!T)(string question) {
writefln("%s (Point!%s): ", question, T.stringof);

auto x = getResponse!T(" x");
auto y = getResponse!T(" y");

return Point!T(x, y);
}

was that the "inner" call to getResponse() is not a private 
method within this scope, and thus dispatching on the same type 
(which would make it recursive), but if you had that other 
templates specializing on other types it would call the correct 
function. I had the templating system down as a one pass 
search-and-replace, but it's clearly more dynamic than that. So I 
was correct there was a conceptual un-clarity present. With me.


This brings up an interesting point. How do you write a text that 
teaches these things? If you break up the code in small blocks 
where you explain the concepts step by step, and show variants 
and how you can expand the abilities of the code by using more 
and more complex features, how do you do that so all the code is 
self contained? Maybe it can not always be done, and should you 
then have the final code in a block at the end, or do you note 
clearly in the text that *this* block is an example, but *this 
new* code block is complete and will work? It would be verbose if 
you in every instance would quote boilerplate like the import 
std.* and so on. I'm not sure I know how to best do it. Teaching 
is hard.


Syntax for Pointer to Class

2019-01-25 Thread Q. Schroll via Digitalmars-d-learn

Say I have a class C and I want a pointer to a C handle.

I tried the following pieces of syntax:

  C* obj = new C(); // gives me a C
  C* obj = new C*(); // gives me a C**
  C* obj = C*(); // refuses to compile

Is it even possible? This sounds so newbie...
I know it's fairly simple with structs: S() gives me a struct 
instance, new S() gives me a ptr to a struct instance.


Re: Alternative to Interfaces

2019-01-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 January 2019 at 19:34:02 UTC, Sebastien Alaiwan 
wrote:

On Saturday, 19 January 2019 at 09:24:21 UTC, Kagamin wrote:
On Friday, 18 January 2019 at 18:48:46 UTC, Jonathan M Davis 
wrote:

Yes, but some D features will use the GC


They would like to allocate, but they don't know nor care 
where it's allocated from, if the developer uses custom memory 
management, he will know how it's allocated and will be able 
to manage it.


Is it possible to get the raw context pointer from a given 
delegate?
If not, how to deallocate the context without garbage 
collection?


You can access the context pointer of a delegate using its `.ptr` 
property, but changing how it's allocated would require changes 
to druntime.


Some alternatives are discussed in this thread: 
https://forum.dlang.org/thread/mgbdrhcudlhsadnwz...@forum.dlang.org


code-d and serve-d fail to load after vs update

2019-01-25 Thread greatsam4sure via Digitalmars-d-learn
Just update vs code and since then I cannot use code-d and 
serve-d.


below is dmd 2.084 complains. I will appreciate any help



Installing into C:\Users\Greatsam\AppData\Roaming\code-d\bin
git clone --recursive https://github.com/Pure-D/serve-d.git 
serve-d

Cloning into 'serve-d'...

dub upgrade
Upgrading project in 
C:\Users\Greatsam\AppData\Roaming\code-d\bin\serve-d

dub build --arch=x86_mscoff
Invalid source/import path: 
C:\Users\Greatsam\AppData\Local\dub\packages\dfmt-0.9.0\dfmt\bin

Running pre-generate commands for dfmt...
Access is denied.
Command failed with exit code 1: rdmd 
C:\Users\Greatsam\AppData\Local\dub\packages\dfmt-0.9.0\dfmt/dubhash.d

Failed to install serve-d (Error code 2)



Re: Alternative to Interfaces

2019-01-25 Thread Sebastien Alaiwan via Digitalmars-d-learn

On Saturday, 19 January 2019 at 09:24:21 UTC, Kagamin wrote:
On Friday, 18 January 2019 at 18:48:46 UTC, Jonathan M Davis 
wrote:

Yes, but some D features will use the GC


They would like to allocate, but they don't know nor care where 
it's allocated from, if the developer uses custom memory 
management, he will know how it's allocated and will be able to 
manage it.


Is it possible to get the raw context pointer from a given 
delegate?

If not, how to deallocate the context without garbage collection?



Re: How is this code supposed to work?

2019-01-25 Thread Neia Neutuladh via Digitalmars-d-learn
On Fri, 25 Jan 2019 09:34:47 +, AndreasDavour wrote:
>auto point3 = getResponse!Point!int("What's the point? ");

This could be:

getResponse!(Point!int)

or:

(getResponse!Point)!int

D requires this to be disambiguated at the parsing stage, before the 
compiler works out what getResponse might be.

An example of the latter:

template getResponse(alias n)
{
alias getResponse = n;
}

getResponse!Point is just Point.

I'm not sure why that tutorial has it wrong, but the way to get it fixed 
is to contact the author.


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

2019-01-25 Thread Kagamin via Digitalmars-d-learn
also 
http://blogs.microsoft.co.il/pavely/2009/07/23/changing-console-fonts/


Converting a type to a char

2019-01-25 Thread Thomas Gregory via Digitalmars-d-learn
I would like to check that a char (unknown at compile time) 
matches a particular type but I would like to do so without if 
statements or a hash map as I would like it to be as fast as 
possible.


Ideally I would choose something like this:

enum typeIndex{
byte = 0,
ubyte,
short,
ushort,
int,
uint
}

char[6] typechars="cCsSiI";

bool check(T)(char c){
   return typechars[typeIndex(typeof(T))]==c;
}

Though I know this is implementation is not remotely correct D.


Re: GC options

2019-01-25 Thread Alex via Digitalmars-d-learn

On Friday, 25 January 2019 at 13:39:53 UTC, Radu wrote:

On Friday, 25 January 2019 at 13:33:25 UTC, Alex wrote:

I'm experimenting with GC, and reading
https://dlang.org/spec/garbage.html#gc_config

There is an option setting possible via
´´´
extern(C) __gshared string[] rt_options = [ "gcopt=gc:precise" 
];

//gc:conservative|precise|manual
´´´
conservative and manual seem to work, while setting gc to 
precise, yields the error:


"No GC was initialized, please recheck the name of the 
selected GC ('precise')."


Is the docu out of date, or do I something wrong?
And another question: is there some extended docu, what do the 
options lead to?


The precise collector was only recently added 
https://github.com/dlang/druntime/pull/2418


Ah, yes. Thanks.


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

2019-01-25 Thread John Chapman via Digitalmars-d-learn

On Friday, 25 January 2019 at 14:23:15 UTC, FrankLike wrote:
I need to set the font by the code now, because I need to do 
the installer, can't let this installer set the properties on 
each computer?


SetCurrentConsoleFontEx perhaps?

https://docs.microsoft.com/en-us/windows/console/setcurrentconsolefontex



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

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

On 1/25/19 3:20 AM, FeepingCreature wrote:

On Thursday, 24 January 2019 at 17:49:34 UTC, Ali Çehreli wrote:

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


Aren't the semantics of .clear that it's invalid to access references to 
.data after calling .clear, period? And if not, then shouldn't they be? 
Consider if Appender managed its own memory and held on to 
previously-allocated arrays while you were appending, only to free them 
on .clear. That seems like something Appender should be allowed to do. 
If you don't want it, just reinitialize Appender instead of calling .clear.


You are advised not to. But it's not unsafe, as the memory is still there.



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.


Any reason that the semantics of .clear should be different for a 
starting array? Anyway if so, I'd prefer to just make that a runtime error.


Yes, because Appender doesn't know where that memory comes from. A 
string could be, for instance, passed to another thread and being used, 
you wouldn't want to overwrite it.


Generally speaking, overwriting immutable data is UB in D anyway.

Making it a runtime error would be possible, but there has to be a good 
reason to make it that way.




I'm mostly fishing around if anyone has an objection to a PR to change 
this.


Without good reasons to change, I don't see why it would be accepted.

Maybe you can describe your use case?

-Steve


Re: How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn

On Friday, 25 January 2019 at 12:11:51 UTC, AndreasDavour wrote:

On Friday, 25 January 2019 at 12:09:34 UTC, AndreasDavour wrote:
On Friday, 25 January 2019 at 09:36:24 UTC, rikki cattermole 
wrote:

On 25/01/2019 10:34 PM, AndreasDavour wrote:

 [...]


auto point3 = getResponse!(Point!int)("What's the point? ");


[...]


I thought about that option, if it was unclear to the parser 
that Point!int was the type to instantiate upon. Changing to 
the other syntax made me wonder what would happen when 
getResponse is calling itself, which in self was something I 
am a bit unsure of how to understand. Because we get the same 
syntax issue there and it does not seem to work.


https://run.dlang.io/is/a4oDFZ is an example of how this looks. 
I feel like there's more to these templates than meet the eye.


To clarify. I really don't understand the thinking behind these 
templates, and wonder a bit about why a tutorial text like that 
contains examples that doesn't compile. There must be a 
conceptual unclarity somewhere I feel, not just where the 
parenthesis goes.


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

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

On Friday, 25 January 2019 at 08:41:23 UTC, Kagamin wrote:
Create a shortcut to cmd.exe and edit its properties. The 
console window itself has a system menu for this too.


I known that.
I need to set the font by the code now, because I need to do the 
installer, can't let this installer set the properties on each 
computer?


Thank you.


Re: GC options

2019-01-25 Thread Radu via Digitalmars-d-learn

On Friday, 25 January 2019 at 13:33:25 UTC, Alex wrote:

I'm experimenting with GC, and reading
https://dlang.org/spec/garbage.html#gc_config

There is an option setting possible via
´´´
extern(C) __gshared string[] rt_options = [ "gcopt=gc:precise" 
];

//gc:conservative|precise|manual
´´´
conservative and manual seem to work, while setting gc to 
precise, yields the error:


"No GC was initialized, please recheck the name of the selected 
GC ('precise')."


Is the docu out of date, or do I something wrong?
And another question: is there some extended docu, what do the 
options lead to?


The precise collector was only recently added 
https://github.com/dlang/druntime/pull/2418


GC options

2019-01-25 Thread Alex via Digitalmars-d-learn

I'm experimenting with GC, and reading
https://dlang.org/spec/garbage.html#gc_config

There is an option setting possible via
´´´
extern(C) __gshared string[] rt_options = [ "gcopt=gc:precise" ];
//gc:conservative|precise|manual
´´´
conservative and manual seem to work, while setting gc to 
precise, yields the error:


"No GC was initialized, please recheck the name of the selected 
GC ('precise')."


Is the docu out of date, or do I something wrong?
And another question: is there some extended docu, what do the 
options lead to?


Re: How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn

On Friday, 25 January 2019 at 12:09:34 UTC, AndreasDavour wrote:
On Friday, 25 January 2019 at 09:36:24 UTC, rikki cattermole 
wrote:

On 25/01/2019 10:34 PM, AndreasDavour wrote:

 [...]


auto point3 = getResponse!(Point!int)("What's the point? ");


[...]


I thought about that option, if it was unclear to the parser 
that Point!int was the type to instantiate upon. Changing to 
the other syntax made me wonder what would happen when 
getResponse is calling itself, which in self was something I am 
a bit unsure of how to understand. Because we get the same 
syntax issue there and it does not seem to work.


https://run.dlang.io/is/a4oDFZ is an example of how this looks. I 
feel like there's more to these templates than meet the eye.


Re: How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn
On Friday, 25 January 2019 at 09:36:24 UTC, rikki cattermole 
wrote:

On 25/01/2019 10:34 PM, AndreasDavour wrote:

How am I supposed to use that??

   auto point3 = getResponse!Point!int("What's the point? ");


auto point3 = getResponse!(Point!int)("What's the point? ");


   auto point4 = getResponse!Point!int("What's the point? ");
   writeln("Distance: ", point3.distanceTo(point4));

generates the error:

struct_templates.d(48): Error: multiple ! arguments are not 
allowed
struct_templates.d(49): Error: multiple ! arguments are not 
allowed


Which makes me wonder about the syntax.


I thought about that option, if it was unclear to the parser that 
Point!int was the type to instantiate upon. Changing to the other 
syntax made me wonder what would happen when getResponse is 
calling itself, which in self was something I am a bit unsure of 
how to understand. Because we get the same syntax issue there and 
it does not seem to work.


Re: How is this code supposed to work?

2019-01-25 Thread rikki cattermole via Digitalmars-d-learn

On 25/01/2019 10:34 PM, AndreasDavour wrote:

How am I supposed to use that??

   auto point3 = getResponse!Point!int("What's the point? ");


auto point3 = getResponse!(Point!int)("What's the point? ");


   auto point4 = getResponse!Point!int("What's the point? ");
   writeln("Distance: ", point3.distanceTo(point4));

generates the error:

struct_templates.d(48): Error: multiple ! arguments are not allowed
struct_templates.d(49): Error: multiple ! arguments are not allowed

Which makes me wonder about the syntax.




How is this code supposed to work?

2019-01-25 Thread AndreasDavour via Digitalmars-d-learn

I'm reading the "Programming D" here:
http://ddili.org/ders/d.en/templates.html
and am a bit confused by the section on templates.

with a struct template like this:
struct Point(T) {
  T x;
  T y;

  T distanceTo(Point that) const {
immutable real xDistance = x - that.x;
immutable real yDistance = y - that.y;

immutable distance = sqrt((xDistance * xDistance) +
  (yDistance * yDistance));
return cast(T) distance;
  }
}

and a function template like this:
Point!T getResponse(T : Point!T)(string question) {
writefln("%s (Point!%s): ", question, T.stringof);

auto x = getResponse!T(" x");
auto y = getResponse!T(" y");

return Point!T(x, y);
}

How am I supposed to use that??

  auto point3 = getResponse!Point!int("What's the point? ");
  auto point4 = getResponse!Point!int("What's the point? ");
  writeln("Distance: ", point3.distanceTo(point4));

generates the error:

struct_templates.d(48): Error: multiple ! arguments are not 
allowed
struct_templates.d(49): Error: multiple ! arguments are not 
allowed


Which makes me wonder about the syntax.



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

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

On Thursday, 24 January 2019 at 17:49:34 UTC, Ali Çehreli wrote:

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


Aren't the semantics of .clear that it's invalid to access 
references to .data after calling .clear, period? And if not, 
then shouldn't they be? Consider if Appender managed its own 
memory and held on to previously-allocated arrays while you were 
appending, only to free them on .clear. That seems like something 
Appender should be allowed to do. If you don't want it, just 
reinitialize Appender instead of calling .clear.


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.


Any reason that the semantics of .clear should be different for a 
starting array? Anyway if so, I'd prefer to just make that a 
runtime error.


I'm mostly fishing around if anyone has an objection to a PR to 
change this.


Re: Am I missing with ref in this code?

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

Do I need in my code `ref`s ? https://run.dlang.io/is/8dtkC7
Or if vibed doing copy under the hood `ref`s useless?