Re: Non-ugly ways to implement a 'static' class or namespace?

2023-01-20 Thread torhu via Digitalmars-d-learn
On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear 
wrote:

Hi,

In Java/C# you can create purely static classes.

These are classes whose methods are all static, the classes 
cannot be derived from or instantiated:


```
static class Algo {
void drawLine(Canvas c, Pos from, Pos to) { .. };
}
```

Class in use:

```
Algo.drawLine(new Canvas(), new Pos(5, 3), new Pos(7, 9));
```


But why not have drawLine just be a free function?

```
import bluepandastuff.algo;

auto canvas = new Canvas();

drawLine(canvas, Pos(5, 3), Pos(7, 9));

// Or, using UFCS:
canvas.drawLine(Pos(5, 3), Pos(7, 9));

```

I turned Pos into a struct, seems like a typical value type to me.


Re: Is there such a JSON parser?

2023-01-02 Thread torhu via Digitalmars-d-learn
On Monday, 2 January 2023 at 21:36:10 UTC, Steven Schveighoffer 
wrote:

On 1/1/23 6:28 PM, torhu wrote:
I need to parse some JSON data into various data structures, 
so I'm looking for a parser based on events or ranges. One 
that doesn't just load the file and build a data structure 
that represents the whole thing. So not std.json, at least.


It's pretty rough-edged, but 
https://code.dlang.org/packages/jsoniopipe will do this. It has 
mechanisms to jump to specific object members, and to rewind to 
a cached point. It does not use any intermediate representation.


-Steve


Thank, I can check it out.


Re: Handling CheckBox state changes in DLangUI

2023-01-02 Thread torhu via Digitalmars-d-learn
On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson 
wrote:


The compiler errors I get are, for no '&' and with '&':

Error: function `app.checkbox_b_clicked(Widget source, bool 
checked)` is not callable using argument types `()`


Error: none of the overloads of `opAssign` are callable using 
argument types `(bool function(Widget source, bool checked))`


If checkbox_b_clicked is a non-static nested function or 
non-static method, taking the address of it should result in a 
delegate, not a function pointer.


You can check what it is like this:

writeln(typeid(_b_clicked));


Re: Is there such a JSON parser?

2023-01-02 Thread torhu via Digitalmars-d-learn

On Monday, 2 January 2023 at 14:56:27 UTC, SealabJaster wrote:

Are you asking for a SAX-styled parser for JSON?


Yes, I actually want to replace a SAX parser.


Re: Is there such a JSON parser?

2023-01-02 Thread torhu via Digitalmars-d-learn

On Monday, 2 January 2023 at 05:44:33 UTC, thebluepandabear wrote:

You might want to try the following:

https://github.com/libmir/asdf


I had a look at that, but I think it just loads the whole file 
into it's own data structure. And then you can use attributes to 
get it to fill structs with data, but that's too basic for my 
needs.


Is there such a JSON parser?

2023-01-01 Thread torhu via Digitalmars-d-learn
I need to parse some JSON data into various data structures, so 
I'm looking for a parser based on events or ranges. One that 
doesn't just load the file and build a data structure that 
represents the whole thing. So not std.json, at least.


Re: How to use version in dub?

2022-12-13 Thread torhu via Digitalmars-d-learn

On Tuesday, 13 December 2022 at 19:50:15 UTC, torhu wrote:

On Tuesday, 13 December 2022 at 19:28:44 UTC, Leonardo A wrote:

Hello. How to use version in dub?

https://dlang.org/spec/version.html
"The version level and version identifier can be set on the 
command line by the -version"


I tried everything but noting.


In SDL syntax, either at the top level, in a configuration, or 
build type:


```
versions "something" "other"
```


To be more clear: When using dub you need to put this in the dub 
file, dub.sdl or dub.json. If you want to be able to choose from 
the command line, use a configuration:


```
configuration "something" {
versions "something"
}
```

Then you can do:
```
dub build -c=something
```


Re: How to use version in dub?

2022-12-13 Thread torhu via Digitalmars-d-learn

On Tuesday, 13 December 2022 at 19:28:44 UTC, Leonardo A wrote:

Hello. How to use version in dub?

https://dlang.org/spec/version.html
"The version level and version identifier can be set on the 
command line by the -version"


I tried everything but noting.


In SDL syntax, either at the top level, in a configuration, or 
build type:


```
versions "something" "other"
```



Re: aa.keys, synchronized and shared

2022-11-14 Thread torhu via Digitalmars-d-learn

On Monday, 14 November 2022 at 07:57:16 UTC, Kagamin wrote:

This works for me:
```
shared SyncAA!(string,string) saa;
void f()
{
saa=new shared SyncAA!(string,string)("1","2");
saa.keys();
saa["12"]="34";
saa.remove("12");
}
```


The strange error message I got was because I initialized the 
variable at module level, that doesn't work when you have a 
constructor. It worked when I moved it into a module constructor.


Re: aa.keys, synchronized and shared

2022-11-11 Thread torhu via Digitalmars-d-learn

