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

2023-01-20 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 20, 2023 at 01:32:22PM -0800, Ali Çehreli via Digitalmars-d-learn 
wrote:
> On 1/20/23 07:01, torhu wrote:
> 
> > But why not have drawLine just be a free function?
> 
> Exactly.
> 
> If I'm not mistaken, and please teach me if I am wrong, they are
> practically free functions in Java as well. That Java class is working
> as a namespace.

Exactly. Every time you see a static singleton class, you're essentially
looking at a namespace. Only, in OO circles non-class namespaces are
taboo, it's not OO-correct to call them what they are, instead you have
to do lip service to OO by calling them static singleton classes
instead.  And free functions are taboo in OO; OO doctrine declares them
unclean affronts to OO purity and requires that you dress them in more
OO-appropriate clothing, like putting them inside a namesp^W excuse me,
static singleton class.

;-)


> So, the function above is the same as the following free-standing
> function in D, C++, C, and many other languages:
> 
>   void Algo_drawLine(Canvas c, Pos from, Pos to) { .. };
[...]

That way of naming a global function is essentially a poor
man's^W^Wexcuse me, I mean, C's way of working around the lack of a
proper namespacing / module system. In D, we do have a proper module
system, so you could just call the function `drawLine` and put it in a
file named Algo.d, then you can just use D's symbol resolution rules to
disambiguate between Algo.drawLine and PersonalSpace.drawLine, for
example. :-P


T

-- 
Public parking: euphemism for paid parking. -- Flora


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

2023-01-20 Thread Ali Çehreli via Digitalmars-d-learn

On 1/20/23 07:01, torhu wrote:

> But why not have drawLine just be a free function?

Exactly.

If I'm not mistaken, and please teach me if I am wrong, they are 
practically free functions in Java as well. That Java class is working 
as a namespace. So, the function above is the same as the following 
free-standing function in D, C++, C, and many other languages:


  void Algo_drawLine(Canvas c, Pos from, Pos to) { .. };

Ali



Re: vibe.d + mongoDB

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn

On Friday, 20 January 2023 at 18:58:16 UTC, seany wrote:
Hi I am googling to find some vibe.d and mongoDB tutorial. Are 
their some available? Thank you


There is a nice book, titled D Web Development, that despite 
being 6 years old, is still mostly applicable to using vibe.d.  
The only archaic thing in the book is the use of the ```shared 
static this``` module constructor, which no longer works, and 
should be replaced by the main() function.


Re: vibe.d + mongoDB

2023-01-20 Thread Ben Jones via Digitalmars-d-learn

On Friday, 20 January 2023 at 18:58:16 UTC, seany wrote:
Hi I am googling to find some vibe.d and mongoDB tutorial. Are 
their some available? Thank you


There's a couple of examples like this one in main vibe repo in 
the examples directory: 
https://github.com/vibe-d/vibe.d/tree/master/examples/mongodb


When I used it recently, I ran into some issues where upsert 
operations I wanted to do had changed their wire format in mongo 
5 (I think), so I had to use run mongo 4


I'm not aware of any larger tutorials


vibe.d + mongoDB

2023-01-20 Thread seany via Digitalmars-d-learn
Hi I am googling to find some vibe.d and mongoDB tutorial. Are 
their some available? Thank you


Re: Is there a way to get a template’s parameters and constraints?

2023-01-20 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 20 January 2023 at 17:15:31 UTC, Quirin Schroll wrote:
For what I want, `constraintsOf` may expect every template 
parameter to be a type and to have a constraint.



If I'm not mistaken, the following will help:

https://dlang.org/phobos/std_range_primitives.html

SDB@79=


Re: Is there a way to get a template’s parameters and constraints?

2023-01-20 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 20 January 2023 at 17:15:31 UTC, Quirin Schroll wrote:
Is there a trait (or a combination of traits) that gives me the 
constraints of a template?


No, reflection over templates is very limited.



Re: Is there a way to get a template’s parameters and constraints?

2023-01-20 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/20/23 12:15 PM, Quirin Schroll wrote:
Is there a trait (or a combination of traits) that gives me the 
constraints of a template?


Example:
```D
void f(T1 : long, T2 : const(char)[])(T x) { }
template constraintsOf(alias templ) { /*Magic here*/ }
alias constraints = constraintsOf!f; // tuple(long, const(char)[])
```

