mirror of
https://github.com/fluencelabs/hackethberlin
synced 2025-04-24 17:02:18 +00:00
Function definitions with Decorators
This commit is contained in:
parent
291e8b70a2
commit
27740833d5
@ -13,7 +13,7 @@ startYear := Some(2018)
|
||||
licenses += ("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt"))
|
||||
//headerLicense := Some(License.ALv2("2018", organizationName.value))
|
||||
resolvers += Resolver.bintrayRepo("fluencelabs", "releases")
|
||||
//scalafmtOnCompile := true,
|
||||
scalafmtOnCompile := true
|
||||
// see good explanation https://gist.github.com/djspiewak/7a81a395c461fd3a09a6941d4cd040f2
|
||||
scalacOptions += "-Ypartial-unification"
|
||||
|
||||
|
11
src/main/scala/fluence/hackethberlin/Decorator.scala
Normal file
11
src/main/scala/fluence/hackethberlin/Decorator.scala
Normal file
@ -0,0 +1,11 @@
|
||||
package fluence.hackethberlin
|
||||
|
||||
sealed abstract class Decorator(name: String) {
|
||||
def toVyper: String = s"@$name"
|
||||
}
|
||||
|
||||
object Decorator {
|
||||
case object `@public` extends Decorator("public")
|
||||
case object `@payable` extends Decorator("payable")
|
||||
case object `@constant` extends Decorator("constant")
|
||||
}
|
17
src/main/scala/fluence/hackethberlin/FuncDef.scala
Normal file
17
src/main/scala/fluence/hackethberlin/FuncDef.scala
Normal file
@ -0,0 +1,17 @@
|
||||
package fluence.hackethberlin
|
||||
|
||||
import fluence.hackethberlin.types.DataVyper
|
||||
import shapeless._
|
||||
|
||||
class FuncDef[Args <: HList: DataVyper](
|
||||
name: String,
|
||||
argsDef: Args,
|
||||
decorators: Set[Decorator] = Set.empty
|
||||
) {
|
||||
|
||||
def toVyper: String =
|
||||
s"${decorators.map(_.toVyper).mkString("\n")}\ndef $name(${DataVyper[Args].mkString(argsDef, ", ")}):\n body;\n"
|
||||
|
||||
def @:(decorator: Decorator): FuncDef[Args] =
|
||||
new FuncDef[Args](name, argsDef, decorators + decorator)
|
||||
}
|
@ -2,6 +2,7 @@ package fluence.hackethberlin
|
||||
|
||||
import shapeless._
|
||||
import types._
|
||||
import Decorator._
|
||||
|
||||
object MakeVyperApp extends App {
|
||||
|
||||
@ -21,6 +22,15 @@ object MakeVyperApp extends App {
|
||||
("struct2" → struct) :: HNil
|
||||
)
|
||||
|
||||
val func = new FuncDef(
|
||||
"myFunc",
|
||||
("address" → address) :: HNil
|
||||
)
|
||||
|
||||
println(data.toVyper)
|
||||
|
||||
println(func.toVyper)
|
||||
|
||||
println((`@public` @: func).toVyper)
|
||||
|
||||
}
|
@ -5,6 +5,9 @@ import shapeless.tag._
|
||||
|
||||
sealed trait DataVyper[T] {
|
||||
def toVyperDefinitions(data: T): List[String]
|
||||
|
||||
def mkString(data: T, sep: String): String =
|
||||
toVyperDefinitions(data).mkString(sep)
|
||||
}
|
||||
|
||||
sealed trait LowPriorityDataVyperImplicits {
|
||||
@ -30,6 +33,8 @@ sealed trait LowPriorityDataVyperImplicits {
|
||||
|
||||
object DataVyper extends LowPriorityDataVyperImplicits {
|
||||
|
||||
def apply[T](implicit dataVyper: DataVyper[T]): DataVyper[T] = dataVyper
|
||||
|
||||
implicit def pairDataPublicVyper[T <: Type]: DataVyper[(String, T @@ Public)] =
|
||||
new DataVyper[(String, T @@ Public)] {
|
||||
override def toVyperDefinitions(pair: (String, T @@ Public)): List[String] = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user