Skip to content

Generic lenses #31

Description

@cryogenian

Looking at #30

There is a couple of suggestions

Type classes for lenses

class HasScheme a where 
  _scheme :: ALens' a (Maybe Scheme)

Using records instead

type AbsoluteURI = 
  { scheme :: Maybe Scheme 
  , query :: Maybe Query 
  , hiePart :: HierarchicalPart 
  } 

_scheme :: forall r a. Lens' { scheme :: a |r } a 
_scheme = lens _.scheme _{ scheme = _ }

Using newtype around records

newtype AbsoluteURI = AbsoluteURI
  { scheme :: Maybe Scheme 
  ... } 

derive instance newtypeAbsoluteURI :: Newtype AbsoluteURI _ 

_scheme :: forall r n a rr. Newtype n r => RowCons "scheme" a r rr => Lens' n a 
_scheme = _Newtype <<< prop "scheme"

@garyb @kritzcreek What do you think? I think the 3rd one preserves both typesafety and polymorphic behaviour w/o introducing new entities.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions