Pointer to dlang spec for this alias construct?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Mon Jun 17 05:05:06 UTC 2024
On Sunday, June 16, 2024 10:32:50 PM MDT Andy Valencia via Digitalmars-d-learn
wrote:
> In the alias:
>
> alias Unshared(T) = T;
> alias Unshared(T: shared U, U) = U;
>
> as used in:
>
> cast(Unshared!mytype)value
>
> turns a mytype with shared attribute into one without shared.
>
> I deduce the alias is using some sort of type matching and
> decomposition?
>
> I've read through the language spec, and didn't spot this
> mechanism. Can somebody tell me what section of the spec covers
> this very interesting mechanism?
>
> Thanks in advance,
> Andy
Unshared is an eponymous template.
https://dlang.org/spec/template.html#implicit_template_properties
And it's using a shortcut syntax.
alias Unshared(T) = T;
alias Unshared(T: shared U, U) = U;
is equivalent to
template Unshared(T)
{
alias Unshared = T;
}
template Unshared(T : shared U, U)
{
alias Unshared = U;
}
The alias is simply a normal alias that's the result of evaluating the
template.
https://dlang.org/spec/declaration.html#alias
The second template uses a template specialization
https://dlang.org/spec/template.html#parameters_specialization
that matches based on the fact that the given type implicitly converts to
shared, and the shared U part separates out shared from the type so that you
can get the type without shared. That's then used for the alias to get the
type without shared. The way that the shared U part works comes from is
expressions:
https://dlang.org/spec/expression.html#is_expression
Eponymous templates get used in a variety of circumstances, not just with
aliases. For instance, they can be used with enums, e.g.
enum sizeOf(T) = T.sizeof;
which would be equivalent to
template sizeOf(T)
{
enum sizeOf = T.sizeof;
}
and they're used with function templates, e.g.
T identity(T)(T value)
{
return value;
}
is equivalant to
template identity(T)
{
T identity(T value)
{
return value;
}
}
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list