template/mixin magic for to! auto inferring type from variable

2024-02-01 Thread Chris Katko via Digitalmars-d-learn

Is there some way to do:
```D
string[3] data; //strings from some file input, some are ints, 
uints, etc.


auto into!(T)(T value){return to!???(value); } // ???

uint time = into!(data[1]); // We already know this is uint
int priority = into!(data[2]);
```

instead of:
```D
uint time = to!uint(data[1]); // specifying type twice
int priority = to!int(data[2])
```


Re: import locality with function parameters

2024-02-01 Thread Carl Sturtivant via Digitalmars-d-learn
On Friday, 2 February 2024 at 01:23:11 UTC, Steven Schveighoffer 
wrote:

Are you thinking of this?

https://dlang.org/phobos/object.html#.imported

-Steve


Yes! Glad it's now part of D.
Thank you.




Re: how can I load html files and not .dt or diet files in vibe.d

2024-02-01 Thread Menjanahary R. R. via Digitalmars-d-learn

On Thursday, 1 February 2024 at 03:20:31 UTC, dunkelheit wrote:
this is my code, I'm a begginer on vibe, and I want to use html 
and not diet files


`import vibe.vibe;

void main()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
auto listener = listenHTTP(settings, );
scope (exit)
{
listener.stopListening();
}

logInfo("Please open http://127.0.0.1:8080/ in your browser.");
runApplication();
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
res.render!"hola.dt";
}`

I tried to load my html through iframe but it not works

!!! 5
html
body
h1 hello diet
-iframe("hola.html")


please help me :')


Explore this link https://github.com/D-Programming-GDC/gdcproject 
for potential sources of inspiration.





Re: Setting field of struct object

2024-02-01 Thread TheTeaLady via Digitalmars-d-learn

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:


```d
struct Person {
string name, email;
ulong age;
}
Person a{"n","email",33};
```



C++ can achieve ultimate `simplicity` without violating `DRY`,
And here, D violates the `DRY` principle!
Moreover, as the `package level, module level, class level, 
member level`, D language violates integrity.

Because D has no `class level` limit.
These are all not `serious states`.


Apparently comments about module visibility issues are no longer 
allowed on the D forum.


I wonder why your comment was not deleted though ??

No wonder there is now a fork of D.


Re: import locality with function parameters

2024-02-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On Friday, 2 February 2024 at 00:29:51 UTC, Carl Sturtivant wrote:

Hello,

I seem to recall that there is surprising template to import a 
module and get a type from it inside the declaration of the 
type of a parameter to a function, so that the module need not 
be imported outside of the function definition. I think there 
was a blog article some years ago about this where there was 
some indication that this or something with equivalent effect 
would be incorporated into D in some way.


How do I define a function with a parameter that is a type in 
an outside module while keeping module import local to that 
definition?


Are you thinking of this?

https://dlang.org/phobos/object.html#.imported

-Steve


import locality with function parameters

2024-02-01 Thread Carl Sturtivant via Digitalmars-d-learn

Hello,

I seem to recall that there is surprising template to import a 
module and get a type from it inside the declaration of the type 
of a parameter to a function, so that the module need not be 
imported outside of the function definition. I think there was a 
blog article some years ago about this where there was some 
indication that this or something with equivalent effect would be 
incorporated into D in some way.


How do I define a function with a parameter that is a type in an 
outside module while keeping module import local to that 
definition?




Re: Setting field of struct object

2024-02-01 Thread Anonymouse via Digitalmars-d-learn

On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:

```d
import std;

struct Person {
string name, email;
ulong age;
auto withName(string name) { this.name=name; return this; }
auto withEmail(string email) { this.email=email; return 
this; }

auto withAge(ulong age) { this.age=age; return this; }
}

void main() {
Person p;

p.withName("Tom").withEmail("joel...@gmail.com").withAge(44);

writeln(p);
}
```


I had reason to need this to work a while ago and `opDispatch` 
came in very handy. I was able to cook up one that forwarded 
calls to other members in a struct, while returning `this` by 
ref, allowing for chaining calls. I use it with UDAs.


(Scroll to the unit tests for examples.)