At the moment, I care about constraints that are types. I don’t care 
about value or alias constraints (e.g. `opBinary(string op : "+")(..)` 
or `f(alias x : something)()`, but if it works for types, it should 
probably work for other constraints as well.


For what I want, `constraintsOf` may expect every template parameter to 
be a type and to have a constraint.


No, there is no way to introspect anything about a template's details 
until its instantiated.


-Steve


Is there a way to get a template’s parameters and constraints?

2023-01-20 Thread Quirin Schroll via Digitalmars-d-learn
Is there a trait (or a combination of traits) that gives me the 
constraints of a template?


Example:
```D
void f(T1 : long, T2 : const(char)[])(T x) { }
template constraintsOf(alias templ) { /*Magic here*/ }
alias constraints = constraintsOf!f; // tuple(long, const(char)[])
```

At the moment, I care about constraints that are types. I don’t 
care about value or alias constraints (e.g. `opBinary(string op : 
"+")(..)` or `f(alias x : something)()`, but if it works for 
types, it should probably work for other constraints as well.


For what I want, `constraintsOf` may expect every template 
parameter to be a type and to have a constraint.


Re: What is the 'Result' type even for?

2023-01-20 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 20, 2023 at 12:49:54PM +, Ruby The Roobster via 
Digitalmars-d-learn wrote:
[...]
> Thank you.  I didn't know that there was such a property `.array`.

It's not a property, it's a Phobos function from std.array.


T

-- 
INTEL = Only half of "intelligence".


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: Non-ugly ways to implement a 'static' class or namespace?

2023-01-20 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/20/23 6:28 AM, thebluepandabear wrote:


This type of semantics is not possible in D, which sucks.


Well, static methods do exactly this.

If you want to disable class creation, then use `@disable this();`, if 
you want to make all methods static, put `static:` at the top of the class.


Note that at this point, the class becomes a namespace. You can use a 
template as a namespace as well, though it can be a bit ugly:


```d
template Algo_ns()
{
   void drawLine(Canvas c, Pos from, Pos to) {...}
}

// need this to avoid the instantiation syntax
alias Algo = Algo_ns!();
```

The benefit here is that other things that classes do (generate 
typeinfo, add to the type system, etc.) would be wasted, so this is not 
done for a template.


-Steve


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

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn

On Friday, 20 January 2023 at 13:38:52 UTC, Hipreme wrote:
On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear 
wrote:

...
[snip]

With a single file, you can do:
```d

final class Algo
{
@disable this();
static:
void drawLine(...){}
}
```


This also works, but it dissimilar to Java in that in Java, each 
class gets its own file.


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

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn

On Friday, 20 January 2023 at 13:38:47 UTC, evilrat wrote:
On Friday, 20 January 2023 at 13:17:05 UTC, Ruby The Roobster 
...

[snip]

Also there is various import options such as renamed import or 
static import(doesn't add module to a scope thus requiring to 
fully qualify it)


static import, can be used to somewhat mimic namespaces, more 
complex scenario would be making a module consisting of public 
imports to group things together, but I don't think it is 
common in D.

https://dlang.org/spec/module.html#static_imports

```d
static import std.stdio;

void main()
{
  // nope, this function will not be resolved, compilation error
  // wrtiteln("hello");

  // ok
  std.stdio.writeln("hello");
}
```


I never knew that there even WAS a `static import` option.  Good 
to know.


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

2023-01-20 Thread Hipreme 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) { .. };
}
```




There are 2 solutions for that. One involved doing a private 
implementation:


```d
module drawer.impl;
void drawLine(...)
```

Then, you create another file:

```d
module drawer;
public import Algo = drawer.impl;
```
After that, you can use it as `Algo.drawLine`.


With a single file, you can do:
```d

final class Algo
{
@disable this();
static:
void drawLine(...){}
}
```


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

2023-01-20 Thread evilrat via Digitalmars-d-learn
On Friday, 20 January 2023 at 13:17:05 UTC, Ruby The Roobster 
wrote:
On Friday, 20 January 2023 at 13:03:18 UTC, thebluepandabear 
wrote:

ll
a function without instantiating said class, as functions act 
on the class object.


Ok, thanks.

I think D should implement something similar to `static class` 
but I doubt it will happen.


D isn't Java, and never will be.  If you want similar 
functionality, you put the functions in a separate file, and 
add the line to the top of it:


```d
module modulename;
```

and title the file modulename.d.  Then you can use this module 
as a .class file in java by adding


```d
import modulename;
```

to the file that uses it.


Also there is various import options such as renamed import or 
static import(doesn't add module to a scope thus requiring to 
fully qualify it)


static import, can be used to somewhat mimic namespaces, more 
complex scenario would be making a module consisting of public 
imports to group things together, but I don't think it is common 
in D.

https://dlang.org/spec/module.html#static_imports

```d
static import std.stdio;

