Jump to page: 1 2 3
Thread overview
Re: Higher level built-in strings
Jul 19, 2010
Jesse Phillips
Jul 20, 2010
Sean Kelly
Jul 20, 2010
Jérôme M. Berger
Jul 20, 2010
Walter Bright
Jul 20, 2010
Jérôme M. Berger
Jul 20, 2010
Walter Bright
Jul 21, 2010
Aelxx
Jul 20, 2010
Walter Bright
Jul 20, 2010
Walter Bright
Jul 20, 2010
Rory McGuire
Jul 20, 2010
Rory McGuire
Jul 20, 2010
Rory McGuire
Jul 20, 2010
Simen kjaeraas
Jul 20, 2010
Rory McGuire
Jul 20, 2010
Walter Bright
Jul 20, 2010
Jonathan M Davis
Jul 20, 2010
Fawzi Mohamed
July 19, 2010
What about:

struct String {
	string items;
	alias items this;
}

And add the needed functions you wish to have in string and it will still work in existing functions that operate on immutable(char)[]
July 20, 2010
On 07/19/2010 06:51 PM, Jesse Phillips wrote:
> What about:
>
> struct String {
> 	string items;
> 	alias items this;
> }
>
> And add the needed functions you wish to have in string and it will still work in existing functions that operate on immutable(char)[]

Fortunately you can essentially achieve the above by simply writing free functions that take a string or a ref string as their first argument. Then you can use str.foo(args) as an alternative for foo(str, args).

Andrei
July 20, 2010
On Tue, 20 Jul 2010 01:51:51 +0200, Jesse Phillips <jessekphillips+d@gmail.com> wrote:

> What about:
>
> struct String {
> 	string items;
> 	alias items this;
> }
>
> And add the needed functions you wish to have in string and it will still work in existing functions that operate on immutable(char)[]

You shouldn't need to do that:

string strstr(string haystack, string needle);

can be used as:

string s;
s.strstr("needle");

so you can add "methods" to a string or whatever just by defining functions.

-Rory
July 20, 2010
On Mon, 19 Jul 2010 20:26:47 -0400, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:

> On 07/19/2010 06:51 PM, Jesse Phillips wrote:
>> What about:
>>
>> struct String {
>> 	string items;
>> 	alias items this;
>> }
>>
>> And add the needed functions you wish to have in string and it will still work in existing functions that operate on immutable(char)[]
>
> Fortunately you can essentially achieve the above by simply writing free functions that take a string or a ref string as their first argument. Then you can use str.foo(args) as an alternative for foo(str, args).

How do we make this work?

auto str = "hello world";
foreach(c; str)
   assert(is(typeof(c) == dchar));

-Steve
July 20, 2010
On Tue, 20 Jul 2010 16:08:06 +0200, Jesse Phillips <jesse.k.phillips@gmail.com> wrote:

> But then you can't overload operators.
>
> On Tue, Jul 20, 2010 at 12:54 AM, Rory McGuire <rmcguire@neonova.co.za> wrote:
>> On Tue, 20 Jul 2010 01:51:51 +0200, Jesse Phillips
>> <jessekphillips+d@gmail.com> wrote:
>>
>>> What about:
>>>
>>> struct String {
>>>        string items;
>>>        alias items this;
>>> }
>>>
>>> And add the needed functions you wish to have in string and it will still
>>> work in existing functions that operate on immutable(char)[]
>>
>> You shouldn't need to do that:
>>
>> string strstr(string haystack, string needle);
>>
>> can be used as:
>>
>> string s;
>> s.strstr("needle");
>>
>> so you can add "methods" to a string or whatever just by defining functions.
>>
>> -Rory
>>
>
>
>

such as?
July 20, 2010
Steven Schveighoffer Wrote:
> 
> How do we make this work?
> 
> auto str = "hello world";
> foreach(c; str)
>     assert(is(typeof(c) == dchar));

foreach (dchar c; str)
    assert(...);

This feature has been in D for years.
July 20, 2010
On Tue, 20 Jul 2010 16:51:57 +0200, Rory McGuire <rmcguire@neonova.co.za> wrote:

> On Tue, 20 Jul 2010 16:08:06 +0200, Jesse Phillips <jesse.k.phillips@gmail.com> wrote:
>
>> But then you can't overload operators.
>>
>> On Tue, Jul 20, 2010 at 12:54 AM, Rory McGuire <rmcguire@neonova.co.za> wrote:
>>> On Tue, 20 Jul 2010 01:51:51 +0200, Jesse Phillips
>>> <jessekphillips+d@gmail.com> wrote:
>>>
>>>> What about:
>>>>
>>>> struct String {
>>>>        string items;
>>>>        alias items this;
>>>> }
>>>>
>>>> And add the needed functions you wish to have in string and it will still
>>>> work in existing functions that operate on immutable(char)[]
>>>
>>> You shouldn't need to do that:
>>>
>>> string strstr(string haystack, string needle);
>>>
>>> can be used as:
>>>
>>> string s;
>>> s.strstr("needle");
>>>
>>> so you can add "methods" to a string or whatever just by defining functions.
>>>
>>> -Rory
>>>
>>
>>
>>
>
> such as?

I mean is there not another way to do the same thing?
July 20, 2010
Rory McGuire <rmcguire@neonova.co.za> wrote:

[snip]

Rory, is there something wrong with your newsreader? I keep seeing your
posts as replies only to the top post.

-- 
Simen
July 20, 2010
Sean Kelly wrote:
> Steven Schveighoffer Wrote:
>> How do we make this work?
>>
>> auto str = "hello world";
>> foreach(c; str)
>>     assert(is(typeof(c) == dchar));
> 
> foreach (dchar c; str)
>     assert(...);
> 
> This feature has been in D for years.

	And what about this one:

void func(T) (T range) {
    foreach (elem; range)
        assert (is (typeof (elem) == ElementType!(T)));
}

func ("azerty");
auto a = [ 1, 2, 3, 4, 5];
func (a);

		Jerome
-- 
mailto:jeberger@free.fr
http://jeberger.free.fr
Jabber: jeberger@jabber.fr



July 20, 2010
On Tue, 20 Jul 2010 11:02:57 -0400, Sean Kelly <sean@invisibleduck.org> wrote:

> Steven Schveighoffer Wrote:
>>
>> How do we make this work?
>>
>> auto str = "hello world";
>> foreach(c; str)
>>     assert(is(typeof(c) == dchar));
>
> foreach (dchar c; str)
>     assert(...);
>
> This feature has been in D for years.

The omission of dchar is on purpose.  Phobos has characterized string as a bidirectional range of dchars.  For every range where I do:

foreach(e; range)

e is of the type of the range.  Except for char and wchar.  This schizophrenia of type induction is very bad for D, and it's a good argument of why strings should not simply be arrays.

-Steve
« First   ‹ Prev
1 2 3