Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: how to create a type parametrized by a value, instead of a type

Tags:

scala

Is it possible to create, in Scala, a type which is parametrized by a value? Instead of defining a partial type List[A] and parametrize it with List[Int], I'd like to define a partial type (in pseudo-code) Element[Symbol] and parametrize it with Element['td]. In this example, the type represent an XML element: Element['td] informs the Scala type checker that we have a <td> element, and you can imagine that one could have APIs that specifically expect or return <td> elements.

(Subclassing, as inclass Td extends Element, is not a very satisfactory solution, as it doesn't prevent two pieces of code, maybe written by independent developers, from declaring different subclasses for <td>, which will then be considered to be different types by the Scala type checker.)

like image 850
avernet Avatar asked Feb 24 '23 05:02

avernet


1 Answers

If you really want to parameterize a type by a value, you need a dependently typed programming language such as Agda.

like image 180
Kim Stebel Avatar answered Feb 25 '23 23:02

Kim Stebel