Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Existential type at declaration site

I was playing around with Scala when I found that this compiles:

class Foo[_]

What does an existential type in a class declaration do?

like image 763
drhagen Avatar asked Aug 27 '26 10:08

drhagen


1 Answers

This is legal because of following part of the grammar (given in the Scala specification):

TmplDef ::= ‘class’ ClassDef
ClassDef ::= id [TypeParamClause] {Annotation}
  [AccessModifier] ClassParamClauses ClassTemplateOpt
TypeParamClause ::= ‘[’ VariantTypeParam {‘,’ VariantTypeParam} ‘]’
VariantTypeParam ::= {Annotation} [‘+’ | ‘-’] TypeParam
TypeParam ::= (id | ‘_’) [TypeParamClause] [‘>:’ Type] [‘<:’ Type] [‘:’ Type]

I believe _ simply ends up being a type parameter name (which isn't actually usable in the class body), not part of existential type syntax.

like image 159
Alexey Romanov Avatar answered Aug 29 '26 22:08

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!