Giter Site home page Giter Site logo

kotlinbard's People

Contributors

glassbricks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

kotlinbard's Issues

Code block inside control flow not generating properly

Hey, first wanna say this project is awesome, much more readable than vanilla KotlinPoet!!

I think, however, there is currently a problem with nested control flows. A simple example

fun main() {
  val potatoFun = buildFunction("test") {
    addCode {
      controlFlow("if (1 == 1)") {
        controlFlow("if (2 == 2)") {
          addStatement("val nice = true")
          addStatement("val cool = true")
          addCode {
            addStatement("val amazing = false")
          }
        }
      }
    }
  }

  println(potatoFun.toString())
}

should, in theory, correspond to

fun test() {
  if (1 == 1) {
    if (2 == 2) {
      val nice = true
      val cool = true
      val amazing = false
    }
  }
}

However, currently, KotlinBard will generate the following

fun test() {
  val amazing = false
  if (1 == 1) {
    if (2 == 2) {
      val nice = true
      val cool = true
    }
  }
}

My project versions, if that helps, are
kotlin -> 1.4
kotlinPoet -> 1.6.0
kotlinBard -> 0.2.0

Compact element adding syntax

Here are some ideas possible more compact syntax for adding elements inside builders.

Current syntax:

buildElement(){
   addSubelement(){ }
}

Ideas to enhance this:

Use unaryPlus operator

val mySpec = element(){
  +subelement(){ }
}

Pros:

  • Extra compact
  • Separates "adding" from "building"
    • can easily do +mySpec or +mySpec.withModifications()
    • room for "prefix" modifiers, integrates well with new building syntax

Cons:

  • Requires having own type to control scope (cannot be purely extension functions)
  • Requires unique type to each "add" function; may have clashes
  • May not always be obvious what is added (readability)
  • easy to forget +

Use infix extension functions

val mySpec = element(){
   this add subelement(){ }
// or
   this addXXX subelement(){ }
}

Pros:

  • Still separates "adding" from "building"
  • Named function (clarity and dis-ambiguity)
  • Can be just extension functions (or operator + )

Cons:

  • Clunky syntax (restating this repeatedly)
  • Possibly more words than necessary

Just remove 'add' prefix

val mySpec = buildElement(){
   subelement(){ }
}

Pros:

  • Named function
  • Can be just extension functions

Cons:

  • Adding and building put together
  • Need separate functions for only adding and only building
  • Doesn't express "adding"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.