Re: Quotes and backticks together in a raw string

2021-08-18 Thread Brian Tiffin via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 22:34:54 UTC, jfondren wrote:
On Wednesday, 18 August 2021 at 22:18:59 UTC, Brian Tiffin 
wrote:

Google fu is failing on this one.


string docs are at https://dlang.org/spec/lex.html

Is there a way to have a raw multi-line string literal with 
both double-quotes and backticks inside?




https://dlang.org/spec/lex.html#delimited_strings give you most 
freedom. Token strings might also be appropriate.


```d
import std.string : stripRight;

enum quotes = q"EX
`backticks`
"doublequotes"
EX".stripRight; // otherwise ends in a newline
```


Thank you.  I misread

When quotes appear in the document, Wysiwyg strings (see below) 
or heredoc strings can be used.


as meaning: heredoc strings are another name for Wysiwyg strings. 
;-)


Mystery solved.

Have good.


Re: How to get element type of a slice?

2021-08-18 Thread jfondren via Digitalmars-d-learn

On Thursday, 19 August 2021 at 03:32:47 UTC, Jesse Phillips wrote:
On Thursday, 19 August 2021 at 03:29:03 UTC, Jesse Phillips 
wrote:
On Tuesday, 17 August 2021 at 12:33:03 UTC, Ferhat Kurtulmuş 
wrote:

On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote:
On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş 
wrote:

[...]


This one's not in std.traits:

```d
import std.range : ElementType;

struct Point { int x, y; }

unittest {
Point[] points;
assert(is(ElementType!(typeof(points)) == Point));
}
```


Hey, thank you again but, I don't want an instance of Point[] 
I need:


alias T = Point[];

alias ElementOfPointSlice =  // element type of T




Sorry last post was not complete. Not tested.


```
 alias T = Point[];
alias ElementOfPointSlice = ElementType!(T);

 unittest {
 assert(is(ElementOfPointSlice == Point));
  }
```


so, what's the problem? This passes tests:

```d
import std.range : ElementType;

struct Point { int x, y; }
alias T = Point[];
alias ElementOfPointSlice = ElementType!(T);

unittest {
assert(is(ElementOfPointSlice == Point));
}
```


Re: How to get element type of a slice?

2021-08-18 Thread Jesse Phillips via Digitalmars-d-learn

On Thursday, 19 August 2021 at 03:29:03 UTC, Jesse Phillips wrote:
On Tuesday, 17 August 2021 at 12:33:03 UTC, Ferhat Kurtulmuş 
wrote:

On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote:
On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş 
wrote:

[...]


This one's not in std.traits:

```d
import std.range : ElementType;

struct Point { int x, y; }

unittest {
Point[] points;
assert(is(ElementType!(typeof(points)) == Point));
}
```


Hey, thank you again but, I don't want an instance of Point[] 
I need:


alias T = Point[];

alias ElementOfPointSlice =  // element type of T




Sorry last post was not complete. Not tested.


```
 alias T = Point[];
alias ElementOfPointSlice = ElementType!(T);

 unittest {
 assert(is(ElementOfPointSlice == Point));
  }
```




Re: How to get element type of a slice?

2021-08-18 Thread Jesse Phillips via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 12:33:03 UTC, Ferhat Kurtulmuş 
wrote:

On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote:
On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş 
wrote:

[...]


This one's not in std.traits:

```d
import std.range : ElementType;

struct Point { int x, y; }

unittest {
Point[] points;
assert(is(ElementType!(typeof(points)) == Point));
}
```


Hey, thank you again but, I don't want an instance of Point[] I 
need:


alias T = Point[];

alias ElementOfPointSlice =  // element type of T



```
alias T = Point[];

unittest {
 Point[] points;
 assert(is(ElementType!(typeof(points)) == Point));
 }


Re: std.stdio.File is throwing with the message of: "Access Violation"

2021-08-18 Thread Jesse Phillips via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster 
wrote:
  All I did was try to access a file with a self-made library.  
It didn't work.  I tried again directly from the main file. 
This is the code:


  ```d
  File file = 
