question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

specify Show type class

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
paldepindcommented, Jul 14, 2017

@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 the show method. I can definitely see how such a method can be useful 😄 . It’s purpose does lies very close to the toString method though.

It’s currently the place for defining type classes.

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.

I suppose we could specify sanctuary/show in sanctuary-type-classes. Do you think this would be more natural?

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.

1reaction
davidchamberscommented, Jul 17, 2017

I get a bit worried at 4 and 5. Will S.show continue to provide sane @@show implementations for primitives and built-ins?

Nothing would change in this respect. We’d simply move Array$prototype$toString and friends to the new package and replace toString with show.

I like @paldepind’s idea with serialize/deserialize.

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found