www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to dlang spec for this alias construct?

reply Andy Valencia <dont spam.me> writes:
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
Jun 16
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Monday, 17 June 2024 at 04:32:50 UTC, Andy Valencia 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
not sure if it works the same with template arguments but here is the rules https://dlang.org/spec/expression.html#IsExpression
Jun 16
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Jun 16
parent Andy Valencia <dont spam.me> writes:
On Monday, 17 June 2024 at 05:05:06 UTC, Jonathan M Davis wrote:
      alias Unshared(T) = T;
      alias Unshared(T: shared U, U) = U;
... Unshared is an eponymous template. https://dlang.org/spec/template.html#implicit_template_properties And it's using a shortcut syntax. ... The second template uses a template specialization https://dlang.org/spec/template.html#parameters_specialization
No wonder I couldn't find it in the spec; I was looking for an "alias" language feature. alias as used here leans on a template mechanism--specialization. It might have taken many readings of the spec to have hunted that down, so--thank you. It's quite an interesting mechanism. D is quite easy for a C programmer to get started with, but it certainly has its depths! Andy
Jun 17