Re: Ascii string literal.

2016-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 7 May 2016 at 01:37:30 UTC, Jonathan M Davis wrote:
In general, it's better to use representation than to cast, 
because representation gets the constness right, whereas if you 
cast, there's always the risk that you won't.



Yeah, if it is a general thing, but here it is a simple function 
statically typed to string...


Re: Ascii string literal.

2016-05-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Fri, 06 May 2016 21:57:22 +
"Adam D. Ruppe via Digitalmars-d-learn"
 wrote:

> On Friday, 6 May 2016 at 21:39:35 UTC, Anonymouse wrote:
> > Is this different from what std.string.representation does?
>
> No, it does the same thing, but with your own function the
> intention may be clearer (or you could do a template to avoid any
> function or custom types too)

In general, it's better to use representation than to cast, because
representation gets the constness right, whereas if you cast, there's always
the risk that you won't.

- Jonathan M Davis


Re: Ascii string literal.

2016-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 6 May 2016 at 21:39:35 UTC, Anonymouse wrote:

Is this different from what std.string.representation does?


No, it does the same thing, but with your own function the 
intention may be clearer (or you could do a template to avoid any 
function or custom types too)


Re: Ascii string literal.

2016-05-06 Thread Anonymouse via Digitalmars-d-learn

On Friday, 6 May 2016 at 20:29:35 UTC, Adam D. Ruppe wrote:

On Friday, 6 May 2016 at 20:01:27 UTC, Alexandru Ermicioi wrote:

Is it possible somehow to convert implicitly a string literal


Not implicitly (well, unless you just use string, ascii is a 
strict subset of utf-8 anyway), but you could do it explicitly 
easily.


immutable(ubyte)[] ascii(string s) { return 
cast(typeof(return)) s; }


Is this different from what std.string.representation does?


Re: Ascii string literal.

2016-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 6 May 2016 at 20:01:27 UTC, Alexandru Ermicioi wrote:

Is it possible somehow to convert implicitly a string literal


Not implicitly (well, unless you just use string, ascii is a 
strict subset of utf-8 anyway), but you could do it explicitly 
easily.


immutable(ubyte)[] ascii(string s) { return cast(typeof(return)) 
s; }



Then use it like

ascii("your string")

or make it a template and use ascii!"your string" or "your 
string".ascii whatever.


You could (and imo should!) also make a struct to hold the new 
type.