On Fri, 3 Feb 2023 at 04:28, Bram Moolenaar <b...@moolenaar.net> wrote:

>
> Doug Kearns wrote:
>
> > I was just looking at the constructor documentation and noticed that the
> > default constructor includes parameters for each field in the order they
> > were declared.  Having to change all the constructor call sites when
> > reordering field declarations seems like a probable source of bugs,
> > particularly when switching fields of the same type.  This seems like a
> > feature that really needs named-parameter support.
>
> Good point.  Although I doubt it would happen often, it can be difficult
> to figure out what went wrong when it does happen.
>
> What we could do:
>
> 1. Do not change how it works, add remarks to the help warning for
>    changing the member ordering.
>
> 2. Make the default new() method not accept arguments.  The caller must
>    set the fields after calling new().  This is safe, but requires
>    several extra lines.
>         var obj = Class.new()
>         obj.line = 5
>         obj.column = 0
>

This is probably the 'standard' approach.


> 3. Add a mechanism to name the members in the call, e.g.
>         var obj = Class.new(line = 5, column = 0)
>    This is similar to 2. but puts it in one line.
>    It requires marking a method to not use positional arguments.a
>
> Although 3. looks like a good solution, it requires implementing a new
> mechanism.  And disallowing positional arguments might be considered
> weird, I cannot think of a language that has this functionality.
>

Swift does this for structs and calls them memberwise initializers.  It
creates a default initializer (constructor) with named fields if no
initializer is otherwise specified.

struct Foo {
  var x: Int
  var y: Int
}
let bar = Foo(x: 1, y: 2)

Classes don't currently create the default memberwise initializer (there's
a proposal for this) but named parameters are either specified or created
automatically for declared initializers.

Swift actually defaults to named parameters and the mechanism is a little
more elaborate than usual as argument labels can be specified that differ
from the parameter names.   Unnamed parameters are allowed but these must
be explicitly specified with an argument label of "_".

I've never used Swift in anger; it was just the first search result of
interest.  Here are the details -
https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

Regards,
Doug

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/CAJ1uvoCakFg4TaHuF_w-f76kFNygeTypcppF7XaXDF0ZAzg_BA%40mail.gmail.com.

Raspunde prin e-mail lui