```d
/++
Mixin template generating an `opDispatch` redirecting calls 
to members whose
names match the passed variable string but with an underscore 
prepended.

 +/
mixin template UnderscoreOpDispatcher()
{
ref auto opDispatch(string var, T)(T value)
{
import std.traits : isArray, isAssociativeArray, 
isSomeString;


enum realVar = '_' ~ var;
alias V = typeof(mixin(realVar));

static if (isAssociativeArray!V)
{
// Doesn't work with AAs without library solutions
}
else static if (isArray!V && !isSomeString!V)
{
mixin(realVar) ~= value;
}
else
{
mixin(realVar) = value;
}

return this;
}

auto opDispatch(string var)() inout
{
enum realVar = '_' ~ var;
return mixin(realVar);
}
}

///
unittest
{
struct Foo
{
int _i;
string _s;
bool _b;
string[] _add;
alias wordList = _add;

mixin UnderscoreOpDispatcher;
}

Foo f;
f.i = 42; // f.opDispatch!"i"(42);
f.s = "hello";// f.opDispatch!"s"("hello");
f.b = true;   // f.opDispatch!"b"(true);
f.add("hello");   // f.opDispatch!"add"("hello");
f.add("world");   // f.opDispatch!"add"("world");

assert(f.i == 42);
assert(f.s == "hello");
assert(f.b);
assert(f.wordList == [ "hello", "world" ]);

auto f2 = Foo()
.i(9001)
.s("world")
.b(false)
.add("hello")
.add("world");

assert(f2.i == 9001);
assert(f2.s == "world");
assert(!f2.b);
assert(f2.wordList == [ "hello", "world" ]);
}
```

You could trivially adapt it to use a `withName`, `withEmail` 
calling scheme instead of the underscore thing.


Re: Setting field of struct object

2024-02-01 Thread TheTeaLady via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:47:23 UTC, bachmeier wrote:

On Monday, 22 January 2024 at 15:45:45 UTC, zjh wrote:

On Monday, 22 January 2024 at 15:33:01 UTC, ryuukk_ wrote:

it only took me 1 project to never want to touch C++ again..



D language used to have no `copy constructor`, isn't it now 
added in again?


You have to admit the good aspects of `C++`.
You should take a look at the `latest C++`. C++ has already 
learned many advantages of `D`, but D has not made 
`significant progress`!
As a user, `C++` is really not much different from D, and even 
surpasses D `in many aspects`.
`RAII `, `variable parameter` template, `coroutine, concept`, 
`value semantics`, very easy to understand.
Moreover, the `inheritance` of C++ is very enjoyable to use in 
many aspects.


Sounds like you should be using C++. Why are you here?


Everyone who uses D should be welcome, even if they have a 
preference for another language, or a preference for features in 
another language.


This is how D will evolve and improve.

And this should include people who don't like the untyped 
universe of the D module (specifically, its lack of an optional 
mechanism for information hiding within a module). People can 
think of it as if it were typed, but this is just an illusion. 
This illusion can easily lead to type confusion and inconsistent 
and erroneous interactions - i.e. bugs.
To avoid this in D one needs to literally reorganise that untyped 
universe into into a typed system. Now your types impose 
constraints which will help to enforce their correctness. 
Organisation is important of course, but for using types in D, 
its vital - if strong typing is your objective.


I think D is clumsy in many other areas as well.

btw. I too prefer multiple inheritance.


Re: how can I load html files and not .dt or diet files in vibe.d

2024-02-01 Thread Rene Zwanenburg via Digitalmars-d-learn

On Thursday, 1 February 2024 at 03:20:31 UTC, dunkelheit wrote:
this is my code, I'm a begginer on vibe, and I want to use html 
and not diet files


Take a look at the static file example, I think that's close to 
what you're looking for.


https://github.com/vibe-d/vibe.d/blob/master/examples/http_static_server/source/app.d


Re: how can I load html files and not .dt or diet files in vibe.d

2024-02-01 Thread Aravinda VK via Digitalmars-d-learn

On Thursday, 1 February 2024 at 03:20:31 UTC, dunkelheit wrote:


!!! 5
html
body
h1 hello diet
-iframe("hola.html")


please help me :')


Try

```
html
body
h1 hello diet
iframe(src="hola.html")
```