How/why can toStringz() and toUTFz() be used as properties?

2013-04-26 Thread Trey Brisbane

Hey all,

Can someone please explain to me how and why it is that 
toStringz() and toUTFz() can be used in the following way?


string a = "123";
auto b = a.toStringz;
auto c = a.toUTFz;

Also, how is it that they can even be called as if they were 
class methods? That is:


string a = "123";
auto b = a.toStringz(); // <-- Note I've added parentheses
auto c = a.toUTFz();

As opposed to being limited to:

string a = "123";
auto b = toStringz(a);
auto c = toUTFz(a);

I need to understand this, as I wish to write a function 
toWinStr() that can be used in the same way which will accept any 
string type, and, based on something like version(Unicode) and 
const-ness, output a WinAPI LPSTR, LPCSTR, LPWSTR or LPCWSTR as 
appropriate.

Is such a thing possible?


Re: How/why can toStringz() and toUTFz() be used as properties?

2013-04-26 Thread anonymous

On Friday, 26 April 2013 at 07:31:57 UTC, Trey Brisbane wrote:

Hey all,

Can someone please explain to me how and why it is that 
toStringz() and toUTFz() can be used in the following way?


string a = "123";
auto b = a.toStringz;
auto c = a.toUTFz;

Also, how is it that they can even be called as if they were 
class methods? That is:


string a = "123";
auto b = a.toStringz(); // <-- Note I've added parentheses
auto c = a.toUTFz();

As opposed to being limited to:

string a = "123";
auto b = toStringz(a);
auto c = toUTFz(a);


There's two things at work here:

a) Universal Function Call Syntax (UFCS). That basically means 
that you can call a free function via method/property-like dot 
syntax. The first argument to the function goes before the dot. 
UFCS should be explained in the online language documentation, 
but if it's in there I can't find it.

So, with UFCS you get from  toStringz(a)  to  a.toStringz() .

b) Empty parentheses are optional. -- Unless you compile with the 
-property flag. There has been some discussion about the future 
of the feature. Optional empty parentheses and/or the -property 
flag may go away in the future.

That gets you from  a.toString()  to  a.toString .

I need to understand this, as I wish to write a function 
toWinStr() that can be used in the same way which will accept 
any string type, and, based on something like version(Unicode) 
and const-ness, output a WinAPI LPSTR, LPCSTR, LPWSTR or 
LPCWSTR as appropriate.

Is such a thing possible?


You just write your function so that  toWinStr(stuff)  works. 
UFCS and optional empty parentheses then enable  stuff.toWinStr .


Re: How/why can toStringz() and toUTFz() be used as properties?

2013-04-26 Thread Trey Brisbane

On Friday, 26 April 2013 at 08:49:10 UTC, anonymous wrote:

On Friday, 26 April 2013 at 07:31:57 UTC, Trey Brisbane wrote:

Hey all,

Can someone please explain to me how and why it is that 
toStringz() and toUTFz() can be used in the following way?


string a = "123";
auto b = a.toStringz;
auto c = a.toUTFz;

Also, how is it that they can even be called as if they were 
class methods? That is:


string a = "123";
auto b = a.toStringz(); // <-- Note I've added parentheses
auto c = a.toUTFz();

As opposed to being limited to:

string a = "123";
auto b = toStringz(a);
auto c = toUTFz(a);


There's two things at work here:

a) Universal Function Call Syntax (UFCS). That basically means 
that you can call a free function via method/property-like dot 
syntax. The first argument to the function goes before the dot. 
UFCS should be explained in the online language documentation, 
but if it's in there I can't find it.

So, with UFCS you get from  toStringz(a)  to  a.toStringz() .

b) Empty parentheses are optional. -- Unless you compile with 
the -property flag. There has been some discussion about the 
future of the feature. Optional empty parentheses and/or the 
-property flag may go away in the future.

That gets you from  a.toString()  to  a.toString .

I need to understand this, as I wish to write a function 
toWinStr() that can be used in the same way which will accept 
any string type, and, based on something like version(Unicode) 
and const-ness, output a WinAPI LPSTR, LPCSTR, LPWSTR or 
LPCWSTR as appropriate.

Is such a thing possible?


You just write your function so that  toWinStr(stuff)  works. 
UFCS and optional empty parentheses then enable  stuff.toWinStr 
.


Ahhh ok, thanks for explaining :)


Re: How/why can toStringz() and toUTFz() be used as properties?

2013-04-26 Thread ollie
On Fri, 26 Apr 2013 09:31:54 +0200, Trey Brisbane wrote:

> I need to understand this, as I wish to write a function toWinStr() that
> can be used in the same way which will accept any string type, and,
> based on something like version(Unicode) and const-ness, output a WinAPI
> LPSTR, LPCSTR, LPWSTR or LPCWSTR as appropriate.
> Is such a thing possible?

You might want to look at std.windows.charset. You'll find the following 
functions:

const(char)* toMBSz(in char[] s, uint codePage = 0)
string fromMBSz(immutable(char)* s, int codePage = 0)

These might work for you or you may be able to expand on them.


Re: How/why can toStringz() and toUTFz() be used as properties?

2013-04-26 Thread Ali Çehreli

On 04/26/2013 01:49 AM, anonymous wrote:

> On Friday, 26 April 2013 at 07:31:57 UTC, Trey Brisbane wrote:

> UFCS should be explained in the online language documentation,
> but if it's in there I can't find it.

Me neither. I wonder where in the language syntax a "function call" lives...

UFCS does not require more than what you have already said but it looks 
like I had managed to fill up more space than a couple of paragraphs: :)


  http://ddili.org/ders/d.en/ufcs.html

Ali