void main()
{
  // nope, this function will not be resolved, compilation error
  // wrtiteln("hello");

  // ok
  std.stdio.writeln("hello");
}
```


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

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn
On Friday, 20 January 2023 at 13:03:18 UTC, thebluepandabear 
wrote:

ll
a function without instantiating said class, as functions act 
on the class object.


Ok, thanks.

I think D should implement something similar to `static class` 
but I doubt it will happen.


D isn't Java, and never will be.  If you want similar 
functionality, you put the functions in a separate file, and add 
the line to the top of it:


```d
module modulename;
```

and title the file modulename.d.  Then you can use this module as 
a .class file in java by adding


```d
import modulename;
```

to the file that uses it.


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

2023-01-20 Thread thebluepandabear via Digitalmars-d-learn

ll
a function without instantiating said class, as functions act 
on the class object.


Ok, thanks.

I think D should implement something similar to `static class` 
but I doubt it will happen.


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

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn
On Friday, 20 January 2023 at 12:55:37 UTC, Ruby The Roobster 
wrote:

On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear ...

There is no way to implement that functionality in D.  `final` 
means that the class cannot be extended, and `abstract` 
requires that only an extension of said class can be 
instantiated.  However, unlike in Java and C#, you cannot call 
a function without instantiating said class, as functions act 
on the class object.


Also, there is a `static` keyword, but a `static class` can be 
instantiated as a member of the external class.


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

2023-01-20 Thread Ruby The Roobster 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));
```

This type of semantics is not possible in D, which sucks.

After scouring the forums, the only workaround seems to be the 
following:


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

This solution seems like a bit of a hack, which is why I don't 
like it.


Alternatively you could create a module, but then it would just 
be a function without a clear type.


Is anyone aware of a non-ugly way to implement a 'static' class 
or namespace?


Regards,
thebluepandabear


There is no way to implement that functionality in D.  `final` 
means that the class cannot be extended, and `abstract` requires 
that only an extension of said class can be instantiated.  
However, unlike in Java and C#, you cannot call a function 
without instantiating said class, as functions act on the class 
object.


Re: What is the 'Result' type even for?

2023-01-20 Thread Ruby The Roobster via Digitalmars-d-learn

On Friday, 20 January 2023 at 03:39:48 UTC, H. S. Teoh wrote:
On Fri, Jan 20, 2023 at 03:34:43AM +, Ruby The Roobster via 
Digitalmars-d-learn wrote:
On Friday, 20 January 2023 at 03:30:56 UTC, Steven 
Schveighoffer wrote:

> On 1/19/23 10:11 PM, Ruby The Roobster wrote:
> ...
> 
> The point is to be a range over the original input, 
> evaluated lazily. Using this building block, you can create 
> an array, or use some other algorithm, or whatever you want. 
> All without allocating more space to hold an array.

[...]
I get the point that it is supposed to be lazy.  But why are 
these basic cases not implemented?  I shouldn't have to go 
write a wrapper for something as simple as casting this type 
to the original type. This is one of the things that one 
expects the standard library to do for you.


There's no need to write any wrappers.  Just tack `.array` to 
the end of your pipeline, and you're good to go.



T


Thank you.  I didn't know that there was such a property `.array`.


Re: What is the 'Result' type even for?

2023-01-20 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 20 January 2023 at 04:46:07 UTC, Ali Çehreli wrote:
Different instantiations of templates are distinct types. For 
example, if I called 'alternate' with two 'long' values, both 
alternate!int (as instantiated by the code above) and 
alternate!long would have different MyResult struct types.


In general, the ranges are compatible with each other because 
they use the empty, front, popFront interface. In the example 
below, different types (one of which is double) but the same 
ranges can be combined with chain(). However, it is necessary to 
convert it to array because of the opCmp() compatibility from 
algorithms such as sorting.


```d
  import std.algorithm : sort;
  import std.conv  : to;
  import std.range;
  import std.stdio;


  enum limit = 5;
  enum step = limit / 10.0;/*
  enum step = 1; //*/

void main()
{
  TypeInfo rangeType;   

  auto a = iota(limit);
  auto b = iota(step, limit, step);

  /* <- toggle comment, please add -> /
  auto ab = chain(a, b);
  rangeType = typeid(ab);/*/
  auto arrA = a.array.to!(double[]);
  auto arrB = b.array;
  auto ab = chain(arrA, arrB);
  rangeType = typeid(ab.sort);//*/

  ab.writeln(": ", rangeType);
} /*
current print:
==
[0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3, 3, 3.5, 4, 4, 4.5]: 
std.range.SortedRange!(std.range.chain!(double[], 
double[]).chain(double[], double[]).Result, "a < b", 
0).SortedRange


other print:

[0, 1, 2, 3, 4, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]: 
std.range.chain!(std.range.iota!(int, int).iota(int, int).Result, 
std.range.iota!(double, int, double).iota(double, int, 
double).Result).chain(std.range.iota!(int, int).iota(int, 
int).Result, std.range.iota!(double, int, double).iota(double, 
int, double).Result).Result

```
SDB@79


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

2023-01-20 Thread thebluepandabear via Digitalmars-d-learn

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));
```

This type of semantics is not possible in D, which sucks.

After scouring the forums, the only workaround seems to be the 
following:


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

This solution seems like a bit of a hack, which is why I don't 
like it.


Alternatively you could create a module, but then it would just 
be a function without a clear type.


Is anyone aware of a non-ugly way to implement a 'static' class 
or namespace?


Regards,
thebluepandabear