Re: Template with default parameter

2022-03-10 Thread bauss via Digitalmars-d-learn

On Friday, 11 March 2022 at 04:41:40 UTC, Andrey Zherikov wrote:

I have simple template:
```d
template T(int i=3)
{
mixin template M(int m)
{
enum t = i;
}
}

{
mixin T!1.M!1;
pragma(msg, t);   // 1
}
{
mixin T!().M!1;
pragma(msg, t);   // 3
}
{
	mixin T.M!1;  // Error: identifier `M` of `T.M` is not 
defined

  // Error: mixin `M!1` is not defined
pragma(msg, t);   // Error: undefined identifier `t`
  //while evaluating 
`pragma(msg, t)`
}
```

What should I do to be able to write `T.M!...`? I want to omit 
verbose `!()` for `T`. Note that mixins are essential here.


Create an alias for T!() is the best you can do.

Ex.

```
alias t = T!();
```

There isn't really any better method as far as I know.


Re: gdc or ldc for faster programs?

2022-03-10 Thread Chris Piker via Digitalmars-d-learn

On Tuesday, 25 January 2022 at 20:04:04 UTC, Adam D Ruppe wrote:
Not surprising at all: gdc is excellent and underrated in the 
community.


The performance metrics are just a bonus.  Gdc is the main reason 
I can get my worksite to take D seriously since we're a 
traditional unix shop (solaris -> linux).  The gcd crew are doing 
a *huge* service for the community.





Template with default parameter

2022-03-10 Thread Andrey Zherikov via Digitalmars-d-learn

I have simple template:
```d
template T(int i=3)
{
mixin template M(int m)
{
enum t = i;
}
}

{
mixin T!1.M!1;
pragma(msg, t);   // 1
}
{
mixin T!().M!1;
pragma(msg, t);   // 3
}
{
	mixin T.M!1;  // Error: identifier `M` of `T.M` is not 
defined

  // Error: mixin `M!1` is not defined
pragma(msg, t);   // Error: undefined identifier `t`
  //while evaluating 
`pragma(msg, t)`
}
```

What should I do to be able to write `T.M!...`? I want to omit 
verbose `!()` for `T`. Note that mixins are essential here.


Re: Write UTF-8 bytes directly to stack buffer

2022-03-10 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Mar 10, 2022 at 05:39:34PM +, Chris Piker via Digitalmars-d-learn 
wrote:
> Hi D
> 
> There are quite a few string, array and range functions in phobos so
> I'm getting confused as to the right way to encode string data as
> UTF-8 directly into a stack buffer while keeping track of the write
> point.
> 
> I have some output packets I'm building up in a tight loop.  For speed
> I'm using the a priori knowledge that output packets will never be
> larger then 64K.   So what's the best way to do this:
> 
> ```d
> ubyte[65536] buf;
> ubyte[] usable_buf = buf;
> 
>   // part of some tight loop, how to create function writef_utf8 ?
>   foreach(input_thing; things){
>  usable_buf.writef_utf8!"format str"(input_thing.fieldA, 
> input_thing.fieldB);
>   }
> 
> size_t used = buf.length - usable_buf.length;
> stdout.write(buf[0.. used]);
> 
> ```

Probably what you're looking for is std.format.formattedWrite. For
example:

--
import std;
void main() {
ubyte[65536] buf;
char[] usable_buf = cast(char[]) buf[];
usable_buf.formattedWrite!"Blah %d blah %s"(123, "Это UTF-8 строка.");
auto used = buf.length - usable_buf.length;
writefln("%(%02X %)", buf[0 .. used]);
}
--

D strings are UTF-8 by default, so for the most part, you don't need to
worry about it.


T

-- 
Guns don't kill people. Bullets do.


Write UTF-8 bytes directly to stack buffer

2022-03-10 Thread Chris Piker via Digitalmars-d-learn

Hi D

There are quite a few string, array and range functions in phobos 
so I'm getting confused as to the right way to encode string data 
as UTF-8 directly into a stack buffer while keeping track of the 
write point.


I have some output packets I'm building up in a tight loop.  For 
speed I'm using the a priori knowledge that output packets will 
never be larger then 64K.   So what's the best way to do this:


```d
ubyte[65536] buf;
ubyte[] usable_buf = buf;

  // part of some tight loop, how to create function writef_utf8 ?
  foreach(input_thing; things){
 usable_buf.writef_utf8!"format str"(input_thing.fieldA, 
input_thing.fieldB);

  }

size_t used = buf.length - usable_buf.length;
stdout.write(buf[0.. used]);

```