User loginNavigation |
Type classes in a dynamic languageHello, So another attempt. First of all it is quite different from regular type classes as soon as it relies on a dynamic, not static dispatch. Also it is just single dispatch for now (which somewhat logical as soon as all functions are curried). The preview version (link below) contains three new entities. The first is a 'type' entity that creates a user type: type user x = User x This declaration is basically a function (and is first class therefore). The right hand can contain any expression. Here a variant (aka polymorphic variant) tag is used which simply "wraps" a given value. Variant is helpful as soon as it can be used to "unwrap" the value like so: let (User x) = ... The second is a 'class' entity, which is defined like so: class Foo foo bar
(This syntax is probably a bit clumsy). Having a class and a type one can create an instance of a class for a type like so: instance Foo user where foo (User x) = "Foo:" ++ show x et bar (User x) = "Bar:" ++ show x An instance adds these functions to the previously defined vocabularies. These vocabularies are basically mutable objects however all instances are always executed before any user code. When implementing an instance it is required to provide an implementation for all functions in a class. It is not possible to override an existing instance (it is not a problem technically however I am not sure that this is a good feature). And of course it is possible to define instances for built-in types (such as int, double, string, list, etc.). "Class functions" also can't be shadowed by other bindings (Ela does support shadowing for regular bindings). What do you think about such an approach? There is a preview for this implementation. It only comes with prelude that defines several classes/instances for built-ins and other basic functions (such as composition, application operators, etc.) and REPL. Requires Mono 2.6+/.NET 2.0+, 1MB: BTW. There's a short language reference and a side-by-side comparison with Haskell if you're interested: By vorov2 at 2012-06-13 20:54 | LtU Forum | previous forum topic | next forum topic | other blogs | 9582 reads
|
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 19 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago