将重载的构造函数添加到隐式F#类型 [英] Adding Overloaded Constructors to Implicit F# Type

查看:140
本文介绍了将重载的构造函数添加到隐式F#类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用隐式类型构造创建了以下类型:

I have created the following type using implicit type construction:

open System

type Matrix(sourceMatrix:double[,]) =
  let rows = sourceMatrix.GetUpperBound(0) + 1
  let cols = sourceMatrix.GetUpperBound(1) + 1
  let matrix = Array2D.zeroCreate<double> rows cols
  do
    for i in 0 .. rows - 1 do
    for j in 0 .. cols - 1 do
      matrix.[i,j] <- sourceMatrix.[i,j]

  //Properties

  ///The number of Rows in this Matrix.
  member this.Rows = rows

  ///The number of Columns in this Matrix.
  member this.Cols = cols

  ///Indexed Property for this matrix.
  member this.Item
    with get(x, y) = matrix.[x, y]
     and set(x, y) value = 
        this.Validate(x,y)
        matrix.[x, y] <- value

  //Methods
  /// Validate that the specified row and column are inside of the range of the matrix.
  member this.Validate(row, col) =
    if(row >= this.Rows || row < 0) then raise (new ArgumentOutOfRangeException("row is out of range"))
    if(col >= this.Cols || col < 0) then raise (new ArgumentOutOfRangeException("column is out of range"))

但现在我需要添加以下重载的构造函数到这个类型(这是C#在这里):

However now I need to add the following overloaded constructor to this type (which is in C# here):

public Matrix(int rows, int cols)
    {
        this.matrix = new double[rows, cols];
    }

我的问题是,看起来任何重载的构造函数在隐式类型必须具有作为第一构造函数的子集的参数列表。显然,我想添加的构造函数不符合这个要求。有什么办法使用隐式类型构造吗?我应该怎么做呢?

The problem that I have is that it seems any overloaded constructors in an implicit type must have a parameter list that is a subset of the first constructor. Obviously the constructor I want to add does not meet this requirement. Is there any way to do this using implicit type construction? Which way should I do this? I'm pretty new to F# so if you could show the whole type with your changes in it I would greatly appreciate it.

提前感谢,

Bob

PS

推荐答案

如果你有任何其他建议,让我的课程更多的功能风格,我可能只是这样做:

I would probably just do this:

type Matrix(sourceMatrix:double[,]) =
  let matrix = Array2D.copy sourceMatrix
  let rows = (matrix.GetUpperBound 0) + 1
  let cols = (matrix.GetUpperBound 1) + 1

  new(rows, cols) = Matrix( Array2D.zeroCreate rows cols )



<复制空数组成为性能瓶颈。)

unless we are talking about very large arrays which are created very often (i.e. copying the empty array becomes a performance bottleneck).

如果你想模拟C#版本,你需要一个显式字段,可以从两个构造函数访问,像这样:

If you want to emulate the C# version, you need an explicit field that can be accessed from both constructors, like so:

type Matrix(rows,cols) as this =

  [<DefaultValue>]
  val mutable matrix : double[,]
  do this.matrix <- Array2D.zeroCreate rows cols

  new(source:double[,]) as this =
    let rows = source.GetUpperBound(0) + 1
    let cols = source.GetUpperBound(1) + 1
    Matrix(rows, cols)
    then
      for i in 0 .. rows - 1 do
        for j in 0 .. cols - 1 do
          this.matrix.[i,j] <- source.[i,j]

BTW,还有一个矩阵类型

这篇关于将重载的构造函数添加到隐式F#类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