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.

Pass prototype object to tagged and taggedSum

See original GitHub issue

It would be nice to simply pass in an object as the final parameter.

const Point3D = daggy.tagged('Point3D', ['x', 'y', 'z'], {
  scale(n) {
    return Point3D(this.x * n, this.y * n, this.z * n)
  }
})
Point3D.toString() // 'Point3D'
const a = Point3D(1, 2, 3) // { x: 1, y: 2, z: 3 }
a.x == 1 && a.y == 2 && a.z == 3 // true
a.toString() // 'Point3D(1, 2, 3)'
Point3D.is(a) // true
const b = a.scale(2) // { x: 2, y: 4, z: 6 }
b.toString() // 'Point3D(2, 4, 6)'
const Option = daggy.taggedSum('Option', {
  Some: ['x'],
  None: [],
}, {
  map(f) {
    return this.cata({
      Some: (x) => Option.Some(f(x)),
      None: () => this,
    })
  }
})
const a = Option.Some(1) // { x: 1 }
a.toString() // 'Option.Some(1)'
Option.Some.is(a) // true
Option.is(a) // true
Option.None.is(Option.None) // true
Option.is(Option.None) // true
Option.None.toString() // 'Option.None'
Option.Some.toString() // 'Option.Some'
const b = a.map(x => x+1) // { x: 2 }
b.toString() // 'Option.Some(2)'

It would allow customizing the behavior of toString.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gabejohnsoncommented, May 14, 2017

The problem it’s solving is simply reduced boilerplate. I think things could be disambiguated.

const Option = daggy.taggedSum('Option', {
  of(a) {
    return Option.Some(a);
  }
}, {
  Some: [['x'], {}, {
    map(f) {
      return Option.Some(f(x));
    }
  }],
  None: [[], {}, {
    map() {
      return this;
    }
  }],
});

But the added complexity would probably outweigh the benefits.

0reactions
safarelicommented, May 26, 2017

closing this for now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fantas, Eel, and Specification 1: Daggy - Tom Harding
A tiny little utility for creating constructors with named properties. daggy.taggedSum(typeName, constructors). Now for the interesting one.
Read more >
How to use the daggy.taggedSum function in daggy - Snyk
To help you get started, we've selected a few daggy.taggedSum examples, based on popular ways it is used in public projects.
Read more >
Passing a prototype to Object.Create() - Stack Overflow
I understand that whatever you pass into it returns a new object with that argument as the prototype. My teacher listed this as...
Read more >
Extensible Effects in Node.js, Part 1 - HumbleSpark
A Monad is an object with a chain method and an of static method that ... of options that held on to values...
Read more >
Daggy: Library for Creating Tagged Constructors - Morioh
TumblrRestClient is the object you'll make all of your calls to the Tumblr ... A note on tags: When passing tags, as params,...
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