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 template constrant to enforce that a
> >
> > type can be explicitly cast to another type:
> >     void (T)(T t)
> >
> >             if (cast(int) T)//force `cast(int) T` to be possible
> >
> >         {
> >
> >             // Yay I know `t` can be cast to an `int`!
> >
> >     }
> >
> > Is this possible?
>
> I would have thought contracts would be ideal here?
>
> https://dlang.org/spec/contracts.html

Contracts are used to assert runtime state, whereas template constraints
control which arguments can be used with the template (including being used
for function overloading).

The OP wants his function template to reject any arguments that can't be
explicitly cast to int, whereas an in contract would be used for something
like verifying at runtime that the argument was within a particular range of
values.

- Jonathan M Davis

Reply via email to