specify Show type class
See original GitHub issueI would ❤️ a Haskell-like Show type class. Useful functions such as R.toString
, S.toString
, and Z.toString
rely on toString
methods, but there’s a problem with this approach:
// Person :: (String, String, String) -> Person
function Person(firstName, lastName, emailAddress) {
if (!(this instanceof Person)) return new Person(firstName, lastName, emailAddress);
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
}
// Person#toString :: Person ~> () -> String
Person.prototype.toString = function() {
return '"' + this.firstName + ' ' + this.lastName + '" <' + this.emailAddress + '>';
};
In this case, Person#toString
serves some other useful role.
It would be nice to be able to define this as well:
// Person#fantasy-land/show :: Person ~> () -> String
Person.prototype['fantasy-land/show'] = function() {
return 'Person(' + Z.show(this.firstName) + ', '
+ Z.show(this.lastName) + ', '
+ Z.show(this.emailAddress) + ')';
};
With a suitable definition for Person#fantasy-land/equals
, one could then write:
> var puffnfresh = Person('Brian', 'McKenna', 'brian@brianmckenna.org')
> Z.equals(eval(Z.show(puffnfresh)), puffnfresh)
true
I’ve found myself giving a function a Showable a
constraint from time to time, but this is currently almost meaningless as the presence of a toString
method does not imply eval(show(x)) = x
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
A Gentle Introduction to Haskell: Standard Classes
The instances of class Show are those types that can be converted to character strings (typically for I/O). The class Read provides operations...
Read more >Haskell Show type class in a function
The compiler typechecks your code, resolves any polymorphism, and then erases the types. But, this is kind of orthogonal to your main question....
Read more >A Tutorial on Typeclasses in Coq
The Show typeclass can be thought of as "classifying" types whose values can be converted to strings -- that is, types A such...
Read more >Chapter 6. Using Typeclasses - Real World Haskell
[Note], The Show typeclass. Show is usually used to define a String representation for data that is useful for a machine to parse...
Read more >Lesson 14. Using type classes - Get Programming with Haskell
Perhaps the most important type class to implement is Show , because you'll nearly always want to have an easy way to display...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@davidchambers
Thank you for explaining. If I understand correctly, actually using the output with
eval
is not the primary goal. It’s just a guideline for implementing theshow
method. I can definitely see how such a method can be useful 😄 . It’s purpose does lies very close to thetoString
method though.I would argue that what Fantasy Land specifies is not type classes but interfaces. It specifies methods on objects. That is exactly what many languages calls “interfaces”. Type classes, on the other hand, are something else. It may seem like nitpicking, but I think it’s an important distinction to make. Type classes and interfaces are different in ways that make a difference for several of the Fantasy Land abstractions. At leas monoids and applicatives. Therefore I think that calling what Fantasy Land defines “type classes” is misleading. “Interfaces” fit the bill.
Yes, I think so. I think the scope of Fantasy Land is to define interfaces for algebraic structures. That is also what the readme seems to imply. There are practically an unlimited number of possible interfaces so putting the line somewhere is nececarry.
Nothing would change in this respect. We’d simply move
Array$prototype$toString
and friends to the new package and replacetoString
withshow
.I now realize the issue description is misleading. I should have made it clear that
eval(show(x)) = x
is a guide for producing useful string representations for logging and for REPLs. It is not intended to be used for serialization (though it may occasionally be suitable for this purpose).