I suggest we ...

Types with default initial values specified (from Ada 2012)

I suggest introducing an aspect that can be applied to declarations of enumeration types, discriminated union types and user-defined numeric types with the aim of specifying their default initial values in code easily.

This suggestion is an extension of my previous suggestion:
Types with predicates to create subtypes easily (from Ada 2012)
Please see it for more details:
https://fslang.uservoice.com/forums/245727-f-language/suggestions/12564486-types-with-predicates-to-create-subtypes-easily-f

Example # 1:

type OK = bool [with] default true // type with true as its default initial value

Example # 2:

type Animals =
| Tiger
| Lion
| Cat
| Mouse
| Hamster
[with] default Cat

type Prey = Animals
with function
| Mouse
| Hamster -> true
| _ -> false
[and] default Hamster

let a : Prey array = Array.zeroCreate 3 // array of 3 hamsters

Remark: "[and]" or "[with]" means that the "and", "with" keywords are optional in the above-mentioned lines of code.

It's not a breaking change in the language; no new keywords are needed to implement this syntax in the F# language. I think this syntax will help represent a programmer's intent in code and improve the expressiveness of the F# language.

3 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Alexei Odeychuk shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

2 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Richard Gibson commented  ·   ·  Flag as inappropriate

    This would be great, especially if the F# library would come with a function similar to `genericOne` and we can use this as a static type constraint.

    That way you could write a generic function that works on any type that defines a default value.

  • Jack Fox commented  ·   ·  Flag as inappropriate

    I suggest making the "and" and "with" keywords mandatory...I think it improves readability.

F# Language

Feedback and Knowledge Base