Re: startsWith

2023-09-30 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 1 October 2023 at 05:33:36 UTC, Joel wrote:

```d
void main() {
import std.string : split;
import std.algorithm.searching : startsWith;

string bk="Exo";

assert(("Gen Exo Lev Num Deu Jos Judg Rut 1Sam 2Sam".split~
"1Kin 2Kin 1Chr 2Chr Ezra Neh Est Job Psa Pro Ecc 
Son Isa Jer".split~
"Lam Eze Dan Hos Joel Amos Oba Jon Mic Nah Hab Zep 
Hag Zec".split~
	"Mal Mat Mar Luk Joh Act Rom 1Cor 2Cor Gal Eph Phili 
Col".split~
	"1The 2The Titu Phile Heb Jam 1Pet 2Pet 1Joh 2Joh 3Joh 
Jude Rev".split)

.startsWith(bk));
}
```

Why doesn't this work? canFind works though.


Because it starts with Gen, not Exo.


startsWith

2023-09-30 Thread Joel via Digitalmars-d-learn

```d
void main() {
import std.string : split;
import std.algorithm.searching : startsWith;

string bk="Exo";

assert(("Gen Exo Lev Num Deu Jos Judg Rut 1Sam 2Sam".split~
"1Kin 2Kin 1Chr 2Chr Ezra Neh Est Job Psa Pro Ecc Son 
Isa Jer".split~
"Lam Eze Dan Hos Joel Amos Oba Jon Mic Nah Hab Zep 
Hag Zec".split~
	"Mal Mat Mar Luk Joh Act Rom 1Cor 2Cor Gal Eph Phili 
Col".split~
	"1The 2The Titu Phile Heb Jam 1Pet 2Pet 1Joh 2Joh 3Joh 
Jude Rev".split)

.startsWith(bk));
}
```

Why doesn't this work? canFind works though.



SumType structure wrapping seems to fail (dmd v2.105.2)

2023-09-30 Thread Chris Piker via Digitalmars-d-learn

Hi D

As suggested in other threads I've tried wrapping a SumType in a 
structure to add functionality and used `alias ... this` to make 
assignment, etc. easier.  However the following code fails in dmd 
2.105.2.


```d
import std.sumtype;

struct Item{
  SumType!(void*, byte[3],  ubyte[3], string[3]) value;
  alias value this;
  this(T)(T thing){ value = thing;}
}

void main(){
  Item item;
  byte[3] byte_vec = [0x01, 0x02, 0x03];
  item.value = byte_vec;   // <-- works
  item = byte_vec; // <-- fails to compile
}
```
Is there some important detail I'm missing?  The compiler error 
message is:

```
Error: generated function `test_sumtype3.Item.opAssign(Item p)` 
is not

   callable using argument types `(byte[3])`
   cannot pass argument `stuff` of type `byte[3]` to parameter
   `Item p`
```

Thanks for any suggestions,



Re: How to print current type of a SumType?

2023-09-30 Thread Chris Piker via Digitalmars-d-learn

We posted at the same time!  Thanks for help nonetheless.

I do hope over time, SumTypes get the Ali Çehreli treatment.  I 
keep his book open on my desk all the time.





Re: How to print current type of a SumType?

2023-09-30 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 1 October 2023 at 01:17:50 UTC, Chris Piker wrote:

Hi D

I've a simple question but it's bedeviling me anyway.  How do I 
get a string representation of the current type of a SumType?  
I'm trying to avoid something like this:


```d
alias Vec3 = SumType!(void* /* invalid vector */, byte[3], 
short[3], char[][3]);


string prnType(Vec3 vec){

  return vec.match!(
 (void* _) => "void*",
 (byte[3] _) => "byte[3]",
 (short[3] _) => "short[3]",
 (char[][3] _) => "char[][3]"
  );
}
```

I'm sure there's a much easier way, especially once there's a 
largish number of vector types.


Thanks,


```d
return vec.match(value => typeof(value).stringof);
```


Re: How to print current type of a SumType?

2023-09-30 Thread Chris Piker via Digitalmars-d-learn

On Sunday, 1 October 2023 at 01:17:50 UTC, Chris Piker wrote:

```d
alias Vec3 = SumType!(void* /* invalid vector */, byte[3], 
short[3], char[][3]);


```



I know it's bad form to reply to my own question, but I think I 
found a reasonably simple way:


```d
string prnType(Vec3 vec){
   return vec.match!( t => typeof(t).stringof);
}
```

which is hardly worth making into a function of it's own.  Other 
suggestions are welcome of course.


SumTypes are really tripping me up.  Unlearning years of writing 
C takes more time then I'd prefer.




How to print current type of a SumType?

2023-09-30 Thread Chris Piker via Digitalmars-d-learn

Hi D

I've a simple question but it's bedeviling me anyway.  How do I 
get a string representation of the current type of a SumType?  
I'm trying to avoid something like this:


```d
alias Vec3 = SumType!(void* /* invalid vector */, byte[3], 
short[3], char[][3]);


string prnType(Vec3 vec){

  return vec.match!(
 (void* _) => "void*",
 (byte[3] _) => "byte[3]",
 (short[3] _) => "short[3]",
 (char[][3] _) => "char[][3]"
  );
}
```

I'm sure there's a much easier way, especially once there's a 
largish number of vector types.


Thanks,