On Friday, 11 November 2022 at 14:19:31 UTC, Kagamin wrote:

Try this:
```



private:
V[K] sharedTable;
ref inout(V[K]) unsharedTable() inout
{
return *cast(inout(V[K])*)
}
```


Thanks, that worked! Feels like programming in C, though. If I 
could figure out how to initialize the AA explicitly, I could 
also remove the ref here. If I just remove the ref, the AA is 
always null. If I try to initialize it in the constructor, I get 
this:


src\syncaa.d(11,5): Error: `_d_monitorenter` cannot be 
interpreted at compile time, because it has no available source 
code


No idea why, it seems to happen if I try to use the AA in the 
constructor at all. Even when I just do `data_.remove(K.init);`


I also tried DMD 2.101.0-rc.1, using the new `new V[K]` syntax, 
same error there.


Re: aa.keys, synchronized and shared

2022-11-10 Thread torhu via Digitalmars-d-learn

On Thursday, 10 November 2022 at 21:55:26 UTC, torhu wrote:

I'm trying to make a more thread-safe wrapper for AA's:

```
synchronized final class SyncAA(K, V) ///


I chose to fix this by just using `synchronized (this)` inside 
each method instead, for now. Still interested in cleaner 
solutions, but I guess synchronized/shared is a bit of a rabbit 
hole...


aa.keys, synchronized and shared

2022-11-10 Thread torhu via Digitalmars-d-learn

I'm trying to make a more thread-safe wrapper for AA's:

```
synchronized final class SyncAA(K, V) ///
{
///
V opIndex(K key) { return data_[key]; }

///
V opIndexAssign(V value, K key) { return data_[key] = value; }

///
K[] keys() const { return data_.keys; }

///
void remove(K key) { data_.remove(key); }

/// There is no `in` operator, it would not be thread-safe.
V get(K key, lazy V defaultValue=V.init)
{
auto p = key in data_;
return p ? *p : defaultValue;
}

///
int opApply(scope int delegate(inout ref V) dg) const
{
int result = 0;
foreach (value; data_) {
result = dg(value);
if (result)
break;
}
return result;
}

///
int opApply(scope int delegate(K, inout ref V) dg) const
{
int result = 0;
foreach (key, value; data_) {
result = dg(key, value);
if (result)
break;
}
return result;
}

private:
V[K] data_;
}
```

In another file:
`__gshared serverListCache = new SyncAA!(string, ServerList);`

I'm using `keys` in a regular function, this is the error i get:

C:\prog\dmd\windows\bin\..\..\src\druntime\import\object.d(3245,36): Error: 
cannot implicitly convert expression `aa` of type 
`shared(const(ServerList[string]))` to `const(shared(ServerList)[string])`
src\syncaa.d(17,36): Error: template instance 
`object.keys!(shared(const(ServerList[string])), 
shared(const(ServerList)), string)` error instantiating
src\serveractions.d(45,33):instantiated from here: 
`SyncAA!(string, ServerList)`
src\serveractions.d(50,17): Error: `shared` `const` method 
`syncaa.SyncAA!(string, ServerList).SyncAA.keys` is not callable 
using a non-shared mutable object

Error C:\prog\dmd\windows\bin\dmd.exe failed with exit code 1.




Re: Doubt about char.min/max == typeid(char)

2022-10-06 Thread torhu via Digitalmars-d-learn

On Friday, 7 October 2022 at 00:13:59 UTC, matheus wrote:

Hi,

Could anyone please tell me why the properties of min/max of a 
char returns a "char type" and not a value as an int?


Well, why whould the highest and lowest values of a type be of a 
different type..?



```d
import std;

void func(char x) { writeln("It's a char"); }
void func(int x)  { writeln("It's an int"); }
void func(double x)  { writeln("It's a double"); }

void main(){
func(char.min);
}
```



Re: Replacing tango.text.Ascii.isearch

2022-10-06 Thread torhu via Digitalmars-d-learn

On Thursday, 6 October 2022 at 21:36:48 UTC, rassoc wrote:

And what kind of testing was that? Mind to share? Because I did 
the following real quick and wasn't able to measure a "two 
orders of magnitude" difference. Sure, the regex version came 
on top, but they were both faster than the ruby baseline I 
cooked up.


Originally I just loaded a one megabyte file and searched the 
whole thing. I changed it to split it into (40 000) lines 
instead, regex is about ten times faster then. I compile with 
-release -O -inline. Here is the second version:


```d
import std;
import std.datetime.stopwatch;

enum FILE = "test.lst";
string text;
string needle;

void test(bool delegate(string haystack) dg)
{

auto sw = StopWatch(AutoStart.yes);
int counter = 0;
foreach (line; lineSplitter(text)) {
if (dg(line))
counter++;
}
sw.stop();
writefln("%s", sw.peek());
writefln("counter: %s", counter);
}

