Skip to content

Allow the construction of self-referential Refs #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 8, 2020
Merged

Conversation

garyb
Copy link
Member

@garyb garyb commented Feb 14, 2019

No description provided.

@@ -13,7 +13,12 @@ import Effect (Effect)
foreign import data Ref :: Type -> Type

-- | Create a new mutable reference containing the specified value.
foreign import new :: forall s. s -> Effect (Ref s)
new :: forall s. s -> Effect (Ref s)
new s = self (const s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like that new requires an unnecessary closure allocation with const on top of the ref allocation and then Effect thunk allocation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess the whole thing is FFI already so one more function won't do any harm.

@JordanMartinez
Copy link
Contributor

What's the status of this PR?

@garyb
Copy link
Member Author

garyb commented Oct 13, 2020

Is "forgotten entirely about" a valid status? 😄

It sounds like the review concern was addressed, but there's no explicit approval.

@hdgarrood
Copy link
Contributor

This looks good to me, although I think I’d prefer the function was called newSelf too.

@JordanMartinez
Copy link
Contributor

newSelf

Same. @garyb How quickly would you be able to update this PR? Or should someone else submit a PR to your branch to get the name changed?

@garyb
Copy link
Member Author

garyb commented Oct 14, 2020

Done! I wonder if there's still a better name though, as newSelf is a bit weird. Something like knot or newKnot perhaps.

@JordanMartinez
Copy link
Contributor

I wonder if there's still a better name though, as newSelf is a bit weird. Something like knot or newKnot perhaps.

Why? What about knot or newKnot makes you think it's better?

@JordanMartinez
Copy link
Contributor

Looks like CI is failing because the tests weren't updated:

* Building project in /home/travis/build/purescript/purescript-refs

[1/1 UnknownName] test/Main.purs:39:10

  39    ref <- Ref.self \ref -> RefBox { ref, value: 0 }

               ^^^^^^^^

  

  Unknown value Ref.self

@hdgarrood
Copy link
Contributor

People use the phrase “tying the knot” to mean constructing a structure which references itself: https://wiki.haskell.org/Tying_the_Knot

@JordanMartinez
Copy link
Contributor

Let's assume you come to a language and codebase you've never seen before and you only see this: ref.newSelf and later ref.newKnot.
By just looking at those isolated instances, what immediately comes to mind?

IMHO, I think the newSelf makes me think more of what this PR is doing. The newKnot would require me to know about the "Tying the Knot" phrase and I would need to connect that concept with this usage rather than assume that knot is some domain concept and I'm creating a new one.

@natefaubion
Copy link

newWithSelf?

@JordanMartinez
Copy link
Contributor

Sure.

@garyb
Copy link
Member Author

garyb commented Oct 15, 2020

Renamed again, and updated the tests this time!

@JordanMartinez re: people coming to the library looking for this function, realistically I'm not sure how often that will happen. I needed to do this in Halogen, and used an unsafeCoerce hack to make it work, but outside of that case I can't think of another occasion I've needed it. In Halogen it's in a situation where something is stored in an existential form, but internally it needs to be able to do things with its full set of type parameters, so... pretty niche.

Maybe there are some other uses cases for it though, I dunno. I've definitely used self referential AVars more often. But they don't have this problem since they can be created as empty-but-not-Maybe due to their nature, unlike Ref.

I think providing a way to do it in the library rather than having to do an ad-hoc unsafeCoerce hack for the rare cases it's needed is still worthwhile, even if it is rare, since it's not possible to misuse this the way I theoretically could in Halogen.

@JordanMartinez JordanMartinez merged commit 1a1b766 into master Dec 8, 2020
@JordanMartinez JordanMartinez deleted the self-ref branch December 8, 2020 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants