#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

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 }

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!

go4 Mon 25 Jun

Thanks. The full list at here. Any comments about these new features.

SlimerDude Fri 29 Jun

Hi Go4, I have nothing but praise for your continued work on enhancing Fantom!

Many of your new features are very close to my own aspirations for Fantom, including:

  • Portable Library - The sys pod is written in Fantom with little native code.
  • Return From Void - FanCore not allow return a value from Void method, except return a Void type.
  • Build Script - Using pod.props - Andy may address this in his upcoming build posts.

I even like the little enhancement of defaulting fractional numbers to Floats (Float Literals) and have often wondered why this is not the default.

And of course, the major ones such as Generics and Extension Methods are fantastic!

The Data classes may be nice syntactic sugar but (as I've said in the past) I believe that if the compiler were little more open and allowed for extensions, then these could be user plugins and need not be part of Fantom core.

It all looks very promising, great work!

Steve.

brian Fri 29 Jun

@go4: agree with Steve, its very awesome stuff. I keep stumbling across how nice a data/struct class would be with conveniences auto-generated for equals, hash, etc

I even like the little enhancement of defaulting fractional numbers to Floats (Float Literals) and have often wondered why this is not the default.

Steve: see post 201 for how that evolved

go4 Sat 30 Jun

Thanks. I intend to release a dialect version of Fantom when everything is done, if allowed by the license.

brian Sun 1 Jul

Thanks. I intend to release a dialect version of Fantom when everything is done, if allowed by the license.

The license would definitely allow that. We'd just want to be clear on terminology so people don't get confused

ahhatem Tue 3 Jul

May I ask why will it be kept as a separate project? Why not enhance it and merge it into the main project?

go4 Tue 3 Jul

Hi Steve, Thank you for praising. The build script idea seems to be from your past post.

@ahhatem I don't think it can be merged. There are many break changes and most of the code was rewritten. My original purpose is not just to enhance the grammar that is the need to write sys pod for a new backend VM.

Login or Signup to reply.