Re: Template Constraints

2018-02-24 Thread Jonathan via Digitalmars-d-learn
On Saturday, 24 February 2018 at 03:04:07 UTC, Adam D. Ruppe wrote: On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. The constraint is just like static if as to what it allows

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 04:33:52 psychoticRabbit via Digitalmars-d- learn wrote: > On Saturday, 24 February 2018 at 04:22:12 UTC, Jonathan M Davis > > wrote: > > Why is there anything dodgy going on and why would you need > > contracts? Contracts actually tend to go very badly with > >

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 04:22:12 UTC, Jonathan M Davis wrote: Why is there anything dodgy going on and why would you need contracts? Contracts actually tend to go very badly with generic code, because whatever they assert has to be generic, and while that works sometimes, more often

Re: Template Constraints

2018-02-23 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Feb 24, 2018 at 02:54:13AM +, Jonathan via Digitalmars-d-learn wrote: > I am having trouble finding many useful explanations of using template > constraints beyond basic usage. > > I would like to have a template constrant to enforce that a type can > be explicitly cast to another

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 04:13:30 psychoticRabbit via Digitalmars-d- learn wrote: > On Saturday, 24 February 2018 at 03:58:48 UTC, Jonathan M Davis > > wrote: > > Whether an implicit cast or an explicit cast makes more sense > > depends entirely on what the code is doing, but either way, the

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 03:58:48 UTC, Jonathan M Davis wrote: Whether an implicit cast or an explicit cast makes more sense depends entirely on what the code is doing, but either way, the conversion needs to be forced inside the function, or you end up with bugs. Far too often, when

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 03:48:44 psychoticRabbit via Digitalmars-d- learn wrote: > On Saturday, 24 February 2018 at 03:43:25 UTC, Jonathan M Davis > > wrote: > > That does not do what the OP requested at all. That tests > > whether T is one of byte, ubyte, short, ushort, int, uint, > >

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 03:43:25 UTC, Jonathan M Davis wrote: That does not do what the OP requested at all. That tests whether T is one of byte, ubyte, short, ushort, int, uint, long, and ulong, whereas what the OP wants is to test whether T can be cast to int. - Jonathan M Davis

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 03:30:45 UTC, psychoticRabbit wrote: On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. I would like to have a template constrant to enforce that

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 03:30:45 psychoticRabbit via Digitalmars-d- learn wrote: > On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: > > I am having trouble finding many useful explanations of using > > template constraints beyond basic usage. > > > > I would like to have a

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. I would like to have a template constrant to enforce that a type can be explicitly cast to another type: void (T)(T t)

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 03:04:53 psychoticRabbit via Digitalmars-d- learn wrote: > On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: > > I am having trouble finding many useful explanations of using > > template constraints beyond basic usage. > > > > I would like to have a

Re: Template Constraints

2018-02-23 Thread psychoticRabbit via Digitalmars-d-learn
On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. I would like to have a template constrant to enforce that a type can be explicitly cast to another type: void (T)(T t)

Re: Template Constraints

2018-02-23 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. The constraint is just like static if as to what it allows inside, so you can check almost anything in there. Like for the

Re: Template Constraints

2018-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 24, 2018 02:54:13 Jonathan via Digitalmars-d-learn wrote: > I am having trouble finding many useful explanations of using > template constraints beyond basic usage. > > I would like to have a template constrant to enforce that a type > can be explicitly cast to another type:

Re: Template constraints for reference/value types?

2016-09-06 Thread Jon Degenhardt via Digitalmars-d-learn
On Wednesday, 7 September 2016 at 00:40:27 UTC, Jonathan M Davis wrote: On Tuesday, September 06, 2016 21:16:05 Jon Degenhardt via Digitalmars-d-learn wrote: On Tuesday, 6 September 2016 at 21:00:53 UTC, Lodovico Giaretta wrote: > On Tuesday, 6 September 2016 at 20:46:54 UTC, Jon Degenhardt >

Re: Template constraints for reference/value types?

2016-09-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, September 06, 2016 21:16:05 Jon Degenhardt via Digitalmars-d-learn wrote: > On Tuesday, 6 September 2016 at 21:00:53 UTC, Lodovico Giaretta > > wrote: > > On Tuesday, 6 September 2016 at 20:46:54 UTC, Jon Degenhardt > > > > wrote: > >> Is there a way to constrain template arguments to

Re: Template constraints for reference/value types?

2016-09-06 Thread Jon Degenhardt via Digitalmars-d-learn
On Tuesday, 6 September 2016 at 21:00:53 UTC, Lodovico Giaretta wrote: On Tuesday, 6 September 2016 at 20:46:54 UTC, Jon Degenhardt wrote: Is there a way to constrain template arguments to reference or value types? I'd like to do something like: T foo(T)(T x) if (isReferenceType!T) { ...

