English 中文(简体)
是否可以“简单超负荷”?
原标题:Is "implicit overloading" possible?

我举以下例子:

type Stream (capacity) =
    let data = Array.zeroCreate capacity
    member private s.position = ref 0
    static member private encoder = new Text.UTF8Encoding()
    static member private write (x, o, a : byte[]) = for i = 0 to 3 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256)
    static member private write (x, o, a : byte[]) = for i = 0 to 1 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256s)
    static member private write (x : string, o : int, a : byte[]) = Stream.encoder.GetBytes(x, 0, x.Length, a, o)
    static member format (x : int, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a
    static member format (x : int16, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a
    static member format (x : string, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a

首先,对可怕的迷魂药法感到担忧,在F#中,我只开一个开端。 由于您可能看到三个<代码>format上的超载仅因其论点类型而有所不同,而其体体与物体相同(尽管称“代码”的超重载)。 是否有可能将格式功能降低到一个,也许会放在哪里?

我对Im完全没有在此问题上表示歉意,但我无法找到关于这一问题的许多信息。

最佳回答

我认为,你写这封信的方式可能是最好的选择。

如果你不想打上箱子,那么,毫无疑问需要超负荷的<代码>write功能,因为对于不同类型来说,序列化需要以不同的方式实施。 采用<条码>的<>条码/代码>的成员也在此例中做了t 工作,因为<条码> 功能只能要求有特定情况或静态方法on。 这种方法具有一定价值,但并非具体超载。

避免某些重复的合理解决办法是,只界定一个超负荷功能(即write),然后将其明确列为需要的其他职能的理由。 您可以撰写如下文章:

type Stream (capacity) = 
    let data = Array.zeroCreate capacity 
    member private s.position = ref 0 
    static member private encoder = new Text.UTF8Encoding() 
    static member Write (x, o, a : byte[]) = 
        for i = 0 to 3 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256) 
    static member Write (x, o, a : byte[]) = 
        for i = 0 to 1 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256s) 
    static member Write (x : string, o : int, a : byte[]) = 
        Stream.encoder.GetBytes(x, 0, x.Length, a, o) |> ignore
    // Format takes a writer function  f  as the first argument
    static member Format (x, s) f = let a = Array.create s 0uy in f(x, 0, a); a 

// When you call  Format  with  Stream.Write  as an argument, 
// the compiler chooses the right overload for you
Stream.Format (1s, 100) Stream.Write 
Stream.Format ("foo", 100) Stream.Write 

这避免了定义方面的一些法典重复,但使使用时间更长。 如果你不需要诸如<代码>Format等许多职能,那么最好的办法可能是确定超负荷,与你原先的代码重复。

关于<条码>的<>条码/代码>,你可以指出,某种论点应当执行某些特定成员(或静态),但你不能说应当有具体的超载。 如果你写了所有三种类型的包装(int16/code>,string, ......)的固定成员Write,那么你可以写:

let inline format< ^T when ^T : 
    (static member Write : ^T * int * byte[] -> unit)> (value:^T) size =
  let a = Array.create size 0uy
  (^T : (static member Write : ^T * int * byte[] -> unit) (value, 0, a)) 
  a

......但这种更加复杂的解决办法也需要在“<条码>上<>格式>/代码”时再写一些法典,因此,我不会真正使用这一方法(但知道这种做法存在可能是有益的)。

问题回答

Refactoring format functions is obvious; you can create a generic format function and pass different Stream.write functions as parameters. First, put the generic function before any member of the class:

    static let formatGeneric writeFunc x s =
           let a = Array.create s 0uy         
           writeFunc(x, 0, a)         
           a

(a) 采用并使用这一系统,以履行不同的形式职能:

    static member format (x, s) = 
           formatGeneric (fun (x: int, i, a) -> Stream.write(x, i, a)) x s
    static member format (x, s) = 
           formatGeneric (fun (x: int16, i, a) -> Stream.write(x, i, a)) x s
    static member format (x, s) = 
           formatGeneric (fun (x: string, i, a) -> Stream.write(x, i, a)) x s

<代码>Stream.write的通知全文和类型的x是选择适当超载的附加说明。





相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签