Skip to content

Add common PropName, AttrName, ClassName #30

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 3 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Web/HTML.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Web.HTML

import Effect (Effect)
import Web.HTML.Window (Window)
import Web.HTML.Common (PropName, AttrName, ClassName) as Exports
import Web.HTML.History (History) as Exports
import Web.HTML.HTMLAnchorElement (HTMLAnchorElement) as Exports
import Web.HTML.HTMLAreaElement (HTMLAreaElement) as Exports
Expand Down
30 changes: 30 additions & 0 deletions src/Web/HTML/Common.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Web.HTML.Common where

import Prelude

import Data.Newtype (class Newtype)

-- | A type-safe wrapper for property names.
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure calling these "type safe" is exactly right, maybe just "semantic"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

-- |
-- | The phantom type `value` describes the type of value which this property
-- | requires.
newtype PropName value = PropName String

derive instance newtypePropName :: Newtype (PropName value) _
derive newtype instance eqPropName :: Eq (PropName value)
derive newtype instance ordPropName :: Ord (PropName value)

-- | A type-safe wrapper for attribute names.
newtype AttrName = AttrName String

derive instance newtypeAttrName :: Newtype AttrName _
derive newtype instance eqAttrName :: Eq AttrName
derive newtype instance ordAttrName :: Ord AttrName

-- | A wrapper for strings which are used as CSS classes.
newtype ClassName = ClassName String

derive instance newtypeClassName :: Newtype ClassName _
derive newtype instance eqClassName :: Eq ClassName
derive newtype instance ordClassName :: Ord ClassName
derive newtype instance semigroupClassName :: Semigroup ClassName
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
derive newtype instance semigroupClassName :: Semigroup ClassName
derive newtype instance semigroupClassName :: Semigroup ClassName

Copy link
Member

Choose a reason for hiding this comment

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

I still have a problem with this instance - I can see that argument for wanting to support "class1 class2" as considered to be a class name, as unless we actually make a smart constructor that rejects names with spaces there's no guarantee it's happening, but now there's a problem with this meaning of this instance too IMO:

If I have x :: ClassName and y :: ClassName it seems like the result of that should be ClassName (unwrap x <> " " <> unwrap y). The name ClassName suggests to me that the value is not part of a class name, but it's a whole class name, so by combining them without a space it's constructing a new class name, not combining two class names.

But I can also see why that might be unexpected for some people, which is kinda why I don't like the instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's a good point, and it's the reason why in my own applications I have a separate classes function which concatenates with spaces (unlike a standard semigroup instance, which concatenates without spaces (rightly so)). I agree that we shouldn't have a semigroup instance as the law-abiding one is not what you probably want.

Copy link
Member

Choose a reason for hiding this comment

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

It would still be law abiding if it included the space, it's just that I think either instance we choose has the potential to trip people up, depending on how they see the type ("it's just a newtype string wrapper" vs a thing with its own semantics).

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, now I can't remember why I thought that was a semigroup law; I must be thinking of the left/right identity laws for monoid.

For what it's worth, we could assert that this is a thing with its own semantics, where ClassName represents a distinct CSS class, and where append includes a separating space. I don't think I've had a conversation with someone using this type in the Halogen world where they saw it as anything else, though that's a small anecdote.

Regardless, if I'm overconfident and the camps are split then it's much better not to have an instance.

Copy link
Member

Choose a reason for hiding this comment

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

It arose here a while back: purescript-halogen/purescript-halogen#512 🙁

Copy link
Contributor

@thomashoneyman thomashoneyman Dec 11, 2020

Choose a reason for hiding this comment

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

Yea, I say we take it out to avoid the confusion. Thanks for the link!

Copy link
Contributor

Choose a reason for hiding this comment

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

Either instance is a perfectly fine semigroup; semigroups only need associativity, and the space-adding version’s associativity follows from normal string concatenation being associative. The difference is that if you use the space-adding Semigroup, you can’t have a Monoid instance because there’s no identity element.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I say we take it out to avoid the confusion.

Will do. For reference here is where the instance was added for Halogen purescript-halogen/purescript-halogen#451