Re: Template constraints for reference/value types?

2016-09-06 Thread Lodovico Giaretta via Digitalmars-d-learn
On Tuesday, 6 September 2016 at 20:46:54 UTC, Jon Degenhardt wrote: Is there a way to constrain template arguments to reference or value types? I'd like to do something like: T foo(T)(T x) if (isReferenceType!T) { ... } --Jon You can use `if(is(T : class) || is(T : interface))`. If you

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
fails to compile for both integers and my defined types. I read the D Templates: A Tutorial book and as far as I can tell ++B.init and B.init E.init doesn't look too much wrong, but I've not seen any constraint of this kind in phobos (using variables instead of types) so I was wondering if

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 15:38:56 UTC, Francesco Cattoglio wrote: I'm trying to experiment a bit around the iota function. If I try to impose the following constraits: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot cleaner.

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Francesco Cattoglio
On Friday, 20 December 2013 at 16:40:23 UTC, monarch_dodra wrote: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in constraints. That's news to me. It seems strange to me too, but: page 69 on the PDF: Do not use argument in your

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Francesco Cattoglio
On Friday, 20 December 2013 at 17:18:01 UTC, Timon Gehr wrote: On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I find the latter cleaner: its intent is more apparent. I use is(typeof()) only for really testing for type

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
On Fri, Dec 20, 2013 at 6:31 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: On Friday, 20 December 2013 at 16:40:23 UTC, monarch_dodra wrote: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in constraints. That's news

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 17:48:03 UTC, Philippe Sigaud wrote: On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I find the latter cleaner: its intent is more

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 17:18:01 UTC, Timon Gehr wrote: On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 09:42 PM, monarch_dodra wrote: On Friday, 20 December 2013 at 17:48:03 UTC, Philippe Sigaud wrote: On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 10:57 PM, Timon Gehr wrote: Most non-trivial templates that use is(typeof(...)) in the constraint can be broken. (In the sense that it is possible to instantiate them even though their body does not compile.) Actually, it seems that the behaviour of DMD has changed in this

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Jakob Ovrum
On Friday, 20 December 2013 at 20:42:13 UTC, monarch_dodra wrote: AFAIK, there is no real difference, but is(typeof()) is more idiomatic in phobos. Most uses of `is(typeof())` were written before the `compiles` trait was introduced. Add the cargo cult to that, and it's no wonder that Phobos

Re: Template constraints and opAdd

2013-07-02 Thread bearophile
John: Mass!(T,S) opAdd(Mass!(T,S) other) { If you are using D2 then don't use opAdd, use opBinary: http://dlang.org/operatoroverloading.html#Binary Time ago I added an enhancement request for a warning (that later is meant to become a deprecation) that helps avoid your problem:

Re: Template constraints and opAdd

2013-07-01 Thread bearophile
John: Mass!(T,S) opAdd(Mass!(T,S) other) { If you are using D2 then don't use opAdd, use opBinary: http://dlang.org/operatoroverloading.html#Binary Bye, bearophile

Re: Template constraints and opAdd

2013-07-01 Thread John
On Tuesday, 2 July 2013 at 00:01:48 UTC, bearophile wrote: John: Mass!(T,S) opAdd(Mass!(T,S) other) { If you are using D2 then don't use opAdd, use opBinary: http://dlang.org/operatoroverloading.html#Binary Bye, bearophile Thanks, I switched over to using the new function and

Re: Template constraints and opAdd

2013-07-01 Thread anonymous
On Monday, 1 July 2013 at 23:36:27 UTC, John wrote: struct Mass(T, S) { ... Mass!(T,S) opAdd(Mass!(T,S) other) { You can't overload non-templates with templates, yet. It's supposed to work, but not implemented. The workaround is simple enough: Mass!(T,S)

Re: Template constraints and opAdd

2013-07-01 Thread bearophile
John: Mass!(T,S) opBinary(alias operator)(Mass!(T,S) other) { Mass!(T,S) opBinary(alias operator, O)(Mass!(O,S) other) if (!is(O == T)) { Isn't operator better as string? -- anonymous: You can't overload non-templates with templates, I think it was recently fixed in

Re: Template constraints and opAdd

2013-07-01 Thread anonymous
On Tuesday, 2 July 2013 at 01:24:18 UTC, bearophile wrote: anonymous: You can't overload non-templates with templates, I think it was recently fixed in Git. It's still buggy then, since adding the empty parentheses makes the error go away.

Re: Template constraints and opAdd

2013-07-01 Thread anonymous
On Tuesday, 2 July 2013 at 01:39:22 UTC, anonymous wrote: On Tuesday, 2 July 2013 at 01:24:18 UTC, bearophile wrote: anonymous: You can't overload non-templates with templates, I think it was recently fixed in Git. It's still buggy then, since adding the empty parentheses makes the error