Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 29, 2020 How make Optional pre determined parameter type without overload function? | ||||
---|---|---|---|---|
| ||||
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 Re: How make Optional pre determined parameter type without overload function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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 Re: How make Optional pre determined parameter type without overload function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 Re: How make Optional pre determined parameter type without overload function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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];} } |
Copyright © 1999-2021 by the D Language Foundation