void main(char[][] args)
{
enforce(args.length > 1, "Need a needle argument.");

text = cast(string)read(FILE);
needle = args[1].idup;
auto re = regex(to!string(escaper(needle)), "i");
string needleLower = needle.toLower();

test((h) => !!h.matchFirst(re));
test((h) => h.asLowerCase().canFind(needleLower));
}
```



Re: Replacing tango.text.Ascii.isearch

2022-10-05 Thread torhu via Digitalmars-d-learn
On Wednesday, 5 October 2022 at 17:29:25 UTC, Steven 
Schveighoffer wrote:



```d
bool isearch(S1, S2)(S1 haystack, S2 needle)
{
import std.uni;
import std.algorithm;
return haystack.asLowerCase.canFind(needle.asLowerCase);
}
```

untested.

-Steve


I did some basic testing, and regex was two orders of magnitude 
faster. So now I know, I guess.


Re: Replacing tango.text.Ascii.isearch

2022-10-05 Thread torhu via Digitalmars-d-learn

On Wednesday, 5 October 2022 at 20:45:55 UTC, torhu wrote:

On Wednesday, 5 October 2022 at 20:40:46 UTC, torhu wrote:



Am I doing something wrong here?


Right, you can instantiate structs without arguments. It's been 
ten years since I last used D, I was thinking of structs like 
if they were classes.


I think there should be sensible default here, seems like an easy 
trap to remove.


Re: Replacing tango.text.Ascii.isearch

2022-10-05 Thread torhu via Digitalmars-d-learn

On Wednesday, 5 October 2022 at 20:40:46 UTC, torhu wrote:



Am I doing something wrong here?


Right, you can instantiate structs without arguments. It's been 
ten years since I last used D, I was thinking of structs like if 
they were classes.


Re: Replacing tango.text.Ascii.isearch

2022-10-05 Thread torhu via Digitalmars-d-learn
On Wednesday, 5 October 2022 at 17:29:25 UTC, Steven 
Schveighoffer wrote:

[...]


I wanted to do some quick benchmarking to figure out what works.

When I run this:

```d
import std.stdio;
import std.datetime.stopwatch;

void main()
{
auto sw = StopWatch();  
sw.stop();
writeln(sw.peek().toString());
}
```

It prints this:
2 weeks, 6 days, 9 hours, 34 minutes, 43 secs, 214 ms, 946 ╬╝s, 
and 7 hnsecs


Am I doing something wrong here?


Replacing tango.text.Ascii.isearch

2022-10-05 Thread torhu via Digitalmars-d-learn
I need a case-insensitive check to see if a string contains 
another string for a "quick filter" feature. It should 
preferrably be perceived as instant by the user, and needs to 
check a few thousand strings in typical cases. Is a regex the 
best option, or what would you suggest?


Re: Template function alias that leaves out the last type parameter

2022-09-28 Thread torhu via Digitalmars-d-learn

On Wednesday, 28 September 2022 at 12:43:52 UTC, rassoc wrote:

On 9/28/22 02:04, torhu via Digitalmars-d-learn wrote:

Thank you, works like a charm!


It does? How are you formatting `info("foo", 'a', 42)` inside 
the template if I may ask?


It works like writefln, so that example would not compile. I 
forgot to make the format string explicit, I probably should have 
done that.


```
private template _messageBox(string title, int style)
{
void _messageBox(T...)(T args)
{
messageBox(format(args), title, style);
}
}
```



Re: Template function alias that leaves out the last type parameter

2022-09-27 Thread torhu via Digitalmars-d-learn

On Tuesday, 27 September 2022 at 23:18:06 UTC, Adam D Ruppe wrote:

On Tuesday, 27 September 2022 at 22:39:52 UTC, torhu wrote:
How would I achieve something like this, do I have to turn the 
aliases into functions?


You can write it out long-form with two steps like this:



Thank you, works like a charm!


Re: Template function alias that leaves out the last type parameter

2022-09-27 Thread torhu via Digitalmars-d-learn

On Tuesday, 27 September 2022 at 22:39:52 UTC, torhu wrote:
How would I achieve something like this, do I have to turn the 
aliases into functions?


```d
void _messageBox(string title, int style, T...)(T args)
{
string s = format(args);
/* etc... */
}

alias _messageBox!(APPNAME, SWT.ICON_INFORMATION) info;
alias _messageBox!("Warning", SWT.ICON_WARNING) warning;
alias _messageBox!("Error", SWT.ICON_ERROR) error;
```


I should mention that I am really looking for a Phobos 2 way of 
achieving what I currently do, which is this:

```d
void _messageBox(string title, int style)(...)
{
char[] msg;
void f(dchar c) { encode(msg, c); }
doFormat(, _arguments, _argptr);
messageBox(cast(string)msg, title, style);
}
```


Template function alias that leaves out the last type parameter

2022-09-27 Thread torhu via Digitalmars-d-learn
How would I achieve something like this, do I have to turn the 
aliases into functions?


```d
void _messageBox(string title, int style, T...)(T args)
{
string s = format(args);
/* etc... */
}

alias _messageBox!(APPNAME, SWT.ICON_INFORMATION) info;
alias _messageBox!("Warning", SWT.ICON_WARNING) warning;
alias _messageBox!("Error", SWT.ICON_ERROR) error;
```