jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Support Uniform Function Call Syntax

Open kuchta opened this issue 2 years ago • 6 comments

It would be awesome if jakt supports UFCS

kuchta avatar Aug 26 '22 20:08 kuchta

Is this also a call for currying and/or partial application? (Would also be nice if possible).

konradekk avatar Aug 26 '22 22:08 konradekk

No, this actually doesn't have much to do with functional programming but it definitely helps to use the functions in a way that is more apropriate for the task. It's also great for extending the class/struct behavior outside of the class/struct definition...

kuchta avatar Aug 26 '22 23:08 kuchta

If traits (parametric type system) are landed, the unified function call is for free because you can determine the set of names to look up even in a generic function. ~~That's how Rust does it as well.~~

zhihaoy avatar Sep 19 '22 18:09 zhihaoy

@zhihaoy How exactly do you do it in Rust? I'm not aware that Rust can use free functions as methods...

kuchta avatar Sep 21 '22 16:09 kuchta

I was mistaken by how Rust uses this term, it's something different... Anyway, let me explain the idea of how traits/protocol can help. With trait,

trait Reader {
    fn Next(&self) -> String;
}

Right now you read self as something special. It doesn't have to be, not in Haskell's typeclass, for example. You can understand a trait as a concept that the type that implements it must satisfy, while each function is just a free function that lets you do

let tok = Next(reader);

And because a type implements Reader, you can now call reader.Next() as if impl works as adding an extension method - only in the context of generic function like this

fn foo<T: Reader>(reader: &mut T) -> ()
{
    let tok = reader.Next();

zhihaoy avatar Sep 22 '22 03:09 zhihaoy

It looks like golang interfaces a little…? 🤔

(Those are pretty neat in my opinion; itʼs even a little bit more: the compiler finds whether trait/protocol is met and takes care of resolving the type of a value)

konradekk avatar Oct 06 '22 23:10 konradekk