Re: Default arguments

2023-02-07 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 7 February 2023 at 09:49:46 UTC, Salih Dincer wrote: Is it a feature or a bug that the code below can be compiled without arguments? You should use `@disable this()` recommended in the relevant article: https://dlang.org/spec/struct.html#disable_default_construction ```d void ma

Default arguments

2023-02-07 Thread Salih Dincer via Digitalmars-d-learn
Hi All... Is it a feature or a bug that the code below can be compiled without arguments? ```d import std.stdio, std.conv : to; void main() { auto noArgument = Sarr!char(); // it works no argument... assert(noArgument.length == 8); string dlang = "D-lang"; const len = dlang.le

Re: default arguments for const ref parameters in extern C++ functions

2020-09-14 Thread Mathias LANG via Digitalmars-d-learn
On Monday, 14 September 2020 at 18:58:44 UTC, 60rntogo wrote: On Monday, 14 September 2020 at 17:11:59 UTC, k2aj wrote: AFAIK the only way to have default ref arguments is to use a global variable: --- extern(C++) struct Foo { int x; } immutable foo1 = Foo(1); extern(C++) void fun(const ref F

Re: default arguments for const ref parameters in extern C++ functions

2020-09-14 Thread 60rntogo via Digitalmars-d-learn
On Monday, 14 September 2020 at 17:11:59 UTC, k2aj wrote: AFAIK the only way to have default ref arguments is to use a global variable: --- extern(C++) struct Foo { int x; } immutable foo1 = Foo(1); extern(C++) void fun(const ref Foo foo = foo1); --- Thanks. This appears to work, but feels l

Re: default arguments for const ref parameters in extern C++ functions

2020-09-14 Thread k2aj via Digitalmars-d-learn
On Monday, 14 September 2020 at 12:44:34 UTC, 60rntogo wrote: I'm trying to use a C++ library that has a function declared like this: --- struct Foo { int x; }; void fun(const Foo& foo = Foo(1)); --- I have translated this to a D declaration: --- struct Foo { int x; } extern(C++) void f

Re: default arguments for const ref parameters in extern C++ functions

2020-09-14 Thread 60rntogo via Digitalmars-d-learn
On Monday, 14 September 2020 at 12:44:34 UTC, 60rntogo wrote: --- struct Foo { int x; } extern(C++) void fun(const ref Foo foo = Foo(1)); --- I suppose this should have been: --- extern(C++): struct Foo { int x; } void fun(const ref Foo foo = Foo(1)); --- Not that it changes the questi

default arguments for const ref parameters in extern C++ functions

2020-09-14 Thread 60rntogo via Digitalmars-d-learn
I'm trying to use a C++ library that has a function declared like this: --- struct Foo { int x; }; void fun(const Foo& foo = Foo(1)); --- I have translated this to a D declaration: --- struct Foo { int x; } extern(C++) void fun(const ref Foo foo = Foo(1)); --- This yields an error: "Foo

Re: Varargs and default arguments

2015-10-07 Thread anonymous via Digitalmars-d-learn
On Wednesday 07 October 2015 02:22, Steven Schveighoffer wrote: > On 10/6/15 4:27 PM, anonymous wrote: [...] >> void foo(T...)(string str=null, T args = T.init) { [...] > I find it quite fascinating that in anonymous' solution, the T.init > doesn't ever actually get used! It's not used with IFTI

Re: Varargs and default arguments

2015-10-06 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/6/15 4:27 PM, anonymous wrote: You can put an expression tuple ("expression AliasSeq"??) there. T.init is one that always fits T's types. But you could generate one with different values, too. void foo(T...)(string str=null, T args = T.init) { //... } void main() { fo

Re: Varargs and default arguments

2015-10-06 Thread anonymous via Digitalmars-d-learn
On Tuesday 06 October 2015 22:01, Nick Sabalausky wrote: > Ok, D-style varargs can accept a parameter length of zero: > > --- > void foo(T...)(T args) { > //... > } > foo(); > foo(t1, t2); > --- Terminology fun: The spec uses the term "D-style variadic function" for

Varargs and default arguments

2015-10-06 Thread Nick Sabalausky via Digitalmars-d-learn
Ok, D-style varargs can accept a parameter length of zero: --- void foo(T...)(T args) { //... } foo(); foo(t1, t2); --- Is there any way to stick a param with a default value before that? --- void foo(T...)(string str=null, T args=/+what goes here???+/) {

Re: Purity not enforced for default arguments?

2015-03-10 Thread Xinok via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 22:00:29 UTC, safety0ff wrote: On Tuesday, 10 March 2015 at 21:56:39 UTC, Xinok wrote: I'm inclined to believe this is a bug. https://issues.dlang.org/show_bug.cgi?id=11048 Thanks for the link, and I didn't mean to post this in D.learn. >.<

Re: Purity not enforced for default arguments?

2015-03-10 Thread safety0ff via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 21:56:39 UTC, Xinok wrote: I'm inclined to believe this is a bug. https://issues.dlang.org/show_bug.cgi?id=11048

Purity not enforced for default arguments?

2015-03-10 Thread Xinok via Digitalmars-d-learn
The following code fails to compile because unpredictableSeed is impure: void main() { foreach(i; 0..10) writeln(pureRandom); } pure uint pureRandom() { auto r = Random(unpredictableSeed); return r.front; } However, make unpredictableSeed a defau

Re: Template function type inference with default arguments

2015-01-05 Thread anonymous via Digitalmars-d-learn
On Sunday, 4 January 2015 at 00:22:01 UTC, ixid wrote: Why don't templates take a type from the default argument if nothing else is supplied? https://issues.dlang.org/show_bug.cgi?id=2803

Re: Template function type inference with default arguments

2015-01-03 Thread Meta via Digitalmars-d-learn
On Sunday, 4 January 2015 at 00:22:01 UTC, ixid wrote: Why don't templates take a type from the default argument if nothing else is supplied? It would be useful to be able to use an enum to set a default. I doubt anyone's ever thought of that particular use-case. Using your typeof(MAX) workar

Template function type inference with default arguments

2015-01-03 Thread ixid via Digitalmars-d-learn
Why don't templates take a type from the default argument if nothing else is supplied? It would be useful to be able to use an enum to set a default. enum MAX = 1_000; auto sieve(T)(T max = MAX) { import std.bitmanip : BitArray; BitArray n; n.length = max; T[] p

Re: Templates, constructors and default arguments

2014-12-24 Thread ketmar via Digitalmars-d-learn
On Thu, 25 Dec 2014 03:07:55 + aldanor via Digitalmars-d-learn wrote: > On Thursday, 25 December 2014 at 02:28:47 UTC, ketmar via > Digitalmars-d-learn wrote: > > happy hacking! ;-) > > Thanks once again! I think this mostly solves it. Would it be > possible to somehow do the same trick wi

Re: Templates, constructors and default arguments

2014-12-24 Thread aldanor via Digitalmars-d-learn
On Thursday, 25 December 2014 at 02:28:47 UTC, ketmar via Digitalmars-d-learn wrote: happy hacking! ;-) Thanks once again! I think this mostly solves it. Would it be possible to somehow do the same trick with this()? (I guess due to having to write Type!() when default template arguments are

Re: Templates, constructors and default arguments

2014-12-24 Thread ketmar via Digitalmars-d-learn
On Thu, 25 Dec 2014 02:07:51 + aldanor via Digitalmars-d-learn wrote: > I'm wondering how to best implement the following pattern: the > constructor of a class has some required and some optional > arguments; and one of the (optional) arguments also controls if > any additional arguments s

Templates, constructors and default arguments

2014-12-24 Thread aldanor via Digitalmars-d-learn
I'm wondering how to best implement the following pattern: the constructor of a class has some required and some optional arguments; and one of the (optional) arguments also controls if any additional arguments should be passed. A hypothetical/simplified example that I came up with: there's a

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Simen Kjaeraas
because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then t

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Jacob Carlborg
Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. I've wondered the same thing, why this doesn't work: template Foo (T = int) {} mixin Foo; But this works: t

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Steven Schveighoffer
#x27;t have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. Perhaps a different approach: struct PointT(T) {...} alias PointT!(double) Point; // and if so desir

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Jonathan M Davis
and only in rare cases Point with ints. So to simplify code > one doesn't have to write Point!double in all of their code, but > simply Point. > > If the bang syntax wasn't required in presence of default arguments > then these workarounds wouldn't be needed. There

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Christophe
#x27;re not both a template Point() that takes a type argument >> is because in most cases the user will use the Point structure with >> doubles, and only in rare cases Point with ints. So to simplify code >> one doesn't have to write Point!double in all of their code, but

Re: Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Simen Kjaeraas
#x27;t have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. How would you then pass a single-argument template as a template alias parameter? Example: temp

Why can't templates with default arguments be instantiated without the bang syntax?

2011-09-15 Thread Andrej Mitrovic
Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed.