Overriding static properties in F# with extension methods -
f# extension methods can defined on types same name , type signature existing instance , static methods override default implementation of these methods, can't work on static properties.
in particular, i'm trying create extension method datetime
returns more precise time follows:
#nowarn "51" open system module datetimeextensions = open system.runtime.interopservices [<dllimport("kernel32.dll", callingconvention = callingconvention.winapi)>] extern void private getsystemtimepreciseasfiletime(int64*) type system.datetime // example showing static methods can overridden static member isleapyear(_: datetime) = printfn "using overridden isleapyear!" true // more accurate utcnow method (note: not supported older os versions) static member utcnow = printfn "using overridden utcnow!" let mutable filetime = 0l getsystemtimepreciseasfiletime(&&filetime) datetime.fromfiletimeutc(filetime)
however, output when executing
open datetimeextensions let _ = datetime.isleapyear(datetime.utcnow)
is just
using overridden isleapyear!
which shows static method 'override' working, not static property.
(note: i'm using f# 4.0)
this statement seems incorrect:
f# extension methods can defined on types same name , type signature existing instance , static methods override default implementation of these methods, can't work on static properties.
no, don't override.
you might confused because in fact signature of isleapyear
wrong, should take integer, that's why works mean not overriding anything, adding new (extension) method.
if try original signature you'll see doesn't work either:
type system.datetime // example showing static methods can not overridden static member isleapyear(_: int) = printfn "using overridden isleapyear!" false > datetime.isleapyear(2000);; val : bool = true
which consistent behavior of static property extension.
anyway i'm not sure why decided not override, if there such decision @ when designing language. think interesting feature , if there reason not implement @ least should emit warning saying since method exists never called.
maybe open issue or suggestion f# compiler.
Comments
Post a Comment