class Type parameters in monax

51 views
Skip to first unread message

Nathan Hüsken

unread,
Jul 7, 2015, 4:58:09 AM7/7/15
to haxe...@googlegroups.com
Hey,

I am trying to write a monad for monax
(https://github.com/sledorze/monax) that uses the tink_core "Surprise"
class.

It all works well, when I assume a fixed type for the Failure (in my
case, String). But I want it to be more flexible, variating the Failure
type with a type parameter.

Now, the "Failure" type of the Surprises is passed as a type Parameter
for the MonadSurprise. So the code starts like this:

class MonadSurprise<F> {
public static function monad<T>(f : Surprise<T, F>) return
MonadSurprise<F>;
...
}

But that does not work, I get this error:

Unexpected ;

Why? Can I not use it this way? Or am I syntacticly doing something
wrong? The function is returning a class, can that not be done with
type-parametrized classes?

Thanks!
Nathan

Juraj Kirchheim

unread,
Jul 7, 2015, 7:50:25 AM7/7/15
to haxe...@googlegroups.com
I still haven't understood monads, but you are trying to put a type where an expression should be.

What the parser sees is `MonadSuprise < F > OPERAND_EXPECTED_HERE`, which is why it tells you there's an unexpected semicolon. You'd have to return something like `new MonadSurprise<F>()` if that works.

Best,
Juraj

Nathan

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Nathan Hüsken

unread,
Jul 7, 2015, 9:55:34 AM7/7/15
to haxe...@googlegroups.com
Hey,

But apparently a function can also return a class, can it not?
I am orienting on the example here: https://github.com/sledorze/monax#learn-the-not-so-hard-way--defining-a-monad-instance

And the monad function there returns a class!
Or did I misunderstand something?

Thanks!
Nathan

Juraj Kirchheim

unread,
Jul 7, 2015, 11:45:10 AM7/7/15
to haxe...@googlegroups.com
A class, yes. Because a class is an actual value. A type in general cannot. For example `Array` is a value of type `Class<Array<Unknown>>` (roughly). But `Array<Int>` is not a value. For example, you cannot compare `Array<Int>` with `Array<String>` at runtime.

More than that, a type parameter belongs to an instance, not a class. Consider this:

class Foo<T> {
   static public function bar():T return null;//Type not found : T
}

What should work is to `return new MonadSurprise<F>()` and then define the methods on the instance, rather than the class. 
This stuff really hurts my brain, but as far as I understand, the monad function can actually return *anything* that has a map/flatMap/ret method of the right signatures.

Best,
Juraj

Nathan Hüsken

unread,
Jul 7, 2015, 1:48:21 PM7/7/15
to haxe...@googlegroups.com
Hey,

OK, I understand, thanks.

With monad returning an instance but a type I run into other problems with monads. But these seem very monax specific, so I opened an issure here: https://github.com/sledorze/monax/issues/12

Best,
Nathan
Reply all
Reply to author
Forward
0 new messages