Closed m1sta closed 10 years ago
@
isn't a type annotation. Tag reflection is done by means of using a class (uppercase initial letter) without a destructuring. So String
is the pattern that checks if a given parameter is a string. @
is used to create a named pattern binder. foo @ String
is the pattern that checks if a value is a String and binds it to foo
. Likewise you could use foo @ [1, 2, 3]
as a pattern for a three element array containing 1, 2 and 3, which is bound to foo
. Types and annotations are completely unrelated.
Understood, although there does seem to be some similarly to the 'interfaces' concept in Typescript right?.
Interfaces exist at compile time, and have no runtime overhead. Pattern matching is dynamic inspection of the structure of some parameter. So in some sense, sure, in sparkler you could implement an ad-hoc, dynamic interface.
function foo {
x @ { toString: Function } => x.toString()
}
This dynamically verifies at runtime that a parameter has a property called toString
that's also a function. It doesn't tell you anything else though, like what parameters you should give it or if it actually returns a string. Maybe it takes 3 arguments? Maybe it returns a number?
Whereas:
interface ToString {
toString(): String;
}
function callToString(foo: ToString) {
return foo.toString();
}
Is a compile-time construct that guarantees something will actually give me a string when I call toString
. There's no need for runtime validation that something actually implements this interface, because we would have declared that fact statically, and its adherence would be enforced by the type system.
I notice your
@ String
format for type annotations in patterns. Have you used Typescript very much? It's got a great style of type annotations and 'interfaces'.