Why do we need a "for" clause in an interface instance declaration?
An interface instance clause
interface instance MyInterface for NameOfTemplate where
can only occur inside of a template definition, and it can only refer to the same template. It seems redundant, or do you envision lifting the second restriction?
module Main where
template T with
s: Party
where
signatory s
data View = View { a : Int }
deriving (Show, Eq)
interface I where
viewtype View
interface instance I for T where
view = View 0
Sure but for the instances inside a template, it is redundant?
I think the point is that the syntax as it exists right now is independent of whether it’s in a template or an interface.
That means you can move it around easily while refactoring, and you don’t have to learn two different syntaxes for the same thing.
That’s clearly not the only way to do it, and interfaces are still in preview so there’s time to change them to some extent I think, but I personally like that “location independence” property.
(Note that by the same argument the interface name is also redundant when the block appears within an interface.)
Gary is right, the syntax was chosen such that we can use the the same syntax regardless of whether it is in an interface or a template and in return we accepted some redundancy.
Ok, cool, that’s sensible.
I guess I would have preferred a shorter version of
interface instance [I|T]...
to keep things terse, but I could understand how it complicates things downstream.
Thinking about this
That means you can move it around easily while refactoring, and you don’t have to learn two different syntaxes for the same thing.
a little bit more. I think this cuts both ways. In a large code base you would want to have the ability to quickly search for the difference.
To support both approaches, what if we make the redundant parts optional? So that in an template I can write
interface instance MyInterface where
and in an interface I can write
interface for MyTemplate where
?