假设我有一个环境变量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) $
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...发布于 2020-03-17 11:44:54
这里的核心问题来自于Haskell缺乏对存在类型或联合类型的一流支持。一个简单得多的例子是,即使if x then show 'a' else show "abc"是有效的,show $ if x then 'a' else "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来使两个分支具有相同的类型。
https://stackoverflow.com/questions/60715148
复制相似问题