#2677 Experimental syntax extension

go4 Sat 27 Jan

Hi, I have added some experimental new features to Fantom.

Almost done works:

Generic

The generic implemented by type erasure and cast free(no covariance contravariance limit).

class Foo<T> {
  T? t
  T get() { return t }
}

foo := Foo<Str>()

Extension method

Similar to C#, it providers to add methods out side the class. Extension method must be static and declared with extension keyword.

class Whatever {
  extension static Str[] splitBy(Str str, Str sp, Int max := Int.maxVal) {
    ...
  }
}
str := "->A->B->C->"
//shortcut of Whatever.splitBy(str, "->", 3)
fs := str.splitBy("->", 3)

Struct

A struct type is a value type. Struct type is implied const and final so that JVM can safely ignore the flag.

struct class Point {
  const Int x
  const Int y
}

p := Point{ x=1; y=2 }

Maybe I can do more if you have any other ideas.

the source code.

SlimerDude Sat 27 Jan

Wow go4, that's some really cool progress you've made there with some really exciting enhancements!

andy Mon 29 Jan

Cool!

Login or Signup to reply.