小编gre*_*ake的帖子

如何在for循环中注释类型

我想在for-loop中注释一个变量类型.我试过这个:

for i: int in range(5):
    pass
Run Code Online (Sandbox Code Playgroud)

但显然它没有用.

我期望在PyCharm 2016.3.2中进行自动完成工作.像这样的预注释:

i: int
for i in range(5):
    pass
Run Code Online (Sandbox Code Playgroud)

没有帮助.

PS预注释适用于PyCharm> = 2017.1

python for-loop type-hinting pycharm python-3.6

55
推荐指数
3
解决办法
1万
查看次数

C和C++原子之间的互操作性

假设,我有一个可能从另一个线程中取消的任务.该任务在C函数中执行,另一个线程运行C++代码.我怎么做?

粗略的例子.

C:

void do_task(atomic_bool const *cancelled);
Run Code Online (Sandbox Code Playgroud)

C++:

std::atomic_bool cancelled;
…
do_task(&cancelled);
Run Code Online (Sandbox Code Playgroud)

现在,我创建了一个atomics.h包含以下内容的文件:

#ifdef __cplusplus
#include <atomic>
using std::atomic_bool;
#else
#include <stdatomic.h>
#endif
Run Code Online (Sandbox Code Playgroud)

它似乎有效,但我没有看到任何保证.我想知道,如果有更好的(正确的)方式.

c c++ atomic

10
推荐指数
2
解决办法
813
查看次数

为什么 Haskell 中 Int 的标准保证范围恰好是 [-2^29; 2^29)?

为什么范围如此具体?为什么不是 29 位宽或 32 位宽,为什么是 30 位宽?

理由是什么?标准(1)似乎没有给出它

haskell

8
推荐指数
1
解决办法
372
查看次数

有N级镜头吗?

假设我有一个简单的 GADT。

\n
data Expr a where\n  Int\' :: Integer -> Expr Int\n  Fun :: Text -> Expr a\n  (:*) :: Expr (a -> b) -> Expr a -> Expr b\n
Run Code Online (Sandbox Code Playgroud)\n

现在,我可以定义以下遍历:

\n
transform :: Applicative f => (forall b. Expr b -> f (Expr b)) -> Expr a -> f (Expr a)\ntransform f (a :* b) = (:*) <$> transform f a <*> transform f b\ntransform f v = f v\n
Run Code Online (Sandbox Code Playgroud)\n

奇怪的是,从镜头上看,这种类型很像Traversal。然而,不完全是,它的排名更高。

\n

我仍然可以定义 \xc2\xabconventional\xc2\ …

haskell haskell-lens

7
推荐指数
1
解决办法
143
查看次数

COMPLETE pragma doesn't prevent incomplete-patterns warning

I made two pattern views for a list-like class.

infixr 5 :<
pattern (:<) :: Stream s => Token s -> s -> s
pattern b :< bs <- (uncons -> Just (b, bs))
  where b :< bs = cons b bs

pattern Nil :: Stream s => s
pattern Nil <- (uncons -> Nothing)
  where Nil = empty
Run Code Online (Sandbox Code Playgroud)

uncons signature: uncons :: (Stream s) => s -> Maybe (Token s, s).

Suppose I also have function that uses …

haskell ghc

5
推荐指数
0
解决办法
52
查看次数

模式同义词的显式类型变量实例化?

假设我有以下代码

\n
data F a where\n  F :: Typeable a => F a\n\nasType :: forall b a. Typeable b => F a -> Maybe (a :~: b, F b)\nasType e@F{} = case eqT @b @a of\n  Just Refl -> Just (Refl, e)\n  Nothing -> Nothing\n\npattern AsType :: forall a b. Typeable a => (a ~ b) => F a -> F b\npattern AsType e <- (asType @a -> Just (Refl, e))\n\npattern AsInt :: Typeable b\' => (Int ~ b\') => …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-synonyms

5
推荐指数
0
解决办法
122
查看次数

为什么关联类型同义词不暗示约束

为什么在签名中使用关联的类型同义词并不意味着相应的约束?

例如,为什么以下内容不能编译:

{-# LANGUAGE TypeApplications, ScopedTypeVariables, TypeFamilies, AllowAmbiguousTypes #-}

class MyClass a where
  type AssociatedType a 
  bar :: Int

foo :: forall a. AssociatedType a -> Int
foo _ = bar @a
Run Code Online (Sandbox Code Playgroud)

ghc 8.6.5 给出以下错误:

error:
    * No instance for (MyClass a) arising from a use of `bar'
      Possible fix:
        add (MyClass a) to the context of
          the type signature for:
            foo :: forall a. AssociatedType a -> Int
    * In the expression: bar @a
      In an equation for `foo': …
Run Code Online (Sandbox Code Playgroud)

haskell type-families

3
推荐指数
1
解决办法
91
查看次数

从一个char中生成std :: string的最佳方法是什么?

有很多方法可以从一个char创建一个std :: string.

  • std::string(1, ch)
  • std::string() + ch
  • std::string(&ch, 1)
  • std::string {ch} \\ c++11

我想知道哪一个我更喜欢.

c++ c++11

2
推荐指数
1
解决办法
181
查看次数