I assume my title describes what I am up to but, to be honest, I am not sure. First of all: this is not a serious project, just playing around to get to know scala.
object Helpers
{
class AtOperation(list: List[Int])
{
def at(index: Int): List[Int] = list match {
case first :: rest =>
val (before, after) = rest.splitAt(index-1)
before ++ List(first) ++ after
}
}
class MyInt(value: Int)
{
def into(list: List[Int]) = new AtOperation(value :: list)
}
class Hey
{
def insert(value: Int): MyInt = new MyInt(value)
}
}
import Helpers._
object Test2 extends App
{
println(new Hey insert 42 into List(1, 1, 1, 1) at 3) // prints List(1, 1, 42, 1, 1)
}
This works fine. But what I actually want to write is:
insert 42 into List(1, 1, 1, 1) at position 3
So, no new Hey in the beginning and there should be a position after at. Is that possible?
My current attempt is:
object Helpers
{
class PositionOperation(list: List[Int])
{
def position(index: Int): List[Int] = list match {
case first :: rest =>
val (before, after) = rest.splitAt(index-1)
before ++ List(first) ++ after
}
}
class AtOperation(list: List[Int])
{
def at = new PositionOperation(list)
}
class MyInt(value: Int)
{
def into(list: List[Int]) = new AtOperation(value :: list)
}
class Hey
{
def insert(value: Int): MyInt = new MyInt(value)
}
}
import Helpers._
object Test2 extends App
{
def insert(value: Int) = new MyInt(value)
println(insert(42).into(List(1, 1, 1, 1)).at.position(3)) // this works fine
println(new Hey insert 42 into List(1, 1, 1, 1) at position 3) // position is interpreted as a parameter of at :-(
}
This is apparently not working as expected