Thread overview
Filling a char array with letters and element type of char[]
Mar 03, 2015
Kadir Erdem Demir
Mar 03, 2015
Tobias Pankrath
Mar 03, 2015
Jonathan M Davis
Mar 03, 2015
Jonathan M Davis
Mar 03, 2015
Ali Çehreli
Mar 05, 2015
Kadir Erdem Demir
Mar 03, 2015
Ali Çehreli
March 03, 2015
I have an char[];

char[] strArr = "http://www.hurriyet.com.tr/ekonomi".dup;

I stripped the domain out of url like:

auto domain = findSplitAfter(strArr, "http://")[1].until('/');

Than because I am new to the language I became curious if I change domain(which I believe a input iterator); the values of strArr will also change or not. I tried to to modify domain like :

fill(domain, 'a');

Which gives error :
"cannot deduce function from argument types !()(Until!("a == b", char[], char), char)"

But if the array is a array of ints it compiles. I believe it fails because in compile messages I see

std.algorithm.fill(Range, Value)(Range range, Value filler) if (isInputRange!Range && is(typeof(range.front = filler)))

and

writeln( is(typeof(url.front = 'a')) );  ---> false
writeln( is(typeof(intArr.front = 0)) ); ---> true(that is why compiles)

I have three questions?

If I change the iterator which I get from algorithm, the owner data will change or not?

How to use std.algorithm.fill with char types?

What is the type of char array holds why it does not matches char?

Regards
Kadir Erdem
March 03, 2015
> I have three questions?
>
> If I change the iterator which I get from algorithm, the owner data will change or not?
>
> How to use std.algorithm.fill with char types?
>
> What is the type of char array holds why it does not matches char?
>
> Regards
> Kadir Erdem

I have no time to dig into this, but:

is(typeof(arr.front = 'a')) does _not_ check if arr.front is of type char. It is true if you can assign an 'a' to arr.front.

is(typeof(_expr_)) is another way to write __traits(compiles, _expr).

So, either the range returned by until has elements that are not assignable or the reason is that until returns a range of dchar, because all string types are treated as ranges of dchar.

March 03, 2015
On Tuesday, March 03, 2015 09:06:03 Tobias Pankrath via Digitalmars-d-learn wrote:
>
> > I have three questions?
> >
> > If I change the iterator which I get from algorithm, the owner data will change or not?
> >
> > How to use std.algorithm.fill with char types?
> >
> > What is the type of char array holds why it does not matches char?
> >
> > Regards
> > Kadir Erdem
>
> I have no time to dig into this, but:
>
> is(typeof(arr.front = 'a')) does _not_ check if arr.front is of
> type char. It is true if you can assign an 'a' to arr.front.
>
> is(typeof(_expr_)) is another way to write __traits(compiles,
> _expr).
>
> So, either the range returned by until has elements that are not assignable or the reason is that until returns a range of dchar, because all string types are treated as ranges of dchar.

http://stackoverflow.com/questions/6401365

- Jonathan M Davis

March 03, 2015
On 3/3/15 4:06 AM, Tobias Pankrath wrote:
>
>> I have three questions?
>>
>> If I change the iterator which I get from algorithm, the owner data
>> will change or not?

I'm not sure about this question.

>>
>> How to use std.algorithm.fill with char types?

You cannot currently.

>>
>> What is the type of char array holds why it does not matches char?

Because D is schizophrenic ;) Phobos considers char[] arrays not to be arrays of char, only ranges of dchar. Unless you talk to the compiler, where it happily treats char[] as an array.

> I have no time to dig into this, but:
>
> is(typeof(arr.front = 'a')) does _not_ check if arr.front is of type
> char. It is true if you can assign an 'a' to arr.front.

That is the check in std.algorithm. It wants to fill the array by assigning 'a' to the front. In D land, char[] arrays are ranges of dchars, and are only READABLE. This means a char[] array can never be an output range. Boatloads of code specializes char[] arrays in order to work around this.

I guess we need another specialization (because this should be allowed, IMO).

-Steve
March 03, 2015
On 03/03/2015 12:18 AM, Kadir Erdem Demir wrote:

> I have an char[];
>
> char[] strArr = "http://www.hurriyet.com.tr/ekonomi".dup;
>
> I stripped the domain out of url like:
>
> auto domain = findSplitAfter(strArr, "http://")[1].until('/');
>
> Than because I am new to the language I became curious if I change
> domain(which I believe a input iterator); the values of strArr will also
> change or not.

.front of a char range is a dchar that is created on the fly (i.e. it is an rvalue), effectively making such ranges "generators". If possible, setting the returned dchar would not change the original char.

> But if the array is a array of ints it compiles.

Elements of some ranges (e.g. slices) are lvalues. When needed, hasLvalueElements can detect the kind of range at compile time:

  http://dlang.org/phobos/std_range.html#hasLvalueElements

Ali

March 03, 2015
On Tuesday, March 03, 2015 08:50:35 Steven Schveighoffer via Digitalmars-d-learn wrote:
> >> What is the type of char array holds why it does not matches char?
>
> Because D is schizophrenic ;) Phobos considers char[] arrays not to be arrays of char, only ranges of dchar. Unless you talk to the compiler, where it happily treats char[] as an array.

I think that at this point, most of the core developers think that auto-decoding was a mistake, but fixing it would definitely be a bit of a mess, and whenever it comes up, Andrei complains that we need to focus on other things and that we're blowing it out of proportion. :|

I think that the auto-decoding was well-intentioned and an interesting experiment, but ultimately, it's causing too many problems...

- Jonathan M Davis

March 03, 2015
On 3/3/15 2:32 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Tuesday, March 03, 2015 08:50:35 Steven Schveighoffer via Digitalmars-d-learn wrote:
>>>> What is the type of char array holds why it does not matches char?
>>
>> Because D is schizophrenic ;) Phobos considers char[] arrays not to be
>> arrays of char, only ranges of dchar. Unless you talk to the compiler,
>> where it happily treats char[] as an array.
>
> I think that at this point, most of the core developers think that
> auto-decoding was a mistake, but fixing it would definitely be a bit of a
> mess, and whenever it comes up, Andrei complains that we need to focus on
> other things and that we're blowing it out of proportion. :|
>
> I think that the auto-decoding was well-intentioned and an interesting
> experiment, but ultimately, it's causing too many problems...

Sure, but I think that for phobos to say you can't fill a large char[] with a repeat of small char[], but you can fill a large int[] with a repeat of small int[], is more of a problem than somehow fixing the underlying situation.

Other algorithms treat char[] specially. fill should too.

-Steve

March 03, 2015
On 03/03/2015 11:38 AM, Steven Schveighoffer wrote:

> Sure, but I think that for phobos to say you can't fill a large char[]
> with a repeat of small char[], but you can fill a large int[] with a
> repeat of small int[], is more of a problem than somehow fixing the
> underlying situation.
>
> Other algorithms treat char[] specially. fill should too.

std.string.representation works in this case.

import std.stdio;
import std.algorithm;
import std.string;

void main()
{
    char[] strArr = "http://www.hurriyet.com.tr/ekonomi".dup;
    auto domain = findSplitAfter(strArr.representation,
                                 "http://")[1].until('/');
    fill(domain, 'a');
    writeln(strArr);
}

Prints

http://aaaaaaaaaaaaaaaaaaa/ekonomi

Ali

P.S. Grrr. Why can't I ever remember the name of that function! (representation, representation, representation, ...)

March 05, 2015
@Tobias @Ali @Jonathan @Steven

Thanks alot for your answers.
I will try to learn as much as I can from them.

Regards
Erdem