mauro3 / Traits.jl

Exploration of traits in Julia
Other
39 stars 6 forks source link

[WIP] first stab at parametrically-typed trait (e.g. monad) #4

Closed tonyhffong closed 9 years ago

tonyhffong commented 9 years ago

Very experimental. Do not merge.

Just trying to get this out to get some feedback.

The key new thing is that trait definition allows another layer of curly, i.e.

@traitdef MyParamTrait{X{Y}} begin
...
end

So that any datatype supplied must be parametric. How do we get Y in actual usage? we have two choices

I'm not sure which one is more kosher, and how to switch to the other one if we want to (a syntax issue).

Also, istrait currently cannot successfully test if a parametric datatype can satisfy a trait. There is still work to do.

I have made a simple "Nullable monad" example using this framework.

mauro3 commented 9 years ago

This looks good, although I haven't quite grasped all its implications. One nit-pick, should it be:

@traitimpl Tr{ Array{T,N} -> {T,T} } begin
    f{T}( x::Array{T,1}, y::T ) = sum(x)+y
end
tonyhffong commented 9 years ago

julia these days don't like the :( {T,T} ) construct:

WARNING: deprecated syntax "{a,b, ...}".
Use "Any[a,b, ...]" instead.

But you raised an interesting point, in the opposite direction: would it be easier to read if we make the parameter in parenthesis or square bracket? Tr{ X(Y) } or Tr{ X[Y] }

mauro3 commented 9 years ago

You're right about the warning. I was already in the {} means Tuple{} future but this is too ugly:

@traitimpl Tr{ Array{T,N} -> Tuple{T,T} } begin
    f{T}( x::Array{T,1}, y::T ) = sum(x)+y
end

Concerning your question, I think the {} are the right choice though as they are similar to type-parameters.