by dangerlibrary · 6y ago · 0 comments
Fingers crossed Java never implements the `implicit` keyword...
[1] - http://learnyouahaskell.com/making-our-own-types-and-typecla...
I'm sure that there's some kind of purity to them, but the fact that a two records can't share field names ends up making them incredibly difficult to use for anything practical. Granted, using the makeFields function with Lenses allows you to kind of avoid the clashing, but that in itself feels like somewhat of a hack.
They're essentially just syntactic sugar for a standard product type with auto-generated functions for access and update. Unfortunately those functions come with foot guns. Here's two:
1. As you pointed out, clashing field names (since fields don't truly exist but are rather functions)
2. Partiality of the generated functions if you have multiple constructors for a type, at least one of which is a record.
Lenses are great, but would benefit from first-class integration as the "blessed" way of interacting with records (this hopefully would also reduce their learning curve).
One of my top wishes for Haskell is proper row types a la Purescript. Purescript records and polymorphic variants are amazing. I would absolutely love if this https://github.com/natefaubion/purescript-checked-exceptions was possible in Haskell.
For example in OO languages `obj.func(1, 2)` will typically require the compiler to know the type of obj before knowing which func method is being referred to here.
In Haskell however the equivalent expression is `func obj 1 2`. The compiler has already performed name resolution and knows exactly what func is. So that when doing type-checking, it is not only possible for typing information to propagate from object to method, but also from function to object. That is to say, it is possible that the compiler initially knows what type func has, but has no idea what type obj has; it will only do so after type inference using the type of func.
This makes type inference more general and more elegant. I suspect it also makes the implementation of name resolution easier. Unfortunately as you have pointed out, it's practically more of a nuisance.
Also note that this applies to more than just names of fields associated with a type; it even applies to type names associated with a type. For example if you have
class G a where
data N a :: *
The name N is directly usable without even mentioning G. This would require, say in C++, you to write `G::N` to refer to an inner class: class G {
public:
class N;
};
The reasoning is of course the same.That is awkward sometimes yes. Record fields are functions, and you can't have two functions with the same name in the same namespace.
You can, however, use namespaces to import two separate modules with conflicting names. It does mean you have to define the two different records in two different modules.
If, you want different records to have common fields because they are conceptually related, you probably want to use type classes.
My favorite usage is probably in a typesafe Query engine that several people[1] built at Foursquare that is a lot better than what you can find in the java world.
Venue.where(_.closed eqs false)
.orderAsc(_.popularity)
.limit(10)
.modify(_.closed setTo true)
.updateMulti
It allows for all sorts of typesafe queries that will fail at compile time if things are malformed including stuff like failing to query on an index.https://github.com/foursquare/rogue
1. Hi Jorge, Liszka & Neil!
The best case scenario is that they aren't instantly horrified / confused / repulsed.
implicit is one of them
More often than not Java programming just consists of throwing gang signs on your keyboard for your Ide to create tons of boilerplate.
Well case classes can implement traits, might have a companion object and can (poorly) replace enums.
Scala sometimes seems to not need standard classes.
Other languages like go, rust or c can do just fine without classes at all.
There will be new syntax, but otherwise the underlying semantics will remain the same; it's just the implicit keyword itself and the kitchen sink of functionality that it provides in Scala 2 will be split up into new (soft) keywords like "given foo(using: Bar) = ..." rather than "implicit def foo(implicit bar: Bar) = ...", along with "extension listOps of" replacing "implicit class ListOps ...".
The community isn't united over the proposed changes (mostly due to migration concerns it seems), but overall it looks like a nice improvement over the status quo, particularly around typeclass definitions.