Get a newtype'd records underlying type in purescript











up vote
0
down vote

favorite












I'm trying to see if there's an easy way to get the type of a newtype'd record to put in function signatures.



newtype T1 = T1 { foo:: Int}
derive instance newtypeT1 :: Newtype T1 _
... other classes that require me to newtype the record ...


I know I can access a records members with
_.property and I can compose that with unwrap
unwrap >>> _.property to get a function for that property, but I'd like to write a function similar to



testFoo :: forall a. (_ -> a) -> Effect a
testFoo accessor = (unwrap >>> accessor) <$> loadT1


This works but the wildcard symbol gives an warning, but I'm not sure how to get that record definition from T1. (This is a minimal example, I have a massive property object that is from an external source.



A workaround I've been using for now has been to declare my type like



 type InnerT1 = { foo ::Int}
newtype T1 = T1 InnerT1


and exporting that InnerT1 so it can be used in my test file, but this seems a bit clunky and I am wondering if there is a better way?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to see if there's an easy way to get the type of a newtype'd record to put in function signatures.



    newtype T1 = T1 { foo:: Int}
    derive instance newtypeT1 :: Newtype T1 _
    ... other classes that require me to newtype the record ...


    I know I can access a records members with
    _.property and I can compose that with unwrap
    unwrap >>> _.property to get a function for that property, but I'd like to write a function similar to



    testFoo :: forall a. (_ -> a) -> Effect a
    testFoo accessor = (unwrap >>> accessor) <$> loadT1


    This works but the wildcard symbol gives an warning, but I'm not sure how to get that record definition from T1. (This is a minimal example, I have a massive property object that is from an external source.



    A workaround I've been using for now has been to declare my type like



     type InnerT1 = { foo ::Int}
    newtype T1 = T1 InnerT1


    and exporting that InnerT1 so it can be used in my test file, but this seems a bit clunky and I am wondering if there is a better way?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to see if there's an easy way to get the type of a newtype'd record to put in function signatures.



      newtype T1 = T1 { foo:: Int}
      derive instance newtypeT1 :: Newtype T1 _
      ... other classes that require me to newtype the record ...


      I know I can access a records members with
      _.property and I can compose that with unwrap
      unwrap >>> _.property to get a function for that property, but I'd like to write a function similar to



      testFoo :: forall a. (_ -> a) -> Effect a
      testFoo accessor = (unwrap >>> accessor) <$> loadT1


      This works but the wildcard symbol gives an warning, but I'm not sure how to get that record definition from T1. (This is a minimal example, I have a massive property object that is from an external source.



      A workaround I've been using for now has been to declare my type like



       type InnerT1 = { foo ::Int}
      newtype T1 = T1 InnerT1


      and exporting that InnerT1 so it can be used in my test file, but this seems a bit clunky and I am wondering if there is a better way?










      share|improve this question













      I'm trying to see if there's an easy way to get the type of a newtype'd record to put in function signatures.



      newtype T1 = T1 { foo:: Int}
      derive instance newtypeT1 :: Newtype T1 _
      ... other classes that require me to newtype the record ...


      I know I can access a records members with
      _.property and I can compose that with unwrap
      unwrap >>> _.property to get a function for that property, but I'd like to write a function similar to



      testFoo :: forall a. (_ -> a) -> Effect a
      testFoo accessor = (unwrap >>> accessor) <$> loadT1


      This works but the wildcard symbol gives an warning, but I'm not sure how to get that record definition from T1. (This is a minimal example, I have a massive property object that is from an external source.



      A workaround I've been using for now has been to declare my type like



       type InnerT1 = { foo ::Int}
      newtype T1 = T1 InnerT1


      and exporting that InnerT1 so it can be used in my test file, but this seems a bit clunky and I am wondering if there is a better way?







      purescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 7:17









      Beau Trepp

      84021224




      84021224
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can use the Newtype class to get at the inner type:



          testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
          testFoo accessor = (unwrap >>> accessor) <$> loadT1


          This works without additional annotations, because the class has a functional dependency Newtype a b | a -> b, which means that the inner type is uniquely determined by the outer type.






          share|improve this answer





















          • This works perfectly!, I knew there was something in there. Thanks!
            – Beau Trepp
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236838%2fget-a-newtyped-records-underlying-type-in-purescript%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You can use the Newtype class to get at the inner type:



          testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
          testFoo accessor = (unwrap >>> accessor) <$> loadT1


          This works without additional annotations, because the class has a functional dependency Newtype a b | a -> b, which means that the inner type is uniquely determined by the outer type.






          share|improve this answer





















          • This works perfectly!, I knew there was something in there. Thanks!
            – Beau Trepp
            2 days ago















          up vote
          1
          down vote



          accepted










          You can use the Newtype class to get at the inner type:



          testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
          testFoo accessor = (unwrap >>> accessor) <$> loadT1


          This works without additional annotations, because the class has a functional dependency Newtype a b | a -> b, which means that the inner type is uniquely determined by the outer type.






          share|improve this answer





















          • This works perfectly!, I knew there was something in there. Thanks!
            – Beau Trepp
            2 days ago













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can use the Newtype class to get at the inner type:



          testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
          testFoo accessor = (unwrap >>> accessor) <$> loadT1


          This works without additional annotations, because the class has a functional dependency Newtype a b | a -> b, which means that the inner type is uniquely determined by the outer type.






          share|improve this answer












          You can use the Newtype class to get at the inner type:



          testFoo :: forall a inner. Newtype T1 inner => (inner -> a) -> Effect a 
          testFoo accessor = (unwrap >>> accessor) <$> loadT1


          This works without additional annotations, because the class has a functional dependency Newtype a b | a -> b, which means that the inner type is uniquely determined by the outer type.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 14:24









          Fyodor Soikin

          41.2k56295




          41.2k56295












          • This works perfectly!, I knew there was something in there. Thanks!
            – Beau Trepp
            2 days ago


















          • This works perfectly!, I knew there was something in there. Thanks!
            – Beau Trepp
            2 days ago
















          This works perfectly!, I knew there was something in there. Thanks!
          – Beau Trepp
          2 days ago




          This works perfectly!, I knew there was something in there. Thanks!
          – Beau Trepp
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236838%2fget-a-newtyped-records-underlying-type-in-purescript%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python