by jlrubin · 4y ago · 0 comments
Linear types lets you do things like "this data can only be written to a file once".
It gives the example of a key in a database, which can return multiple types of values:
type DB = (k: Key) => Option[k.Value]
Where the type of "Key" is: trait Key { type Value }
You can declare generic values of type "Key[Value]" and attempting to look them up in the DB can result in more than one kind of type, dependent on the value being looked up.https://docs.scala-lang.org/scala3/book/types-dependent-func...
app : Vect n a -> Vect m a -> Vect (n + m) a
where we're using the value-level addition operator to express the type of the result; specifically, that appending a Vect of length m to a Vect of length n will give a Vect of length (n + m). [1][1] Taken from https://www.idris-lang.org/pages/example.html
interface Key<V> { ... }
class Name extends Key<String> { ... }
class Age extends Key<Integer> { ... }
interface DB {
public <V> Optional<V> get(Key<V> k) { ... }
} Name nameKey = ...;
Age ageKey = ...;
Optional<String> name = db.get(nameKey);
Optional<Integer> age = db.get(ageKey);Scala has only a very limited version of dependent types. Even its "dependent function types" are a more restricted version of general dependent function types. Full dependent types can in a certain sense be even simpler than non-dependently-typed languages (e.g. you no longer need a separate notion of generics).