Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn

On Wednesday, 12 April 2023 at 13:09:07 UTC, Ali Çehreli wrote:
Not every type is null'able but nullable. ;) So, a design may 
use the following:

https://dlang.org/library/std/typecons/nullable.html


I implemented Handler into the Voldermort build, which Walter 
loved so much.  For convenience, I put an alias between the 
template and the function.  But Nullable didn't work, instead 
it's returning T.init...


```d
template Handler(alias A)
{
  alias T = typeof(A);

  auto Handler()
  {
struct Impl
{
  T*[string] data;

  void set(string key, ref T value)
  {
data[key] = 
  }

  auto opIndex(string key)
  {
if (auto ret = key in data)
{
  return **ret;
}
return T.init;/*
import std.typecons : Nullable;
return Nullable!T.init;//*/
  }

  auto opSlice()
  {
T[] result;
foreach (ref value; data.values)
  result ~= *value;
return result;
  }
}
return Impl();
  }
}

import std.stdio;

void main()
{
  struct List { string product; float price; }

  auto fruits = [ List("Manderin", 3.79),
  List("Orange", 2.99),
  List("Kiwi", 0.59),
  ];

  auto handlers = Handler!fruits;
  handlers.set("fruits", fruits);
  // please try it:  ^--v
  foreach(h; handlers["fruit"])
  {
h.product.write(": ");
h.price.writeln(" €");
  }

  auto handler = Handler!(List());

  import std.conv : text;
  foreach(i, ref fruit; fruits)
  {
handler.set(i.text, fruit);
  }
  handler[].writeln;
} /* Prints:

  Manderin: 3.79 €
  Orange: 2.99 €
  Kiwi: 0.59 €
  [List("Kiwi", 0.59), List("Manderin", 3.79), List("Orange", 
2.99)]


*/
```

SDB@79



Re: How can a function pointer required to be extern(C)?

2023-04-12 Thread John Chapman via Digitalmars-d-learn

On Wednesday, 12 April 2023 at 20:36:59 UTC, H. S. Teoh wrote:

---snip---
extern(C) void* abc(void*) {return null;}

alias FuncPtr = typeof();


You can also express it like this:

```d
extern(C) alias FuncPtr = void* function(void*);
```


Re: How can a function pointer required to be extern(C)?

2023-04-12 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Apr 12, 2023 at 08:23:51PM +, rempas via Digitalmars-d-learn wrote:
> Sorry if the title doesn't make any sense, let me explain. So, I do have the
> following code that does not compile:
> 
> ```d
> import core.sys.posix.pthread; /* The library */
> 
> struct Thread {
> private:
>   pthread_t thread_id;
> 
> public:
>   this(void* function(void*) func, void* arg = null, scope
> const(pthread_attr_t*) attr = null) {
> pthread_create(_id, attr, func, arg);
>   }
> 
>   @property:
> pthread_t id() { return this.thread_id; }
> }
> 
> ```
> 
> Yes, I'm trying to "encapsulate" the Pthread (POSIX threads) API.
> Normally, the function pointer that is passed to "pthread_create" must
> be "extern(C)" and this is the complaining that the compile does. So,
> I'm thinking to replace the constructor to this:
> 
> ```d
> this(extern(C) void* function(void*) func, void* arg = null,
>  scope const(pthread_attr_t*) attr = null)
> { pthread_create(_id, attr, func, arg); }
> ```
> 
> I just added "extern(C)" before the type. This is how it looks in the
> error message so it must work right? Well... it doesn't. And here I am
> wondering why. Any ideas?

IMO this is a bug either in D's syntax or in the parser.  I'd file an
enhancement request.

In the meantime, you can use alias as a workaround:


---snip---
extern(C) void* abc(void*) {return null;}

alias FuncPtr = typeof();
pragma(msg, typeof(abc));
pragma(msg, typeof());

//void wrapper(extern(C) void* function(void*) callback) {} // NG
void wrapper(FuncPtr callback) {} // OK

pragma(msg, typeof(wrapper));
---snip---


T

-- 
A programming language should be a toolbox for the programmer to draw
upon, not a minefield of dangerous explosives that you have to very
carefully avoid touching in the wrong way.


How can a function pointer required to be extern(C)?

2023-04-12 Thread rempas via Digitalmars-d-learn
Sorry if the title doesn't make any sense, let me explain. So, I 
do have the following code that does not compile:


```d
import core.sys.posix.pthread; /* The library */

struct Thread {
private:
  pthread_t thread_id;

public:
  this(void* function(void*) func, void* arg = null, scope 
const(pthread_attr_t*) attr = null) {

pthread_create(_id, attr, func, arg);
  }

  @property:
pthread_t id() { return this.thread_id; }
}

```

Yes, I'm trying to "encapsulate" the Pthread (POSIX threads) API. 
Normally, the function pointer that is passed to "pthread_create" 
must be "extern(C)" and this is the complaining that the compile 
does. So, I'm thinking to replace the constructor to this:


