F#:我可以在调用基类的构造函数中使用 try/with 吗?

发布于 2024-10-29 12:11:16 字数 1600 浏览 1 评论 0原文

我是一名新手 F# 程序员,在正确获取 F# 程序的语法时遇到了一些困难。

本质上,我想将这个 C# 代码转换为 F#:

class MyRiskyObject : BaseObject
{
    private string field;

    public MyRiskyObject(object foo, string data)
        : base(foo)
    {
        try
        {
            this.data = RiskyOperation(data);
        }
        catch (ArgumentException)
        {
            DoSomethingElse();
        }
    }
}

到目前为止,

type MyRiskyObject
    inherit BaseObject

    val field : string

    new (foo:object, data:string) = {
        inherit BaseObject(foo)
        try
          field = RiskyOperation()
        ????????
    }

我只是无法获得正确的语法......


编辑:

这是我正在处理的实际代码:

type RegExpObject = 
  inherit CommonObject

  val RegExp : Regex
  val Global : bool

  member x.IgnoreCase:bool =
    (x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase

  member x.MultiLine:bool =
    (x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline

  new (env, pattern, options, global') =
    {
      inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp) 

      // Here, I need to catch the exception, and instead call RaiseSyntaxError.
      RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)

      Global = global'
    }
    then RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)

  new (env, pattern) = 
    RegExpObject(env, pattern, RegexOptions.None, false)

I'm a newbie F# programmer, and I'm having some trouble getting the syntax for my F# program correct.

Essentially, I want to turn this C# code into F#:

class MyRiskyObject : BaseObject
{
    private string field;

    public MyRiskyObject(object foo, string data)
        : base(foo)
    {
        try
        {
            this.data = RiskyOperation(data);
        }
        catch (ArgumentException)
        {
            DoSomethingElse();
        }
    }
}

I, so far, have something like

type MyRiskyObject
    inherit BaseObject

    val field : string

    new (foo:object, data:string) = {
        inherit BaseObject(foo)
        try
          field = RiskyOperation()
        ????????
    }

I just can't get the syntax right...


Edit:

here is the actual code I'm working on:

type RegExpObject = 
  inherit CommonObject

  val RegExp : Regex
  val Global : bool

  member x.IgnoreCase:bool =
    (x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase

  member x.MultiLine:bool =
    (x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline

  new (env, pattern, options, global') =
    {
      inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp) 

      // Here, I need to catch the exception, and instead call RaiseSyntaxError.
      RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)

      Global = global'
    }
    then RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)

  new (env, pattern) = 
    RegExpObject(env, pattern, RegexOptions.None, false)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-11-05 12:11:16

为什么不将实际工作委托给一个单独的自由函数,这样工作就不必直接在类的构造函数中完成?

let createRegex pattern options =
  try
    Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)
  with
  | :? System.ArgumentException -> RaiseSynaxError ()

那么你的类(如你的编辑中所示)将是:

type RegExpObject(env, pattern, options, global') //'
  inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp)

  let RegExp = createRegex pattern options
  let Global = global' //'

  member x.IgnoreCase =
    (x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase

  member x.MultiLine =
    (x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline

  new (env, pattern) = RegExpObject(env, pattern, RegexOptions.None, false)

这里的一个优点是,正如 @Brian 所指出的,不需要使用 val ,从而使类定义更加清晰。

Why not just delegate the real work to a separate free function, so the work doesn't have to be done directly in your class' constructor?

let createRegex pattern options =
  try
    Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)
  with
  | :? System.ArgumentException -> RaiseSynaxError ()

Then your class (as demonstrated in your edit) would be:

type RegExpObject(env, pattern, options, global') //'
  inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp)

  let RegExp = createRegex pattern options
  let Global = global' //'

  member x.IgnoreCase =
    (x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase

  member x.MultiLine =
    (x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline

  new (env, pattern) = RegExpObject(env, pattern, RegexOptions.None, false)

An advantage here is that, as @Brian indicated, no use of val would be necessary, making the class definition much cleaner.

℡Ms空城旧梦 2024-11-05 12:11:16

尝试以下

type MyRiskyObject(foo : obj, data : string) as this =
    inherit BaseObject(foo)

    let mutable data = data;

    do
        try
            data <- this.RiskyOperation data 
        with 
            | :? System.ArgumentException -> this.DoSomethingElse()

具有不可变 let 绑定的替代示例,其中 RiskOperationDoSomethingElse 不是 MyRiskObject 的成员

type MyRiskyObject(foo : obj, data : string) =
    inherit BaseObject(foo)

    let data = 
        try 
           OtherModule.RiskyOperation data
        with
        | :? System.ArgumentException -> 
           OtherModule.DoSomethingElse()
           data

Try the following

type MyRiskyObject(foo : obj, data : string) as this =
    inherit BaseObject(foo)

    let mutable data = data;

    do
        try
            data <- this.RiskyOperation data 
        with 
            | :? System.ArgumentException -> this.DoSomethingElse()

Alternate example with non-mutable let binding where RiskOperation and DoSomethingElse are not members of MyRiskObject

type MyRiskyObject(foo : obj, data : string) =
    inherit BaseObject(foo)

    let data = 
        try 
           OtherModule.RiskyOperation data
        with
        | :? System.ArgumentException -> 
           OtherModule.DoSomethingElse()
           data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文