My suggestion to Element

Yes, extensions cannot add stored instance properties, even in Apple Swift.

Good to know, will adjust.

Thank you again for the headsup

Can Swift do pattern matching for collections like head:tail?

No.

Here are the Apple Swift pattern docs.

There are half way solutions that can be defined, but no general solution as custom patterns cannot currently introduce variables.

The 2 general categories of half way are either extract into a tuple before matching or matching with a function (cannot create variables).

// extract into a tuple
extension Collection {
  var headTail: (Element, SubSequence)? {
    // possible implementation for Apple Swift
    self.first.map { ($0, self[self.index(after: self.startIndex)...] }
  }
}
switch aCollection.headTail {
  case let (head, tail)? : break
  default: break
}

matching with a function seems to not work in Silver, but here is an example:

// replace <Pattern> and <Value> with concrete types or generic parameters
func ~= (pattern: <Pattern>, value: <Value>) -> Bool {
    return // does pattern match value?
}
// e.g.
func ~= (pattern: Bool, value: Int) -> Bool {
    return (value != 0) == pattern
}
switch 5 {
  case true: print("gets here")
  case false: break
// This is still necessary in Apple Swift,
// because custom `~=` implementations are not 
// required to be inlined before reachability diagnostics:
default: fatalError("shouldn't be reachable")
}
1 Like

Thank you very much, and hats off to you.

Does RemObjects support C# 9 yet?

Will Oxygene support similar C# 9 feature in Patterns, and Immutable Record?

I’m working on record support. Most of the pattern support is already in but I think there’s 1 or 2 missing.

Oxygene, C# or both?

C# to start with. Not sure how to expose it in pascal yet. Possibly with an aspect.

If it is C#, is it for .NET only, or will cover Island platform too?

Majority of C# 9 new features are functional style, seems they borrow from F#. If Oxygene supports those (e.g, Patterns and switch expression) that would be great (making Oxygene an alternative to F# in some sense)

FTR: C# Evolution

Where ever feasible we will add these features for all platforms, and for all (applicable) languages.

Yes. And I hope we could have these (pattern-related C# 9 features) available to Oxygene - again that makes Oxygene a nice alternative to F# (Oxygene .NET) and OCamel (Oxygene Island for native). But the sad reality is your guys are too busy to cater for this type of request (that may be of interest to only a very small group of users /user)

@wuping so far we always tried to add features to all languages and platforms, where possible. Usually it’s the syntax itself that’s the trickiest part but afaik there are no major gaps.

Any specific pattern matching features you would like to see in oxygene?

I would like to have the following, if possible, for both Oxygene .NET, and Island

  1. var Pattern

bool IsJanetOrJohn (string name) => name.ToUpper() is var upper && (upper == "JANE" || upper == "JOHN");

  1. Relational Patterns

    string GetWeightCategory(decimal bmi) => bmi switch {
    < 18.5m => “underweight”,
    < 25m => “normal”,
    < 30m => “overweight”,
    _ => “obese”
    };

  2. Pattern Combinators

    bool IsJanetOrJohn(string name) => name.ToUpper() is “JANET” or “JOHN”;
    bool IsVowel(char c) => c is ‘a’ or ‘e’ or ‘i’ or ‘o’ or ‘u’;
    bool IsLetter(char c) => c is >= ‘a’ and <= ‘z’ or >= ‘A’ and <= ‘Z’;

  3. Tuple and Positional Patterns

    string Print(object obj) => obj switch
    {
    Point (0, 0) => “Empty point”,
    Point(var x, var y) when x == y => “Diagonal”
    };

  4. Property Patterns

    bool ShouldAllow(Uri uri) => uri switch
    {
    {Scheme: “http”, Port: 80} => true,
    {Scheme: “https”, Port: 443} => true,
    {Scheme: “ftp”, Port: 21} => true,
    {IsLoopback: true} => true,
    _ => false
    };

Some of these in see as being tricky to fit in well with Oxygene’s syntax structure, but we can/will have a look…

Ignoring the “method shortcut” syntax in these example, which just goes to make these more unreadable as they already were…

#1 and #3 are largely covered by “in”, at least for the simple cases. I could see a caae for maybe extending that syntax in ways

#2 could be a nice and clean extension to the exiting case expressions, and I see no downsides to adding that, for now.

#4 and #5 I TBH don’t see a place for in Oxygene, if we don’t want to devolve it into the unreadable mess that is F# or Swift (or now, I guess C#). Those are just terrible, terrible ideas.

FWIW, IMHO:

F# is a functional programming language; Oxygene is not. there’s no way to make Oxygfene into an alternative for F#, without fundamentally breaking Oxygene. (the same could be said for C# and Swift; except that I guess they don’t care, and that’s not our call ;). This is a bit like saying, taking the left two wheels off of your car would make it great alternative to a motorcycle. Nope, it would just make it a (really) shitty car. :wink:


Not listed here, one pattern I would like to see an equivalent for (but don’t have a good idea, aside from “copy the shitty C# syntax”) is “if (x is SomeType y) y.SomeMember”…

4 and 5 are the best part of pattern matching if possible with Oxygene.

Don’t mean we want to make Oxygene a pure functional language — just like to see it a multi-paradigm language that support functional style.

Oh yes I have been wanting that forever :slight_smile:

1 Like

I must admit that is a really good one.

1 Like

That was funny! :smiley: Enjoyed reading this thread.

1 Like