我正在寻找一种方法来获取F#选项的值,或者如果它是None,则使用默认值.这似乎很常见,我无法相信预定义的东西不存在.我现在就是这样做的:
// val getOptionValue : Lazy<'a> -> Option<'a> -> 'a
let getOptionValue (defaultValue : Lazy<_>) = function Some value -> value | None -> defaultValue.Force ()
Run Code Online (Sandbox Code Playgroud)
我(某种程度上)正在寻找与C#相当的F#?? 运营商:
string test = GetString() ?? "This will be used if the result of GetString() is null.";
Run Code Online (Sandbox Code Playgroud)
Option模块中没有任何功能可以完成我认为非常基本的任务.我错过了什么?
在C#5中无法使用自动属性进行显式接口实现,但现在C#6支持仅使用getter的自动属性,现在应该可以使用,对吧?
在C#6中创建自动属性成功,但是当尝试在构造函数中为其赋值时,必须首先this转换为接口类型,因为实现是显式的.但这就是VS 2015 RC和VS Code 0.3.0都显示错误,可以在评论中看到:
using static System.Console;
namespace ConsoleApp
{
public interface IFoo { string TestFoo { get; } }
public class Impl : IFoo
{
// This was not possible before, but now works.
string IFoo.TestFoo { get; }
public Impl(string value)
{
// ERROR: Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
((IFoo)this).TestFoo = value;
}
}
public class Program
{
// Yes, not static. DNX …Run Code Online (Sandbox Code Playgroud) 这是允许的:
type Test = class end
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end
Run Code Online (Sandbox Code Playgroud)
但这不是:
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end
type Test = class end
Run Code Online (Sandbox Code Playgroud)
为什么?
在第二种情况下,错误是:类型或模块"测试"的重复定义.
我希望能够定义一些[<Literal>]类型所需的公共常量,并且对于具有相同名称的模块内的类型的用户来说很重要.
我想使用F#的printfn函数打印%符号.在谷歌搜索格式语法之后,这应该可以解决问题:printfn "%%".显然不是...
F#交互输出:
> printfn "%%";;
%%
val it : unit = ()
Run Code Online (Sandbox Code Playgroud)
奇怪的...
我使用F#3.1和.NET 4.5,F#交互式会话使用.NET 4.0.一样.
供参考:printfn "%"不编译(缺少格式说明符),printfn "%s" "%"是我目前的解决方法......
更新:
当我将目标F#运行时从3.1更改为3.0时,它可以正常工作.这是3.1运行时中的错误吗?
我在F#中使用定点组合器时遇到问题:
let rec fix f a = f (fix f) a
fix (fun body num ->
if num = 1000000
then System.Console.WriteLine "Done!"
else body (num + 1)
) 0
Run Code Online (Sandbox Code Playgroud)
(此代码仅用于演示问题,它是专门编写的,因此生成的IL代码易于阅读.)
此代码 - 在使用优化和尾调功能进行编译时 - 会导致a StackOverflowException.我查看了IL代码,可以将问题跟踪到调用内的lambda fix:
.method assembly static void f@1 (class FSharpFunc`2<int32, class Unit> body,int32 num)
{
ldarg.1
ldc.i4 1000000
bne.un.s IL_0014
ldstr "Done!"
call void Console::WriteLine(string)
ret
IL_0014: ldarg.0 // Load the 'body' function onto the stack.
ldarg.1 // Load num onto the stack. …Run Code Online (Sandbox Code Playgroud) f# tail-recursion tail-call-optimization f#-3.1 fixpoint-combinators
我在Windows(.NET 4.5.1)和Linux(Mono 4.0.1)上尝试了这个简单的ASP.net 5控制台应用程序,两次都有相同的结果.
注意:我称之为ASP.net 5控制台应用程序,因为这是在Visual Studio中调用到RC的内容.现在它被称为控制台应用程序(包),但它仍然使用来自https://github.com/aspnet/dnx的 DNX :)
我的Program.cs:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication
{
public class Program
{
public async void Main(String[] args)
{
#if DNX451
AppDomain.CurrentDomain.UnhandledException +=
(s, e) => Console.WriteLine(e);
#endif
try
{
await Task.Delay(1000);
Console.WriteLine("After Task.Delay");
}
finally
{
Console.WriteLine("Inside Finally");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的project.json:
{
"version": "1.0.0-*",
"dependencies": {},
"commands": {
"ConsoleApplication": "ConsoleApplication"
},
"frameworks": {
"dnx451": {}
}
}
Run Code Online (Sandbox Code Playgroud)
当使用1.0.0-beta4 CLR或运行时1.0.0-beta5-11904 CLR …
c# asynchronous task-parallel-library async-await asp.net-core
我试图为具有两个关键属性的模型类型定义一个键,并定义如下:
type Model () =
member val IdOne = 0 with get, set
member val IdTwo = 0 with get, set
member val OtherProperty = "" with get, set
Run Code Online (Sandbox Code Playgroud)
当我尝试在Entity Framework 5中使用此模型时,我收到错误"模型没有定义键.定义此EntityType的键".给出了模型类型,我无法更改它们并添加[<Key>]属性.所以我尝试了Fluent API.
在C#中,你会做这样的事情:
modelBuilder.Entity<Model>().HasKey(m => new { m.IdOne, m.IdTwo });
Run Code Online (Sandbox Code Playgroud)
它使用匿名类型.但对于我的生活,我无法弄清楚如何在F#中解决这个问题.我尝试了Tuples,Records,甚至是具有IdOne和IdTwo属性的常规类型:
// Regular type with properties IdOne & IdTwo.
type ModelKey (idOne, idTwo) =
member this.IdOne = idOne
member this.IdTwo = idTwo
modelBuilder.Entity<Model>().HasKey(fun (m : Model) -> ModelKey (m.IdOne, m.IdTwo))
// ArgumentNullException: Value cannot be null. Parameter …Run Code Online (Sandbox Code Playgroud) 在重构某些代码的过程中,我注意到一种情况,当移动时代码中断:
type ThingBase () = class end
and Functions =
static member Create<'T when 'T :> ThingBase and 'T : (new : Unit -> 'T)> () = new 'T ()
and Thing () =
inherit ThingBase ()
static member Create () = Functions.Create<Thing> ()
// This works, but try moving the Functions type here instead.
Run Code Online (Sandbox Code Playgroud)
如果您移动Functions类型下面的Thing类型,代码会意外中断:
type ThingBase () = class end
and Thing () =
inherit ThingBase ()
static member Create () = Functions.Create<Thing> () …Run Code Online (Sandbox Code Playgroud) 我想知道这里发生了什么......
我刚刚在Visual Studio 2013中创建了一个新的空F#控制台应用程序(使用F#3.1和.NET 4,FSharp.Core版本4.3.1.0),并使用Nuget 添加了Reactive Extensions主库:Install-Package Rx-Main
现在看看这个:

这有效,悬停测试显示val test: unit -> System.Reactive.Subjects.Subject<'a>.正如所料.然后我添加了new关键字.

有趣.有人知道为什么添加new关键字会破坏代码吗?作为参考,如果您另外指定类型参数,它可以工作:

我正在尝试自己构建F#,官方说明包括克隆回购:git clone https://github.com/fsharp/fsharp.
但是如果你看一下repo的主分支,你会看到它仍然是版本3.1.1.25,例如在CHANGES.txt中所述.
那么在哪里可以找到构建指令或只是3.1.2的源代码?3.1分支与主分支相同......
f# ×9
f#-3.1 ×5
c# ×2
.net-4.0 ×1
asp.net-core ×1
async-await ×1
asynchronous ×1
c#-6.0 ×1
f#-3.0 ×1
linux ×1
option ×1
printf ×1