#1711 Anonymous classes and multiple function results

Throwable Mon 28 Nov 2011

Hi, First of all I want to say thanks to all developers of language. Like a most of you I'm unhappy with Java and even more unhappy with Scala and I'm searching for a language/platform that allows to do things in more clear and explicit manner. And I have been very impressed with Fantom, ideas it implements and its usability.

I want to propose to extend language with functions which return multiple results. Normally it may be done by returning a list of values or creating a new class. But the first method is type-unsafe and the second is requires a lot of boilerplate when you need to use this structure only once. I propose to implement it via anonymous inner classes: t := { Str name, Int age } - defines anonymous class. c := { name="Peter", age=25 } - creates an instance of anonymous class with two fields Str name and Int age. Anonymous classes are always immutable and final.

With anonymous classes a function |Int id -> Str name, Int age| may be converted to |Int id -> {Str name, Int age}| that returns an instance of anonymous class. It allows to access result fields in type-safe manner:

c := getUserData( someId ) echo( "${c.name}, ${c.age}" )

Thanx, Antón

qualidafial Mon 28 Nov 2011

Welcome, Antón!

I think I understand your proposal, but I'm not sure what you want to accomplish with it. Could you give examples of where anonymous inner classes would be preferable to a traditional const class?

brian Mon 28 Nov 2011

Hi @Throwable - welcome to Fantom and nice username :-)

What you suggested is along the lines of C# anonymous classes and I really like what they have done in C# especially in support of LINQ. Although the idea of multiple return types can also be tackled with parameterized tuples or maybe just even simpler compiler support for statically multiple return types. We have had much discussion around the ideas of tuples, which is something I sort of lean towards. But I think a final decision for generics would need to be addressed first, and we are just sort of kicking that idea around right now.

Login or Signup to reply.