| Thread overview | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 19, 2010 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly Attachments:
| 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 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply