我可以在“let”块内定义类型同义词吗?

jus*_*man 5 syntax haskell types

我想要得到这样的东西。这是可能的?

something :: String
something = 
  let
    type FirstName = String
    type LastName = String
    
    fullName :: FirstName -> LastName -> String
    fullName = a ++ " " ++ b
  in
    fullName "Haskell" "Curry"
Run Code Online (Sandbox Code Playgroud)

我一直在尝试寻找能够执行此操作的语言扩展,但无济于事。

Yur*_*yro 6

直接答案是否定的,类型定义只能在模块级别上进行。

类似的事情是可能的:

let
  fullName ::
    (firstName ~ String, lastName ~ String) =>
    firstName -> lastName -> String
  fullName a b = a ++ " " ++ b
in
  _
Run Code Online (Sandbox Code Playgroud)

  • > _类型上下文可以包含 t1 ~ t2 形式的等式约束,这表示类型 t1 和 t2 需要相同。_ 我从 [The Glorious GHC 系统用户指南,版本 7.4.1](http: //downloads.haskell.org/~ghc/7.4.1/docs/html/users_guide/equality-constraints.html) (4认同)