#2677 Experimental syntax extension

go4 Sat 27 Jan 2018

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 2018

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

andy Mon 29 Jan 2018

Cool!

go4 Mon 25 Jun 2018

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

SlimerDude Fri 29 Jun 2018

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 2018

@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 2018

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

brian Sun 1 Jul 2018

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 2018

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 2018

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.

go4 Tue 11 Sep 2018

The dialect now called Fanx.

I'm pleased to say that v1.0 alpha released.

Downloads

The fanx make many syntax enhancements and API changes from Fantom. It's not compatible with Fantom's fcode(.pod file format).

Different from Fantom

Thanks.

SlimerDude Wed 12 Sep 2018

Hi go4, this looks really good!

I was looking through the Different from Fantom document and there are some great ideas, e.g. I like using return in it-blocks. Though some things can be done in pure Fantom already, e.g. the ActorProxy is no different to my SynchronizedState class.

It's shame about the binary compatibility, but understandable.

In all, how exciting! I don't know where you find the time for all this!

go4 Wed 12 Sep 2018

Thanks.

return in it-blocks

It's return in closures not it-blocks. the return keyword will be deprecated.

the ActorProxy is no different to my SynchronizedState

The ActorProxy return Future not the result.

It's shame about the binary compatibility

The fcode has been changed to keep generics type informations.

My favorite feature is Closure Inference. To omit the closure signature as much as possible. That Fantom limit is the closure only take one param and return Void.

brian Wed 12 Sep 2018

This is very interesting! I think its great to experiment with different features.

There are a couple things in there I would like to incorporate into Fantom.

Some questions:

  1. How difficult was it to incorporate to generics into compiler?
  2. Are generics reified? Can you see them in reflection APIs?
  3. What exactly are you doing for Structs for pass-by-value? Is this for future JVM support?

Couple other comments:

  • we used to have a readonly keyword, but got rid of it - it was less like final, and more a shortcut for { private set }

go4 Thu 13 Sep 2018

The Fantom compiler already have code about generics. But it's still difficult for me. It took me a week to fix bug in compiler code.

It's type erasure in generics. You can't see them from reflection API.

The JVM don't support pass-by-value. It's just for future other backend.

Ilove:= Mon 11 Feb 2019

Look pretty cool. But what about IDE support if the new extensions are actually added? F4 was not updated for a long time. Without the help of IDE auto completion | suggestion it's a lot difficult for people like me :(

Login or Signup to reply.