我可以将字符串保存到本地浏览器存储中
Browser.localStorage.setItem(key, str)
但是当我尝试对受歧视的联合进行字符串化(例如通过调用string它)时,它会显示为[Object object].
由于FSharp.ReflectionFable 中不支持 和 枚举操作函数,我如何保存和加载 DU 或枚举值(无需为每种情况做大量额外工作)?
我正在尝试创建一个具有较低级别库的解决方案,该库将知道在调用某些命令时需要保存和加载数据,但是在特定于平台的项目中将提供保存和加载函数的实现它引用了较低级别的库.
我有一些模型,例如:
type User = { UserID: UserID
Situations: SituationID list }
type Situation = { SituationID: SituationID }
Run Code Online (Sandbox Code Playgroud)
而我想要做的是能够定义和调用以下函数:
do saveUser ()
let user = loadUser (UserID 57)
Run Code Online (Sandbox Code Playgroud)
有没有办法在功能习语中干净地定义它,最好是在避免可变状态时(无论如何都不需要)?
一种方法可能看起来像这样:
type IStorage = {
saveUser: User->unit;
loadUser: UserID->User }
module Storage =
// initialize save/load functions to "not yet implemented"
let mutable storage = {
saveUser = failwith "nyi";
loadUser = failwith "nyi" }
// ....elsewhere:
do Storage.storage = { a real implementation of IStorage }
do Storage.storage.saveUser () …Run Code Online (Sandbox Code Playgroud) 在C#中,如果我定义了一个struct,我也可以覆盖ToString().然后,当我正在调试并添加一个监视或将鼠标悬停在结构的实例上时,工具提示将是计算的ToString()而不是结构的类型名称.
我能以某种方式在C++和/或C++/CLI中这样做吗?也就是说,我可以将方法定义为结构的一部分(或做其他任何事情),这将导致watch-value/tooltip显示我选择的字符串?Visual Studio for C/C++中的默认字符串呈现是所有结构的字段值的列表(或者可以卡在小框中的数量).
我的类型都是C风格的结构.(在我将文件转换为.cpp并修复了一些类型问题之前,它实际上是用C编写的,所以我可以在CLI中运行它.)这是一个示例结构:
struct other_dollars_node
{
struct other_dollars_node *next_other_dollars;
override *overrides;
long other_dollars_id;
tm effective_date;
double amount;
}
Run Code Online (Sandbox Code Playgroud)
我对C++/CLI的经验很少 - 我的大多数经验都是使用原生C/C++和C#.我正在使用Visual Studio 2013.
更新:由于几乎所有现有代码都使用本机C语法,而我更喜欢无需重构的解决方案,因此CLI方面可能不那么重要.
当我写下这个声明时:
var x = new ClassName();
x隐式键入为ClassName?. 也就是说,由于ClassName是引用类型,因此隐式赋值会var自动定义为可空(即使我提供非空实例并且从不修改它)。
我的问题是,在使用“var”关键字时,有什么方法可以使非空性成为默认值吗?
我知道这个问题和相关信息:Why does Visual Studio Type a Newly Minted Array as Nullable?
我尝试以下方法:
let c x = System.Numerics.Complex(x, 0.0)
let sum = [c 1.0; c 2.0] |> List.sum
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
The type 'System.Numerics.Complex' does not support the operator 'get_Zero'
我从https://msdn.microsoft.com/en-us/library/dd233211.aspx上阅读了类型扩展的规则,并尝试执行以下操作:
module ComplexExtension =
let c x = System.Numerics.Complex(x, 0.0)
type System.Numerics.Complex with
// I also tried a bunch of other ways of writing these
// as static or instance members, but nothing worked
static member Zero = c 0.0
static member One = c 1.0
open ComplexExtension
let sum = [c …Run Code Online (Sandbox Code Playgroud) 我是单元测试的n00b.我已经安装FsCheck.Nunit,并NUnitTestAdapter从的NuGet,我试图做基于属性的测试,主要是由灵感不可估量的斯科特Wlaschin.
我正在使用该[<Property>]属性,我希望能够"跳过"不符合测试要求的输入:
[<Property(MaxTest=10)>]
let ``Calling unzipTo with an invalid destination will yield a failure.`` badDest =
if Directory.Exists(badDest)
then // somehow skip to the next randomized input
else // do the actual test
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?
我更喜欢FsCheck/NUnit的答案,如果它存在,但我也会考虑任何其他框架,其测试可以在Visual Studio中运行.(我以为我看到了一个框架,其中有一个简单的函数来完成这个,但我无法弄清楚它是什么.)
到目前为止,我更喜欢FsCheck.NUnit,因为它可以为F#类型(有区别的联合等)生成随机输入而无需额外的工作.
var isTrue = new string[0] is object[];
var isFalse = new int[0] is object[];
Run Code Online (Sandbox Code Playgroud)
我知道值类型和引用类型之间的区别,但为什么这种行为有意义?特别是从:
var isTrue = "3" is object;
var isAlsoTrue = 3 is object;
Run Code Online (Sandbox Code Playgroud) 因此,在Project AB中,我有FileA.fs和FileB.fs.FileB使用FileA中的定义,FileA和FileB都使用Project C中的定义(用C#编写).
在FileA.FS中,我有:
#if COMPILED
namespace ProjectAB
#else
#I "bin\debug"
#r "ProjectZ.dll"
#endif
Run Code Online (Sandbox Code Playgroud)
......它的运作方式如何 - 我可以用F#-Interactive运行整个文件,这很棒.
在FileB.fs中,我的标题是:
#if COMPILED
module ProjectAB.ModuleB
#else
#load "FileA.fs"
#I "bin\debug"
#r "ProjectZ.dll"
#endif
Run Code Online (Sandbox Code Playgroud)
但是当我运行它(从FileB),我得到错误:
FileA.fs(6,1): error FS0222: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration.
Run Code Online (Sandbox Code Playgroud)
根据fsi.exe引用,#load指令"读取源文件,编译并运行它".但似乎必须在没有定义COMPILED指令的情况下这样做,因为它没有看到"命名空间ProjectAB"声明.
如何设置标题以便我可以在F#中运行任一文件?
编辑下面的每个latkin的答案,我创建了一个脚本作为项目中的最后一个文件_TestScript.fsx.我从其他文件中删除了所有预编译器的东西,并将其设置为.fsx文件的标头:
#if INTERACTIVE
#I "bin\debug"
#r "ProjectZ.dll"
#load "FileA.fs"
#load …Run Code Online (Sandbox Code Playgroud) 通过引用默认的WPF DLL,使用仅代码的WPF可以轻松完成任何操作:
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
// ...other DLLs...
#r "WindowsBase.dll"
let window = System.Windows.Window()
let panel = System.Windows.Controls.StackPanel()
let button = System.Windows.Controls.Button()
panel.Children.Add button
button.Content <- "hi"
window.Content <- panel
window.Show()
Run Code Online (Sandbox Code Playgroud)
......当窗口仍然打开时你可以操纵它......
button.Click.Add (fun _ ->
button.Content <-
button.Content :?> string |> fun x -> (x + "!") :> obj)
Run Code Online (Sandbox Code Playgroud)
...然后单击按钮以查看其工作情况.这似乎是构建UI组件的一种非常强大的方法.
有没有办法用Windows.UI命名空间/控件/ UI框架做同样的事情 - 在F#interactive中加载一些程序集并动态实例化UI组件?
我天真地尝试引用似乎相关的文件:
#r @"C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.UniversalApiContract\2.0.0.0\Windows.Foundation.UniversalApiContract.winmd"
#r @"C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.FoundationContract\2.0.0.0\Windows.Foundation.FoundationContract.winmd"
Run Code Online (Sandbox Code Playgroud)
...这样做可以让我智能地进入Windows.UI命名空间.但是当我尝试实例化时:
Windows.UI.Xaml.Application()
Run Code Online (Sandbox Code Playgroud)
我明白了:
error FS0193: Could not load file or assembly 'file:///C:\Program Files …
System.Diagnostics.Debugger.Break(); // this DB call has not been mocked
Run Code Online (Sandbox Code Playgroud)
我想将它们放在 I/O 调用站点的代码中,以便让单元测试开发人员知道他们何时没有正确模拟调用,但我不想产生新问题。
编译为 Release 时,调用 Debugger.Break() 是否有任何影响?
执行调试构建但不调试时有什么影响吗?