File("E:\\Users\\User\\Desktop\\dutils\\test.spr","r"); //This 
file exists on my system, so it should work...

  file.close();
  ```

  Output(Given to me by a message box that display's 
Throwable.msg in it's body):

 Access Violation

  Is this a bug, or me being stupid? If it's the latter, than 
tell me what went wrong.  I am using DMD 2.097.2


This is an error message you'll get from Windows if the file is 
locked (open by another application).


Re: Quotes and backticks together in a raw string

2021-08-18 Thread jfondren via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 22:18:59 UTC, Brian Tiffin wrote:

Google fu is failing on this one.


string docs are at https://dlang.org/spec/lex.html

Is there a way to have a raw multi-line string literal with 
both double-quotes and backticks inside?




https://dlang.org/spec/lex.html#delimited_strings give you most 
freedom. Token strings might also be appropriate.


```d
import std.string : stripRight;

enum quotes = q"EX
`backticks`
"doublequotes"
EX".stripRight; // otherwise ends in a newline
```


Quotes and backticks together in a raw string

2021-08-18 Thread Brian Tiffin via Digitalmars-d-learn

Google fu is failing on this one.

Is there a way to have a raw multi-line string literal with both 
double-quotes and backticks inside?


Catenation works fine, busting up the literal, but can it be a 
single literal with both characters?


Not a biggy, just curious.


Re: std.stdio.File is throwing with the message of: "Access Violation"

2021-08-18 Thread Ruby The Roobster via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 17:54:47 UTC, Paul Backus wrote:
On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster 
wrote:
  Output(Given to me by a message box that display's 
Throwable.msg in it's body):

 Access Violation

  Is this a bug, or me being stupid? If it's the latter, than 
tell me what went wrong.  I am using DMD 2.097.2


It's a bug in your code. "Access Violation" means your program 
tried to access out-of-bounds memory.


In addition, I figured out that if I moved the code outside of 
WndProc() everything worked fine, so I think it's a Win32 issue.


Re: How to get element type of a slice?

2021-08-18 Thread jmh530 via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 14:40:20 UTC, Ferhat Kurtulmuş 
wrote:

[snip]

Very informative, thanks. My code is lying here[1]. I want my 
struct to accept 2d static arrays, random access ranges, and 
"std.container.Array". I could achieve it with its present 
form, and I will probably slightly modify it based on your 
comments.


[1]: 
https://github.com/aferust/earcut-d/blob/master/source/earcutd.d#L34


If it would only accept dynamic arrays, you could use something 
like below


```d
import std.traits: isDynamicArray;

template DynamicArrayOf(T : U[], U)
if (isDynamicArray!T)
{
alias DynamicArrayOf = U;
}

struct Point {}

void main()
{
static assert(is(DynamicArrayOf!(int[]) == int));
static assert(is(DynamicArrayOf!(Point[]) == Point));
}
```


Vibe.d error

2021-08-18 Thread JG via Digitalmars-d-learn

Hi,

We are intermittently getting the following error:
Accept TLS connection: server
OpenSSL error at ../ssl/record/rec_layer_s3.c:1543: 
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert 
certificate unknown (SSL alert number 46)
HTTP connection handler has thrown: Accepting SSL tunnel: 
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert 
certificate unknown (336151574)
Full error: 
object.Exception@/home/jg/.dub/packages/vibe-d-0.9.3/vibe-d/tls/vibe/stream/openssl.d(578): Accepting SSL tunnel: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown (336151574)



Anyone have any idea what might cause this?


Re: std.stdio.File is throwing with the message of: "Access Violation"

2021-08-18 Thread Ruby The Roobster via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 17:54:47 UTC, Paul Backus wrote:
On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster 
wrote:
  Output(Given to me by a message box that display's 
Throwable.msg in it's body):

 Access Violation

  Is this a bug, or me being stupid? If it's the latter, than 
tell me what went wrong.  I am using DMD 2.097.2


It's a bug in your code. "Access Violation" means your program 
tried to access out-of-bounds memory.


When I removed those two lines of code, the program ran perfectly 
without displaying any error or throwing any exception...


Re: std.stdio.File is throwing with the message of: "Access Violation"

2021-08-18 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster 
wrote:
  Output(Given to me by a message box that display's 
Throwable.msg in it's body):

 Access Violation

  Is this a bug, or me being stupid? If it's the latter, than 
tell me what went wrong.  I am using DMD 2.097.2


It's a bug in your code. "Access Violation" means your program 
tried to access out-of-bounds memory.


std.stdio.File is throwing with the message of: "Access Violation"

2021-08-18 Thread Ruby The Roobster via Digitalmars-d-learn
  All I did was try to access a file with a self-made library.  
It didn't work.  I tried again directly from the main file. This 
is the code:


  ```d
  File file = 
