小编Lui*_*rez的帖子

F 有界多态性相对于类型类的优点对于返回电流类型问题

StackOverflow 上经常会问到返回当前类型的问题。是一个这样的例子。通常的答案似乎是F 有界多态性类型类模式解决方案。奥德斯基在F-bound 多态性有用吗?

F-bounds 确实增加了显着的复杂性。我希望能够摆脱它们,并用更高级的子类型替换它们

而 tpolecat(链接帖子的作者)建议

更好的策略是使用类型类,它可以巧妙地解决问题,并且几乎没有担心的余地。事实上,在这些情况下完全放弃子类型多态是值得考虑的。

确定以下缺点的地方

F-bounded polymorphism 将一个类型参数化为它自己的子类型,这是一个比用户通常想要的更弱的约束,这是一种表达“我的类型”的方式,你无法通过子类型精确表达。然而类型类可以直接表达这个想法,所以这就是我教初学者的东西

我的问题是,根据上述建议,有人可以证明 F 有界多态性是有利的,还是我们应该指出类型类解决方案作为解决返回电流类型问题的规范答案?

类型参数的 F 绑定多态性

trait Semigroup[A <: Semigroup[A]] { this: A =>
  def combine(that: A): A
}

final case class Foo(v: Int) extends Semigroup[Foo] {
  override def combine(that: Foo): Foo = Foo(this.v + that.v)
}

final case class Bar(v: String) extends Semigroup[Bar] {
  override def combine(that: Bar): Bar = Bar(this.v …
Run Code Online (Sandbox Code Playgroud)

scala typeclass f-bounded-polymorphism return-current-type

9
推荐指数
1
解决办法
491
查看次数

单个特征与混合的实例化

不提供类主体就无法创建单个特征的实例的任何想法:

trait MyTrait
val test1 = new MyTrait // Does not compile.
val test2 = new MyTrait {} // Compiles.
Run Code Online (Sandbox Code Playgroud)

但是,如果我将另一个添加到混合中,则可以创建一个实例:

trait MyTrait
trait SecondTrait
val anotherTest = new SecondTrait with MyTrait  // Compiles successfully.
Run Code Online (Sandbox Code Playgroud)

我本来期望相同的行为。

旁注:我已经读过这个问题。但是特质主体的存在并不能解决我的问题,因为第二个示例仍然没有主体。因此,为什么编译器将第二个示例视为匿名类?

scala

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

为什么更喜欢隐式 val 而不是隐式对象

当询问有关隐式的问题时,与答案(或有时这就是答案本身)一起给出的常见建议/推荐/建议是implicit vals显式类型签名一起使用,而不是使用implicit objects

但是,这背后的原因是什么?

design-patterns scala implicit

4
推荐指数
1
解决办法
211
查看次数

建模可选参数的最佳方法

正如标题所说,在Scala 中建模可选参数的最佳方法是什么?

对于可选参数,我的意思是执行函数体不需要的值。

要么因为该参数存在默认值,要么根本不需要该参数本身(例如配置或调试标志);请注意,在Java 上,我可能会传递null给这些参数。


这是Scala社区的常见问题解答,特别是由新手制作的。

例如:

design-patterns scala optional

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

时间:2019-03-17 标签:c++WriteFileunicodecharacters

我正在尝试使用 WriteFile 函数将 wstring 写入 UTF-8 文件。\n我希望该文件具有这些字符“\xc3\x91\xc3\x81”,但我得到的是“\xef\xbf” \xbd"。

\n\n

这是代码

\n\n
#include <iostream>\n#include <cstdlib>\n#include <sstream>\n#include <string>\n#include <fstream>\n#include <windows.h>\n#include <wchar.h>\n#include <stdio.h>\n#include <winbase.h>\nusing namespace std;\n\nconst char filepath [] = "unicode.txt";\n\nint main ()\n{ \n   wstring str;\n   str.append(L"\xc3\x91\xc3\x81");\n   wchar_t* wfilepath;\n\n   // Create a file to work with Unicode and UTF-8\n   ofstream fs;\n   fs.open(filepath, ios::out|ios::binary);\n   unsigned char smarker[3];\n   smarker[0] = 0xEF;\n   smarker[1] = 0xBB;\n   smarker[2] = 0xBF;\n   fs << smarker;\n   fs.close();\n\n   //Open and write in the file with windows functions\n   mbstowcs(wfilepath, filepath, strlen(filepath));\n   HANDLE hfile;\n …
Run Code Online (Sandbox Code Playgroud)

c++ unicode c++11

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

Scala-Cats:验证Map的条目

假设我有:

val m: Map[String, Int] = Map("one" -> 1, "five" -> 5, "six" -> 6, "nine" -> 9)
Run Code Online (Sandbox Code Playgroud)

我有两个功能:

def isNotDivisibleByTwo(i: Int): ValidatedNec[String, Int] = Validated.condNec(i%2!=0, i, s"$i is divisible by 2.")

def isNotDivisibleByThree(i: Int): ValidatedNec[String, Int] = Validated.condNec(i%3!=0, i, s"$i is divisible by 3.")
Run Code Online (Sandbox Code Playgroud)

我想要一个给我的功能:

def sanitize(m: Map[String, Int]):Map[String, Validated[NonEmptyList[String], Int]] = ???
Run Code Online (Sandbox Code Playgroud)

它应该返回满足上述两个功能的所有数字,以及所有失败数字及其相关故障的映射。
例如对于给定的列表m,我想得到:

val result = Map(
  "one" -> Valid(1),
  "five -> Valid(5),
  "nine" -> Invalid(NonEmptyList("9 is dividible by 3")),
  "six" -> Invalid(NonEmptyList("6 is dividible …
Run Code Online (Sandbox Code Playgroud)

validation scala scala-cats

0
推荐指数
1
解决办法
82
查看次数

c++ &lt;错误:无法访问地址 0x1 处的内存&gt;

我真的不明白为什么我的代码有这个问题

首先,我创建了两个指向 char 的指针

char* finWord;
char* ignoredWord;
Run Code Online (Sandbox Code Playgroud)

然后我将它们作为参数传递给其他函数

lowerCase(line.substr(0, endWord), ignoredWord);
toNormalWord(ignoredWord, finWord);
Run Code Online (Sandbox Code Playgroud)

但是当我运行程序时,它抛出了一个分段错误,问题是 finWord 地址总是 0x1

这里是问题发生的地方

void toNormalWord (string src, char* des) 
{
    char c;
    des[sizeof(src) + 1];
    int position = 0; 

    if (isThere)
    {
        des[position] = c; //Here the gdb show me the following error 0x1 <error: 
                           // Cannot access memory at address 0x1>
        position++;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

-1
推荐指数
1
解决办法
6167
查看次数