{"id":23052683,"url":"https://github.com/wk-j/fsharp-extension-property","last_synced_at":"2025-08-07T14:34:02.795Z","repository":{"id":94332811,"uuid":"57883153","full_name":"wk-j/fsharp-extension-property","owner":"wk-j","description":null,"archived":false,"fork":false,"pushed_at":"2016-05-02T13:17:47.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T18:22:55.566Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"F#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wk-j.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-02T10:44:55.000Z","updated_at":"2017-02-11T14:43:25.000Z","dependencies_parsed_at":"2023-04-29T15:01:24.945Z","dependency_job_id":null,"html_url":"https://github.com/wk-j/fsharp-extension-property","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wk-j%2Ffsharp-extension-property","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wk-j%2Ffsharp-extension-property/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wk-j%2Ffsharp-extension-property/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wk-j%2Ffsharp-extension-property/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wk-j","download_url":"https://codeload.github.com/wk-j/fsharp-extension-property/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246934548,"owners_count":20857232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-16T00:14:23.766Z","updated_at":"2025-04-03T04:21:48.111Z","avatar_url":"https://github.com/wk-j.png","language":"F#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Extension Method / Property\n\n### เขียน Extension method เพิ่มใช้งานใน `F#`\n\n```fsharp\n[\u003cAutoOpen\u003e]\nmodule ExtensionMethodOnlyInFSharp =\n    type System.String with\n        member this.IsFSharp() = this = \"F#\"\n        member this.``IsF#`` with get() = this = \"F#\"\n\nprintfn \"Equal to F#? %A\" (\"F#\".IsFSharp())\nprintfn \"Equal to F#? %A\" (\"F#\".``IsF#``)\n```\n\n- สร้างสร้าง Extension method / property ของ `String` โดยใช้ Keyword `with`\n- `IsFSharp()` เป็น Extension method เรียกใช้ผ่าน `.IsFSharp()`\n- `IsF#` เป็น  Extension property เรียกใช้ผ่าน `.IsF#` โดยไม่ต้องเติม `()`\n\n### เขียน Extension method ให้สามารถเรียกใช้งานจาก `C#`\n\n```fsharp\n[\u003cAutoOpen\u003e]\nmodule ExtensionMethodForCSharp =\n    [\u003cSystem.Runtime.CompilerServices.ExtensionAttribute\u003e]\n    let IsFSharp(str: string) = str = \"F#\"\n    [\u003cSystem.Runtime.CompilerServices.ExtensionAttribute\u003e]\n    let ``IsF#``(str: string) = IsFSharp str\n```\n\n - ต้องเพิ่ม `[\u003cSystem.Runtime.CompilerServices.ExtensionAttribute\u003e]`\n - Extension property `IsF#` ไม่สามารถเรียกใช้งานจาก `C#` ได้\n\n### Extension method ใน Array\n\n```fsharp\ntype 'T``[]`` with\n    member this.MyExtension with get() = \"My Extension\"\n\nlet x: string array = [||]\nx.MyExtension\n```\n\n### Extension property กับ Literal type\n\n```fsharp\ntype System.Int32 with\n  member x.Months = DateTime.Today.AddMonths(x) - DateTime.Today\n  member x.Years = DateTime.Today.AddYears(x) - DateTime.Today\n\ntype System.Double with\n  member x.Days = DateTime.Today.AddDays(x) - DateTime.Today\n\ntype System.TimeSpan with\n  member x.Ago = DateTime.Now.Add(-x)\n  member x.FromNow = DateTime.Now.Add(x)\n  member x.FromToday = DateTime.Today.Add(x)\n\nlet _20YearAgo = DateTime.Now - 20 .Years\nlet _50YearAgo = 50 .Years.Ago\nlet _100YearFromNow = DateTime.Now + 100 .Years\n```\n\n- สามารถใช้ `Space .\u003cExtensionProperty\u003e` แทน `().\u003cExtensionProperty\u003e`\n- เช่น ใช้ `100 .Years` แทน `(100).Years`\n\n## Link\n\n- http://blog.stermon.com/articles/2014/03/06/fsharp-surprisingly-expressive\n- http://apollo13cn.blogspot.com/2012/08/f-extension-methods.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwk-j%2Ffsharp-extension-property","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwk-j%2Ffsharp-extension-property","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwk-j%2Ffsharp-extension-property/lists"}