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.


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: 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.


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: Reversing a string

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

On Friday, 11 January 2019 at 11:15:05 UTC, Mike James wrote:

On Friday, 11 January 2019 at 09:41:30 UTC, bauss wrote:

On Friday, 11 January 2019 at 08:25:41 UTC, Seb wrote:
On Friday, 11 January 2019 at 08:05:39 UTC, AndreasDavour 
wrote:

Hi.

I've just started to learn some D, so maybe this question is 
extremely stupid, but please bear with me.


[...]


Use .retro - it is also lazy and won't allocate:

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


What a terrible name.


Check out the origin :-)

https://forum.dlang.org/thread/hl8345$2b1q$1...@digitalmars.com?page=1


-=mike=-



Look at that.

Incidentally, I kind of like "foreach(i; 99 .. 0 : -1)".


Re: Reversing a string

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

On Friday, 11 January 2019 at 09:41:30 UTC, bauss wrote:

On Friday, 11 January 2019 at 08:25:41 UTC, Seb wrote:
On Friday, 11 January 2019 at 08:05:39 UTC, AndreasDavour 
wrote:

Hi.

I've just started to learn some D, so maybe this question is 
extremely stupid, but please bear with me.


[...]


Use .retro - it is also lazy and won't allocate:

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


What a terrible name.


Well, it was not the first one I would have searched for, no.

Good to know it exists as well, though. Thanks!


Re: Reversing a string

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

On Friday, 11 January 2019 at 08:45:12 UTC, JN wrote:
On Friday, 11 January 2019 at 08:15:01 UTC, rikki cattermole 
wrote:
Note the immutable, it means you cannot modify individual 
values. Which is a problem for reverse because it modifies in 
place.




The error message is kind of unfortunate. This is a simple 
usecase and the error message is undecipherable already. It'd 
be cool if the compiler could try to strip immutability, and if 
the type matches then, throw an error something like "Cannot 
pass immutable char[] to reverse, did you mean char[]?".


That would help a lot, as I got "rikki cattermole"'s answer at 
once, when my eyes were brought to the "immutable" part.


I come from the lisp world, so I'm kind of familiar with the idea 
of copying and/or modifying in place to limit consing. I guess I 
should I have realised this would be a perfect example of that 
kind of situation.


Reversing a string

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

Hi.

I've just started to learn some D, so maybe this question is 
extremely stupid, but please bear with me.


I have been trying to reverse a string, and through some googling 
found the std.algorithm.mutation.reverse method. But, when I try 
to use it I get some very verbose errors that just makes my eyes 
glaze over. What on earth do they mean? How do I reverse a 
string, really?


This is my code:

import std.stdio;
import std.algorithm;

void main() {
  auto test = "This is my text";
  string next_test = reverse(test);

  writeln(test);
  writeln(next_test);
}


This is my error:

[ante@tiny ~/src/D/tutorial]$ rdmd string4.d
/usr/home/ante/src/dmd2/freebsd/bin64/../../src/phobos/std/algorithm/mutation.d(2513):
 Error: template `std.algorithm.mutation.reverse` cannot deduce function from 
argument types `!()(immutable(ubyte)[])`, candidates are:
/usr/home/ante/src/dmd2/freebsd/bin64/../../src/phobos/std/algorithm/mutation.d(2485):
`std.algorithm.mutation.reverse(Range)(Range r) if (isBidirectionalRange!Range && 
(hasSwappableElements!Range || hasAssignableElements!Range && hasLength!Range && 
isRandomAccessRange!Range || isNarrowString!Range && isAssignable!(ElementType!Range)))`
/usr/home/ante/src/dmd2/freebsd/bin64/../../src/phobos/std/algorithm/mutation.d(2521):
 Error: template `std.algorithm.mutation.reverse` cannot deduce function from 
argument types `!()(immutable(ubyte)[])`, candidates are:
/usr/home/ante/src/dmd2/freebsd/bin64/../../src/phobos/std/algorithm/mutation.d(2485):
`std.algorithm.mutation.reverse(Range)(Range r) if (isBidirectionalRange!Range && 
(hasSwappableElements!Range || hasAssignableElements!Range && hasLength!Range && 
isRandomAccessRange!Range || isNarrowString!Range && isAssignable!(ElementType!Range)))`
string4.d(6): Error: template instance 
`std.algorithm.mutation.reverse!string` error instantiating
Failed: ["/usr/home/ante/src/dmd2/freebsd/bin64/dmd", "-v", 
"-o-", "string4.d", "-I."]

--

Can some unpack that for me, please?

I might add that the most intuitive way to reverse a string, I 
think, would be to consider it a range and just do "newstring = 
oldstring[$ .. 0]" but that gives an error about being out of 
bounds, so I guess the compiler expect values to increase from 
left to right, and does not want to increment beyond $. Makes 
sense, but that syntax would be very neat.