简体   繁体   中英

What does the @ symbol do within an expression in Haskell?

I'm trying to figure out what @ does in an expression like endpoint @"start" . Is it part of a language extension perhaps?

I see the follow extensions enabled for the module the function is in.

{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE DeriveAnyClass             #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE DerivingStrategies         #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase                 #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RecordWildCards            #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeApplications           #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE TypeOperators              #-}

The full function:

endpoints :: Contract () AuctionSchema Text ()
endpoints = (start' `select` bid' `select` close') >> endpoints
  where
    start' = endpoint @"start" >>= start
    bid'   = endpoint @"bid"   >>= bid
    close' = endpoint @"close" >>= close

There are two relevant extensions' documentation to read: TypeApplications and DataKinds . A snippet from the type applications documentation:

The TypeApplications extension allows you to use visible type application in expressions. Here is an example: show (read @Int "5") . The @Int is the visible type application; it specifies the value of the type variable in read 's type.

And from the data kinds documentation:

With DataKinds , GHC automatically promotes every datatype to be a kind and its (value) constructors to be type constructors.

I guess you sort of also have to know about Symbol , a type-level representation of strings that is more efficient (but less featureful) than type-level [Char] , but I couldn't find a good place in the official documentation to read about it. You can read about it some in the GHC.TypeLits haddocks .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM