假设我有一个环境变量IS_REMOTE
。我想要一组布局(如果它是“ 1”),以及另一组布局(如果不是)或未定义。到目前为止,我已经
import XMonad
import XMonad.Config.Desktop
import XMonad.Hooks.ManageDocks
import XMonad.Layout.LayoutModifier
import XMonad.Layout.MultiColumns
import XMonad.Layout.PerWorkspace
import XMonad.Layout.ThreeColumns
import Data.Maybe
import System.Environment
main = do
isRemoteEnv <- lookupEnv "IS_REMOTE"
xmonad $ desktopConfig
{
layoutHook = myLayout (fromMaybe "0" isRemoteEnv)
}
myLayout remote = if remote == "1"
then onWorkspace "web" (avoidStruts $ (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
(avoidStruts $ (ThreeColMid 1 0.02 (1/2)) ||| Full)
else onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
(avoidStruts $ Mirror (ThreeColMid 1 0.02 (1/2)) ||| Full)
这不能编译,因为(我认为)if
的两个分支具有不同的类型。但这是我的Haskell知识的极限。正确的方法是什么?
xmonad.hs:21:8: error:
* Couldn't match type `Mirror MultiCol' with `MultiCol'
Expected type: PerWorkspace
(ModifiedLayout AvoidStruts (Choose MultiCol Full))
(ModifiedLayout AvoidStruts (Choose ThreeCol Full))
a
Actual type: PerWorkspace
(ModifiedLayout AvoidStruts (Choose (Mirror MultiCol) Full))
(ModifiedLayout AvoidStruts (Choose (Mirror ThreeCol) Full))
a
* In the expression:
onWorkspace
"web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
$ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
In the expression:
if remote == "1" then
onWorkspace
"web" (avoidStruts $ (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
$ (avoidStruts $ (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
else
onWorkspace
"web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
$ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
In an equation for `myLayout':
myLayout remote
= if remote == "1" then
onWorkspace
"web" (avoidStruts $ (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
$ (avoidStruts $ (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
else
onWorkspace
"web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
$ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
|
21 | else onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
答案 0 :(得分:0)
这里的核心问题来自Haskell对存在或联合类型缺乏一流的支持。一个更简单的示例是show $ if x then 'a' else "abc"
无效,即使if x then show 'a' else show "abc"
有效。无论如何,要解决您的问题,这不是很漂亮,但可以:
import XMonad
import XMonad.Config.Desktop
import XMonad.Hooks.ManageDocks
import XMonad.Layout.LayoutModifier
import XMonad.Layout.MultiColumns
import XMonad.Layout.PerWorkspace
import XMonad.Layout.ThreeColumns
import Data.Kind
import Data.Maybe
import System.Environment
main = do
isRemoteEnv <- lookupEnv "IS_REMOTE"
case myLayout (fromMaybe "0" isRemoteEnv) of Layout c -> xmonad $ desktopConfig {
layoutHook = c
}
myLayout remote = if remote == "1"
then Layout $ onWorkspace "web" (avoidStruts $ (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
(avoidStruts $ (ThreeColMid 1 0.02 (1/2)) ||| Full)
else Layout $ onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
(avoidStruts $ Mirror (ThreeColMid 1 0.02 (1/2)) ||| Full)
它使用the Layout
existential type来使两个分支具有相同的类型。