F# 是否允许使用关键字名称定义成员?
type Fruit =
val type : string
Run Code Online (Sandbox Code Playgroud)
在 C# 中,可以使用@:
class Fruit
{
string @type;
}
Run Code Online (Sandbox Code Playgroud) 我有一个小型或平均大小的F#项目,15*.fs文件,大约2000行代码.编译突然变得有点慢,大约5秒钟.我想找出是什么让编译速度变慢,但找不到fsc的开关,如"verbose"导致它显示进度信息.
有人知道如何跟踪F#编译器fsc以了解在哪里调整编译?
Visual Studio Debugger具有那些内存窗口,用于检查原始内存.在Visual Studio 2015中,这些仍然存在,因为有关联的命令(确定,并且文档说明如此).然而,它们并没有出现.
在菜单中,调试 - > Windows - > ....没有内存窗口(4应该是)此外,命令内存1的键盘快捷方式... Memory4回复
"键组合......绑定到当前不可用的命令(存储器1)"
我在C#中尝试了这个,但也在Win32 C++控制台应用程序中尝试过.这是一个错误吗?
当我在Dart编辑器中的Dart项目中创建子文件夹时,立即在此子文件夹中创建一个包子文件夹.我没有读过子文件夹对项目结构有特殊含义的任何地方,但看起来它们确实如此.有谁知道更多?
F#通过模式匹配来分配函数参数.这就是为什么
// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
Run Code Online (Sandbox Code Playgroud)
有效:元组与(a,b)匹配,a和b直接在f内可用.
对受歧视的工会采取同样的做法同样有益,但我无法做到:
// error: same with discriminated unions
type A =
| X of int * int
| Y of string
let f A.X(a, b) = a + b // Error: Successive patterns
// should be separated by spaces or tupled
// EDIT, integrating the answer:
let f (A.X(a, b)) = a + b // correct
f (A.X(7, 4))
Run Code Online (Sandbox Code Playgroud)
模式匹配是函数调用的一部分,仅限于元组吗?有没有办法与受歧视的工会一起做?
我们如何在F#中为不可变结构定义一个ctor,它只接受一些字段.或者,与C#相比,我们如何在f#中将结构清零(例如在下面的c#示例中调用this())?
C#
struct Point
{
private readonly int _x;
private readonly int _y;
public Point(int x) : this() // Will zero the struct
{
_x = x;
}
public Point(int y) : this() // Will zero the struct
{
_y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的Point(x)ctor通过调用this()将结构清零,然后设置单个字段.这就是我所追求的.以下示例被大大简化,问题是如何将结构归零然后只设置一个字段.
F#
type Point =
struct
val X: float
val Y: float
new(x: float) = ? // how to zero out the struct and then set ONLY X ?
end
Run Code Online (Sandbox Code Playgroud) 我可以使用以下代码创建变量赋值:
ts.createVariableStatement(undefined,
[ts.createVariableDeclaration('a', undefined,
ts.createStringLiteral('42'))])
/// yields: var a = 42
Run Code Online (Sandbox Code Playgroud)
但是我无法创建 const 分配。我很确定它应该像这样工作:
ts.createVariableStatement([ts.createModifier(ts.SyntaxKind.ConstKeyword)],
[ts.createVariableDeclaration('a', undefined,
ts.createStringLiteral('42'))])
Run Code Online (Sandbox Code Playgroud)
但这会产生一个错误:
[!] Error: Unexpected keyword 'var'
Run Code Online (Sandbox Code Playgroud)
由于该错误,不会发出任何 JavaScript。错误消息也非常令人困惑。
在 C# 中,我可以定义一个仅适用于参数化泛型类型的扩展方法:
public static bool fun(this List<int> coll, int x)
{
return coll.Contains(x);
}
Run Code Online (Sandbox Code Playgroud)
我在 F# 中尝试了相同的操作,但发现没有办法这样做:
type List<'k when 'k :> int32> with
member o.x s = o.Contains s
Run Code Online (Sandbox Code Playgroud)
这会引发错误 FS0660:此代码的通用性低于其注释的要求,因为无法泛化显式类型变量“k”。它被限制为“int32”。
当然可以定义一个通用的扩展方法,例如
type List<'k> with
member o.x s = o.Contains s
Run Code Online (Sandbox Code Playgroud)
并附注使该扩展方法在 C# 中可用。但这不是这里的问题。我担心参数化通用函数。我认为只能在 C# 中声明,但不能在 F# 中声明。
我的结论是,在 C# 中,扩展方法是函数,而在 F# 中,类似的概念是作为类型扩展实现的,而参数化泛型不是类型,因此这是不可能的。
我是否正确,在 F# 中没有办法做同样的事情?
这里有一个类似的问题:Is it possible to Define a generic extension method in F#? 但11年过去了,我又提起这个话题。
Regex.Replace("a b c a b c", "a", "*")
Run Code Online (Sandbox Code Playgroud)
回报
"* b c * b c"
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法来替换第一次出现,结果是
"* b c a b c"
Run Code Online (Sandbox Code Playgroud)
找不到限制替换的RegExOption.特别是
Regex.Replace("a b c a b c", "a", "*", RegexOptions.Multiline)
Regex.Replace("a b c a b c", "a", "*", RegexOptions.Singleline)
Run Code Online (Sandbox Code Playgroud)
没有任何区别 - 显然.然而,没有更多的选择.
这很好
int GB = 2;
int bytes = GB * 1024 * 1024 * 1024;
LPVOID memory = VirtualAlloc(
0,
bytes-1, // 2GB - 1
MEM_COMMIT,
PAGE_READWRITE);
Run Code Online (Sandbox Code Playgroud)
在这里,一旦达到2GB,就会失败
int GB = 2;
int bytes = GB * 1024 * 1024 * 1024;
LPVOID memory = VirtualAlloc(
0,
bytes, // 2GB
MEM_COMMIT,
PAGE_READWRITE);
Run Code Online (Sandbox Code Playgroud)
Windows错误消息"参数不正确".为什么会这样?实际上我想分配更多的虚拟内存.
在 F# 中不可能为结构体元组定义类型别名。只有通过解决方法,它才有效。
let x = struct (1, 2)
> val x : struct (int * int) = struct (1, 2)
let y : struct (int * int) = struct (4, 5) // explicit type
> val y : struct (int * int) = struct (4, 5)
type S = struct (int * int) // straight definition
> error FS0010: Unexpected symbol '(' in member definition
type S = ValueTuple<int, int> // workaround
> [<Struct>]
type S = struct (int …Run Code Online (Sandbox Code Playgroud) f# ×6
.net ×1
c# ×1
c++ ×1
constructor ×1
dart ×1
dart-editor ×1
dart-pub ×1
debugging ×1
f#-3.0 ×1
generics ×1
racket ×1
regex ×1
scheme ×1
struct ×1
typescript ×1
valuetuple ×1
winapi ×1