File("E:\\Users\\User\\Desktop\\dutils\\test.spr","r"); //This 
file exists on my system, so it should work...

  file.close();
  ```

  Output(Given to me by a message box that display's 
Throwable.msg in it's body):

 Access Violation

  Is this a bug, or me being stupid? If it's the latter, than 
tell me what went wrong.  I am using DMD 2.097.2


Re: Non-consistent implicit function template specializations

2021-08-18 Thread Ali Çehreli via Digitalmars-d-learn

On 8/18/21 4:10 AM, Rekel wrote:

>>   isArray!T && (isArray!(ElementType!T))
>
> I tried looking into how isArray is defined. Like, does being able to
> index mean it's an array, or are these only static &/or dynamic arrays?

The definitions are in phobos/std/traits.d

  enum bool isArray(T) = isStaticArray!T || isDynamicArray!T;

isStaticArray uses the compiler's __traits feature:

  enum bool isStaticArray(T) = __traits(isStaticArray, T);

isDynamicArray uses the is expression but apparently has some history:

template isDynamicArray(T)
{
static if (is(T == U[], U))
enum bool isDynamicArray = true;
else static if (is(T U == enum))
// BUG: isDynamicArray / isStaticArray considers enums
// with appropriate base types as dynamic/static arrays
// Retain old behaviour for now, see
// https://github.com/dlang/phobos/pull/7574
enum bool isDynamicArray = isDynamicArray!U;
else
enum bool isDynamicArray = false;
}

Ali



Re: Non-consistent implicit function template specializations

2021-08-18 Thread Rekel via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 13:35:07 UTC, Paul Backus wrote:

On Wednesday, 18 August 2021 at 11:10:49 UTC, Rekel wrote:
I tried looking into how isArray is defined. Like, does being 
able to index mean it's an array, or are these only static 
&/or dynamic arrays?


Did you read the documentation?

https://phobos.dpldocs.info/std.traits.isArray.html


Ah it's specifically static & dynamic arrays, I see.


Re: Non-consistent implicit function template specializations

2021-08-18 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 11:10:49 UTC, Rekel wrote:
I tried looking into how isArray is defined. Like, does being 
able to index mean it's an array, or are these only static &/or 
dynamic arrays?


Did you read the documentation?

https://phobos.dpldocs.info/std.traits.isArray.html


Re: Non-consistent implicit function template specializations

2021-08-18 Thread Rekel via Digitalmars-d-learn

On Tuesday, 17 August 2021 at 18:46:05 UTC, Ali Çehreli wrote:
I don't have such problems because I am not smart enough to 
understand that syntax so I don't use it. :) I use template 
constraints (which have other problems).


Yeah, they seem to be a bit more trustworthy to some extent.


If you want 2 dimensional arrays, then you can use

import std.range;

  isArray!T && (isArray!(ElementType!T))


I tried looking into how isArray is defined. Like, does being 
able to index mean it's an array, or are these only static &/or 
dynamic arrays? Though I couldn't understand the sourcecode.
Hence I just use(d) `is(typeof(variable[0]))` and 
`is(typeof(variable[0][0]))`.


Re: Non-consistent implicit function template specializations

2021-08-18 Thread Rekel via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 18:27:21 UTC, Steven Schveighoffer 
wrote:
According to my tests, it prefers the `T` version over the 
static array version. Which leads me to believe that it prefers 
a dynamic array over a static one. In fact, if I comment out 
the `T` version, it doesn't compile. It literally will not pick 
that specialization, even if it can interpret the literal that 
way.


which is really bizarre, since if you do it without 
specializations, but just spelling out all the template 
components (as in your alternative workaround), it WILL pick 
that one over a dynamic array one.


Oh my, that's weird... Not meant to bash but given all I've seen 
of argument deduction & templates & specializations... I think 
the implementation needs some serious rework 


---

Interestingly enough my approach will not even work for 2d array 
literals. It will manage going to int[][2], but int[2][2] is one 
step too far. Which is a real bummer. :(
That is, it won't figure it out itself, but when you call `foo(T, 
uintL)(T[L][L]...` using an explicit `foo!(int, 2)` it _will_ 
work. Even though it manages T[L] just fine.
Meanwhile T[2][L] _and_ T[L][2] won't work when called with a 2x2 
array literal.


Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread james.p.leblanc via Digitalmars-d-learn

On Wednesday, 18 August 2021 at 06:53:51 UTC, Tejas wrote:


void funcTemplate(T:int)(T a){
 writeln("argument is int");
}

void funcTemplate(T : long)(T a){
 writeln("argument is long integer");
}

void main(){
 int a;
 long b;
 func(a);
 func(b);

 funcTemplate(a);
 funcTemplate(b);

}

```



Domninikus, Tejas, and All,

I see that I had been (stupidly) omitting the exclamation point
after **"isIntegral"** in my clumsy attempts to use the traits ...
thanks for your clear example helping me identify my error.

Also, thanks for mentioning the words **"template 
specialization"** , I did

not know what to call the use of **":"** in templates.

Now, I have a search term I can use to learn more ...and find it 
is in Phillippe

Signaud's informative "D-templates-tutorial".

Thanks again,
James

PS Also, I am enjoying, a entertaining and educational tutorial 
that Phillippe

has linked in his tutorial.  Other may learn from this as well:

http://www.semitwist.com/articles/EfficientAndFlexible/SinglePage/




Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread Tejas via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 05:33:13 UTC, james.p.leblanc 
wrote:
On Tuesday, 17 August 2021 at 20:28:20 UTC, Alexandru Ermicioi 
wrote:
On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc 
wrote:


Wow!  That is absolutely beautiful ... I had never seen (or 
even
imagined) a recursive template!  This expands my mind in a 
good

way ... and is going into my toolbox immediately.

Best Regards,
James


Just don't over rely on it. It can cause compilation 
slowdowns, so avoid it if you can.


I've been happily trying to absorb all the helpful concepts 
posted.


A final related question in the quest for simplicity and 
robustness.


If I wanted to ensure that a function accepts only arguments of
byte, int, uint, long, etc.  (i.e. integer-like types).  Is the 
accepted

way to do this like so?:

**auto foo( T : long )(T a, T b){ ... }**

I really wish I could find a good explanation of the ":" (colon)
used in templates.  I am sure one exists ...but haven't come 
upon

it just yet.

(I thought "isIntegral" in traits module would be helpful...but
I failed in my experiments.)

Thanks for patience with this question!

Best Regards,
James


A template specialization is basically a template overload.

For example:

```d

void func(int a){
  writeln("argument is integer");
}

void func(long a){
  writeln("argument is long");
}

void main(){
 int a;
 long b;
 func(a);
 func(b);
}

```

The above does what you expect.

Now the template specialization way:
```d

void funcTemplate(T:int)(T a){
 writeln("argument is int");
}

void funcTemplate(T : long)(T a){
 writeln("argument is long");
}

void main(){
 int c;
 long d;
 funcTemplate(c);
 funcTemplate(d);
}
```

The above will also do what you expect.

Template specialization is basically overloading for templates, 
nothing more.


Below is a complete working copy-paste example combining both:


```d

import std;



void func(int a){
  writeln("argument is integer");
}

void func(long a){
  writeln("argument is long");
}



void funcTemplate(T:int)(T a){
 writeln("argument is int");
}

void funcTemplate(T : long)(T a){
 writeln("argument is long integer");
}

void main(){
 int a;
 long b;
 func(a);
 func(b);

 funcTemplate(a);
 funcTemplate(b);

}

```




Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 05:33:13 UTC, james.p.leblanc 
wrote:

If I wanted to ensure that a function accepts only arguments of
byte, int, uint, long, etc.  (i.e. integer-like types).  Is the 
accepted way to do this like so?:


**auto foo( T : long )(T a, T b){ ... }**


I very much prefer the ususal constraint syntax

**auto foo(T)(T a, T b) if(isIntegral!T) { ... }**