dotnet / dotnet/vblang

Method cascading (like Dart) -- enables Fluent interfaces without any extra effort

Open
#245 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

`obj.Method()` -- returns result of `Method()`
`obj..Method()` -- discards result of `Method()` (if any) and returns `obj`
Therefore, you can use method chaining: `obj..Method()..SecondMethod()..ThirdMethod()` ...without having to create specific Fluent-style methods that return the object.

# More information at the Dart website
- https://news.dartlang.org/2012/02/method-cascades-in-dart-posted-by-gilad.html
- Also: https://www.dartlang.org/guides/language/language-tour#cascade-notation-

# Benefits
- Benefit for class developers -- no need to specially create specific Fluent-style methods; simpler code.
- Benefit for class consumers -- ability to use Fluent-style access on classes that have not been coded for it.

# Setting properties
- Dart also allows setting properties with the same syntax, e.g. `object..Property = "value"` and then on the next line `..NextProperty = "value"`.
- This may not fit in with VB being statement-separated.
- We could do it the same way and decide that `..` at the beginning of a line continues the previous line. _(In any case these days VB's allow unfinished statements to implicitly continue onto new lines. This would be the same thing the other way around.)_
- Or, we could use a different syntax: `object..Property("value")..Property("value")`. It isn't as clear to the newcomer that this is a property assignment, but it does preserves the one-statement-per-line rule. _(Parameters for parameterized properties would be in the parentheses before the new value.)_

# Real-world example of where this would be beneficial

[Example class in my comment below](#issuecomment-362044283).

# Symbol/syntax for this feature

Thought Dart uses `..` and it looks nice and clean, we were discussing perhaps reserving `..` for the Pipe-Forward operator (#165 - and related functionality - see #154). If so, we would need to find a different symbol for this.
Maybe `.<` to show that the expression will return what's on the left side?

New idea: I am thinking that perhaps we could put the cascaded method call in parentheses: `obj.(Method).(MethodTwo(5))`. This way, 1) it looks like a regular method call, 2) the syntax expresses visually the concept that the method is being called "parenthetically" but its result is not used in the expression. There is also no amibiguity because `.(` is not permitted anywhere else.

Thanks for such a great, powerful and yet friendly language. Looking forward to the discussion...

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.