I suggest we ...

Allow to declare new types inside functions

The idea is inspired by "Voldemont" typed from D language http://wiki.dlang.org/Voldemort_types

In short, you are allowed to define types inside function scope and return instances of such types from the functions. The proposing syntax is as following:

let func x =
type Nested = { Value: int }
{ Value = x }

let nested = func 1
let v = nested.Value

The type of the nested type is inferred as "func.Nested". You cannot add type annotations anywhere in code though:

let func x: func.Nested = // not allowed
type Nested = { Value: int }
{ Value = x }

let nested: func.Nested = func 1 // not allowed

Nested types cannot be used as types of arguments, however it's possible to write function with statically resolved type parameters to safely deal with such types.

In D you can use nested types as types for template function arguments like this:

auto getVoldemont() {
struct Voldemont {
int Value;
}
return Voldemont(1);
}

int func(T)(T voldemont) {
return voldemont.Value;
}

auto voldemont = getVoldemont();
auto value = func(voldemont); // template func instantiation

As for me, the main usecase for nested types is replacement for tuples as return types which would hugely increase code readability.

23 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Vasily Kirichenko shared this idea  ·   ·  Admin →

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      F# Language

      Feedback and Knowledge Base