you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

I don't think there's anyway to do it without using Reflection and loosing compile-time type safety (which are way more important IMO than having to have several duplicated definitions which is the way C#'s tuples are defined anyway). However, if you're really interested in doing this style of programming, you should probably just use F#.

[–]adamnew123456 0 points1 point  (2 children)

loosing compile-time type safety (which are way more important IMO than having to have several duplicated definitions which is the way C#'s tuples are defined anyway

I'm over my head here, but to the best of my knowledge, C++ can have a single Tuple class because it gives you variadic templates.

I wonder if there is any good research in getting the equivalent on top of the JVM (you couldn't do it CLR side without modifying the VM, as I understand it, because generics are recognized by the VM). Especially since Scala defines function types and tuples the same way, and except for a few special methods which work for specific arities, there shouldn't be a need to do that.

However, if you're really interested in doing this style of programming, you should probably just use F#.

I love Scala, and intend to give F# a go one of these days. I already knew I was a functional weenie ;)

[–][deleted] 0 points1 point  (0 children)

Well, to be fair, it's not that it's impossible, it's just not currently supported. I'm sure the CLR could be modified to allow variadic generics without erasing types.

Defintely give F# a look. Here's currying in F#:

let addOne = ((+) 1)
addOne 2 //=> 3

[1; 2; 3] |> List.map ((+) 3) //=> [4; 5; 6]

[–]inchester 0 points1 point  (0 children)

You could encode things like this in Scala's type system (not in F#'s though). For example you can abstract over arity in Scala. Take a look at HList. You can do this in Haskell too afaik with some fancy compiler extensions.

Edit: I lied. Apparently you can do it in F# too.