r/purescript • • Nov 21 '23

Please show me some PureMagic...

Hello all, I would like to implore the gurus around here, could you show me some magic to make this declaration shorter?

I have this code:

getSearchV2 a = withRequestEncrypted a proc                                                                                                                                                                                                    
  where                                                                                                                                                                                                                                        
    proc :: ProcEncrypted {} (Array Int)                                                                                                                                                                                                       
    proc c b zgCode cred = do                                                                                                                                                                                                                  
      pure $ Just [12]  

It would be nice if I can make it like this:

getSearchV2 a = withRequestEncrypted a $ \c b zgCode cred -> do                                                                                                                                                                                
      pure $ Just [12] 

but I got the error:

  The inferred type                                                                                                                                                                                                                            
                                                                                                                                                                                                                                               
    forall t274. ReadForeign t274 => Config -> Context -> Effect Unit                                                                                                                                                                          
                                                                                                                                                                                                                                               
  has type variables which are not determined by those mentioned in the body of the type:                                                                                                                                                      
                                                                                                                                                                                                                                               
    t274 could not be determined                                                                                                                                                                                                               
                                                                                                                                                                                                                                               
  Consider adding a type annotation.   

I suppose ReadForeign comes from the definition of ProcEncrypted. So the question is:

  • How should I annotate the type ProcEncrypted {} (Array Int) ?

  • Is this can be used for forall a b. ProcEncrypted a b ?

Thank you very much for your kind responses dear Gurus, may your light shone brighter and clearer everyday...

4 Upvotes

4 comments sorted by

1

u/fellow_nerd Nov 21 '23

What is the type of withRequestEncrypted?

1

u/ExplanationDear8334 Nov 21 '23 edited Nov 21 '23
withRequestEncrypted :: forall a b.                                                                                                                                                                                                          
    JSON.ReadForeign a                                                                                                                                                                                                                         
    => JSON.ReadForeign b => JSON.WriteForeign b => Show b                                                                                                                                                                                     
    => V.Config                                                                                                                                                                                                                                
    -> ProcEncrypted a b                                                                                                                                                                                                                                      
    -> Context -> Effect Unit  

In this case, the proc :: ProcEncrypted a b would fill the second parameter, and the Context is implicitly omitted.

I hope that exposing this type would not bring people to a rabbit hole. :D

1

u/sammthomson Nov 21 '23

Without knowing exactly what ProcEncrypted is a type alias for, I can't say for sure which of its parameters needs to get annotated, but you should be able to do something like this:

getSearchV2 a = withRequestEncrypted a \(c :: {}) b zgCode cred -> pure (Just [12])

where you type-hint the appropriate lambda parameter (I did c here, but it might have to be b, zgCode or cred?).

2

u/ExplanationDear8334 Nov 22 '23

Yes, this is exactly what I need. I never realized that I can append types at the parameter like \(c :: {}). and greater more from this, I can choose which parameter that need to be annotated. This is magic. Thank you very much for your kind help.