为什么unsafePartial在PureScript中不能与简单的函子一起使用?

问题描述 投票:0回答:1

除非我犯了一些简单的错误,否则以下代码在功能上应相同:

-- This code does not compile
pg :: forall a. PG a -> Route a
pg sql = connect $ apply (runPG sql) (unsafePartial <<< fromJust <$> getConnection)

-- This code does not compile
pg sql = connect $ do
    connection <- unsafePartial <<< fromJust <$> getConnection
    runPG sql connection

-- This code does work
pg sql = connect $ do
   connection <- getConnection
   runPG sql $ unsafePartial $ fromJust connection

为了帮助理解这一点,以下是相关类型:

-- Route has a MonadAff instance and all the usual stuff
newtype Route a = Route (ReaderT RouteState Aff a)

-- `connect` makes a connection to Postgres and injects it into the environment.
connect :: forall a. Route a -> Route a 

getConnection :: Route (Maybe Connection)

-- PG has a MonadAff instance and all the usual stuff
newtype PG a = PG (ReaderT Connection Aff a)

runPG :: forall m a. MonadAff m => PG a -> Connection -> m a

这里是错误:

Error found:
    in module AWS.Lambda.Router
    at src/AWS/Lambda/Router.purs:176:70 - 176:83 (line 176, column 70 - line 176, column 83)

      Could not match constrained type

        Partial => t1

      with type

        { client :: Client
        , pool :: Pool    
        }                 


    while trying to match type { client :: Client
                               , pool :: Pool    
                               }                 
      with type Partial => t1
    while checking that expression getConnection
      has type t0 (Maybe (Partial => t1))
    in value declaration pg

    where t0 is an unknown type
          t1 is an unknown type

    See https://github.com/purescript/documentation/blob/master/errors/ConstrainedTypeUnified.md for more information,
    or to contribute content related to this error.


    spago: Failed to build.

我认为这是两件事之一。尽管看起来很简单,要么我正在犯一些愚蠢的语法错误,要么是我以为我不理解Partial

除非我犯了一些简单的错误,否则以下代码在功能上应该相同:-此代码不会编译pg :: forall a。 PG a->路由pg sql = connect $ apply(runPG sql)...

functor purescript
1个回答
1
投票

之所以会这样,是因为类型推断在约束条件下无法很好地工作。它并不总是知道是否需要将约束移到顶部或将约束留在原处。通常,这是一个无法确定的问题,编译器只是尽力而为。

© www.soinside.com 2019 - 2024. All rights reserved.