Thread overview
How make Optional pre determined parameter type without overload function?
Nov 29, 2020
Marcone
Nov 29, 2020
Ali Çehreli
Nov 29, 2020
Marcone
Dec 02, 2020
Marcone
November 29, 2020
void a(T1, T2)(T1 b, T2 c){
	// I need parameter "c" optional, but only (String or int). How can I make it without overload function?
}
November 28, 2020
On 11/28/20 6:40 PM, Marcone wrote:
> void a(T1, T2)(T1 b, T2 c){
>      // I need parameter "c" optional, but only (String or int). How can I make it without overload function?
> }

Since it's optional, T2 must have a default type. I made it 'int' below.

void a(T1, T2 = int)(T1 b, T2 c = T2.init)
if (is (T2 == string) ||
    is (T2 == int))
{
  // ...
}

void main() {
  a(1.5);
  a(2.5, "hello");
  a(3.5, 42);
  static assert(!__traits(compiles, a(4.5, 5.5)));
}

November 29, 2020
On Sunday, 29 November 2020 at 02:55:02 UTC, Ali Çehreli wrote:
> On 11/28/20 6:40 PM, Marcone wrote:
>> void a(T1, T2)(T1 b, T2 c){
>>      // I need parameter "c" optional, but only (String or int). How can I make it without overload function?
>> }
>
> Since it's optional, T2 must have a default type. I made it 'int' below.
>
> void a(T1, T2 = int)(T1 b, T2 c = T2.init)
> if (is (T2 == string) ||
>     is (T2 == int))
> {
>   // ...
> }
>
> void main() {
>   a(1.5);
>   a(2.5, "hello");
>   a(3.5, 42);
>   static assert(!__traits(compiles, a(4.5, 5.5)));
> }

Thank you! work fine!
December 02, 2020
Now my slice works fine.


// Tipo Nulo.
class None {}

// Função slice()
auto slice(T1, T2, T3 = None)(T1 conteudo, T2 inicio, T3 fim = T3.init) {
	int start, end, startlen;
	static if (is(T2 == int)) {inicio = inicio < 0 ? conteudo.length + inicio : inicio;}
	static if (is(T3 == int)) {fim = fim <= 0 ? conteudo.length + fim : fim;}
	static if (is(T2 == int)) {start = inicio;} else static if (is(T2 == string)){start = conteudo.countUntil(inicio);}
	static if (is(T2 == string)) {static if (is(T1 == string)){startlen = start + inicio.length + 1;} else {startlen = start + 1;}}
	static if (is(T3 == int)) {end = fim;} else static if (is(T3 == string)){end = startlen + conteudo[startlen..$].countUntil(fim);}
	static if (is(T3 == None)) {return conteudo[start];} else {return conteudo[start..end];}
}