```d
this(extern(C) void* function(void*) func, void* arg = null,
 scope const(pthread_attr_t*) attr = null)
{ pthread_create(_id, attr, func, arg); }
```

I just added "extern(C)" before the type. This is how it looks in 
the error message so it must work right? Well... it doesn't. And 
here I am wondering why. Any ideas?


Re: @nogc and Phobos

2023-04-12 Thread rempas via Digitalmars-d-learn

On Tuesday, 14 March 2023 at 20:40:39 UTC, bomat wrote:


Sounds intriguing. Anything I can look at yet? Or still all top 
secret? :)


Oh, amazing! I let you on waiting for almost a month and I 
wouldn't had see it if I didn't came to post another question.


I'm so so sorry for the waiting, I will not even use an excuse. 
So, if you are willing to make an account on 
[Codeberg](https://codeberg.org/), the repository is private but 
after making the account, follow 
[me](https://codeberg.org/rempas) and I will give you access! 
Tho, you may read something cringe! Be prepared, lol!


Re: Assocative array lookup for object

2023-04-12 Thread Ali Çehreli via Digitalmars-d-learn

On 4/12/23 04:35, Salih Dincer wrote:

> I made a little mistake and I'll fix it before someone rub nose in it :)

You asked for it! :)

>auto opIndex(string key) {
>  if(auto ret = key in data)
>  {
>return *ret;
>  }
>  return null;
>}

Not every type is null'able but nullable. ;) So, a design may use the 
following:


  https://dlang.org/library/std/typecons/nullable.html

Ali



Re: Mir-ion:YAML Example

2023-04-12 Thread WebFreak001 via Digitalmars-d-learn

On Wednesday, 12 April 2023 at 12:00:14 UTC, Vino wrote:

Hi All,

  Can some point me where i can find examples on how to use 
mir-ion YAML


From,
Vino.B


documentation is very sparse, but essentially with mir-ion you 
import the different ser/deser packages that you would like to 
use. If you for example want to deserialize YAML to a custom 
struct, you use


```d
import mir.deser.yaml;

struct CustomStruct
{
string foo;
uint bar;
}

void main()
{
string yamlString = `{foo: str, bar: 4}`;
CustomStruct s = yamlString.deserializeYaml!CustomStruct;

assert(s == S("str", 4));
}
```

If you want to deserialize arbitrary values and introspect them 
at runtime, you use `YamlAlgebraic` instead of `CustomStruct`, 
which you can think of like std.json : JSONValue, but utilizes 
mir's algebraic structures, which work more like std.sumtype.


To convert algebraic values to custom structs later, you use 
mir.ion.conv : serde, which basically just internally serializes 
the data and deserializes it again (but a little more efficiently)


more deserialization examples in the unittests: 
https://github.com/libmir/mir-ion/blob/62c476a6a00d0d5ddfb3585bdfbe520d825e872b/source/mir/deser/yaml.d


and for serialization you use `serializeYaml` from 
`mir.ser.yaml`, see 
https://github.com/libmir/mir-ion/blob/62c476a6a00d0d5ddfb3585bdfbe520d825e872b/source/mir/ser/yaml.d


Mir-ion:YAML Example

2023-04-12 Thread Vino via Digitalmars-d-learn

Hi All,

  Can some point me where i can find examples on how to use 
mir-ion YAML


From,
Vino.B


Re: Using DUB packages with Meson

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 11:07:56 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

Did you compile the library with dub using ldc2?


Yup, I do not have other compilers installed.

--
Dmitry Olshansky


Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn

On Wednesday, 12 April 2023 at 04:57:58 UTC, Salih Dincer wrote:

I think you want to do an encapsulation like below.

```d
  auto opIndex(string key)
    => *(key in data);
```


I made a little mistake and I'll fix it before someone rub nose 
in it :)


```d
  auto opIndex(string key) {
if(auto ret = key in data)
{
  return *ret;
}
return null;
  }

  assert(handler["D Lang"] == );
  assert(handler["null"] is null);
```

SDB@79




Re: Using DUB packages with Meson

2023-04-12 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

Did you compile the library with dub using ldc2?


Re: Using DUB packages with Meson

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 10:24:48 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
I'm going to guess that you need to use the version specifier 
in the package name. Because I'm not seeing anything there to 
handle it specifically.


https://github.com/mesonbuild/meson/blob/master/mesonbuild/dependencies/dub.py

i.e. ``dub build [[@]] []``

So use ``package:sub@1.0.2``.

Also I just noticed meson doesn't support shared libraries from 
dub, so something to keep in mind.


I'm getting closer. I'm stuck with this at the moment:

Found DUB: /home/d.olshanskiy/bin/dub (DUB version 1.31.1, built 
on Mar 12 2023)

ERROR: strand found but it wasn't compiled with ldc
Run-time dependency strand found: NO

src/meson.build:22:0: ERROR: Dependency "strand" not found

--
Dmitry Olshansky


Re: Using DUB packages with Meson

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 10:24:48 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
I'm going to guess that you need to use the version specifier 
in the package name. Because I'm not seeing anything there to 
handle it specifically.


https://github.com/mesonbuild/meson/blob/master/mesonbuild/dependencies/dub.py

i.e. ``dub build [[@]] []``

So use ``package:sub@1.0.2``.

Also I just noticed meson doesn't support shared libraries from 
dub, so something to keep in mind.


Oh, Rikki, you are so helpful. Thanks!

--
Dmitry Olshansky


Re: Using DUB packages with Meson

2023-04-12 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
I'm going to guess that you need to use the version specifier in the 
package name. Because I'm not seeing anything there to handle it 
specifically.


https://github.com/mesonbuild/meson/blob/master/mesonbuild/dependencies/dub.py

i.e. ``dub build [[@]] []``

So use ``package:sub@1.0.2``.

Also I just noticed meson doesn't support shared libraries from dub, so 
something to keep in mind.


Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn

On Tuesday, 11 April 2023 at 10:24:09 UTC, Ki Rill wrote:

My `dub.json`:
```Json
{
"authors": [
"rillki"
],
"copyright": "Copyright © 2023, rillki",
"dependencies": {
"bindbc-sfml": "~>1.0.2"
},
"description": "D/SFML project template",
"license": "BSL",
"name": "d-sfml-project-template",
"targetPath": "bin",
"versions": [
"SFML_Audio",
"SFML_Graphics",
"SFML_Network",
"SFML_System",
"SFML_Window",
"SFML_250"
]
}
```

I will try to make it work... Meanwhile, if someones spots what 
I am doing wrong, please reply to this post.


If I use your dub.json, my terminal screen says:


user@debian:~/Downloads$ cd dsfml
user@debian:~/Downloads/dsfml$ dub
Fetching bindbc-sfml 1.0.2 (getting selected version)
 Fetching bindbc-loader 1.0.3 (getting selected version)
 Starting Performing "debug" build using /usr/bin/dmd for 
x86_64.

 Building bindbc-loader 1.0.3: building configuration [noBC]
 Building bindbc-sfml 1.0.2: building configuration 
[dynamic]
 Building d-sfml-project-template ~master: building 
configuration [application]

  Linking d-sfml-project-template
  Running bin/d-sfml-project-template
Error Program exited with code -11


If I add "BindSFML_Static" to the DUB's version directive, I 
switch to static, which I guess you don't want. But then I also 
get a bunch of undefined errors:


/usr/bin/ld: 
/home/user/.dub/cache/d-sfml-project-template/~master/build/application-debug-TGcRCxHHanKq_MbfWLee8g/d-sfml-project-template.o: in function `_Dmain':
/home/user/Downloads/dsfml/source/app.d:11: undefined reference 
to `sfRenderWindow_create'

...
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
Error /usr/bin/dmd failed with exit code 1.


If you wanted static I would add and run libraries easy, like 
this:

```Json
"libs": [
"csfml-audio",
"csfml-graphics"
],
"versions": [
"SFML_Audio",
"SFML_Graphics",
"BindSFML_Static",
]
}
```
In summary, DUB takes care of everything for us. It should be 
same in W$...


SDB@79



Using DUB packages with Meson

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn
I'm trying to use my new DUB package from Photon, which is 
polyglot project and is built with Meson.


Have anyone worked with DUB packages in Meson? I've found this 
bit of documentation:

https://mesonbuild.com/Dependencies.html#dub

And I did fetch & build but I do not understand how to introduce 
library dependency on a specific DUB package.


--
Dmitry Olshansky


Re: learning D as your first language but having difficulty in making or logic building in order to make software

2023-04-12 Thread Dom DiSc via Digitalmars-d-learn

On Tuesday, 11 April 2023 at 14:13:17 UTC, slectr wrote:
I want  to make software like krita inkscape and my own 
language using D


Although i previosly learned C and C++ i left it in the middle 
and for some reasons i dont want to learn those so i searched 
for alternatives and found D but there are not a lot of 
resources like cookbooks and videos as compared to C or C++ 
does anybody know any resources for making complex software in 
D for some practise


? Of course there are cookbooks - one even has explicitly that 
title!

Have a look on forum.dlang.org under Resources/Books.



Re: Help with registering dub package

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 08:25:54 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

Your branches + tag is all messed up.

You have both ~master and ~main. ~main has dub.json (which is 
required), but the tag is based upon ~master which has your 
README.


Between the two branches everything is there, its just that it 
needs to be all in one.


Thanks, Rikki!

—
Dmitry Olshansky




Re: Help with registering dub package

2023-04-12 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

Your branches + tag is all messed up.

You have both ~master and ~main. ~main has dub.json (which is required), 
but the tag is based upon ~master which has your README.


Between the two branches everything is there, its just that it needs to 
be all in one.


Help with registering dub package

2023-04-12 Thread Dmitry Olshansky via Digitalmars-d-learn

Could someone walk me through the steps of publish my dub package?

I'm stuck with this:
https://code.dlang.org/packages/strand

For some reason code.dlang.org cannot find my semver tag I guess.

--
Dmitry Olshansky