This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ChainedConversions { | |
implicit def chain[A, B, C](implicit ab: A => B, bc: B => C): A => C = ab andThen bc | |
implicit val intToString: Int => String = _.toString | |
class X | |
implicit val stringToX: String => X = _ => new X | |
val a: X = 5 // idea marks it as a proper conversion | |
//doesn't compile though | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
def abc0[M[_]] = ??? | |
def abc1[M[+_]] = ??? | |
def abc2[M[-_]] = ??? | |
//this one should accept an arbitrary invariant M | |
def abc3[M[_] <: Set[_]] = ??? | |
type Predicate[-a] = a => Boolean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Example { | |
case class Sample(age: Int) | |
def wrapsBlock(block1: Any) = ??? | |
def ifHasImplicit(block2: Any)(implicit request: String) = ??? | |
def sample(sampleObj: Sample) = wrapsBlock { | |
ifHasImplicit { | |
import sampleObj.age |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sample { | |
public static void main(String[] args) { | |
new Dog().speak(10); //I am 10 | |
new Dog().speak("kek"); //I am kek wow | |
} | |
} | |
class Animal { | |
void speak(Object something){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on https://github.com/kamon-io/Kamon/issues/311 | |
class AsyncExecutorContextAware(underlying: AsyncExecutor) extends AsyncExecutor { | |
override def executionContext: ExecutionContext = new ExecutionContextAware(underlying.executionContext) | |
override def close(): Unit = underlying.close() | |
} | |
class ExecutionContextAware(underlying: ExecutionContext) extends ExecutionContext { | |
override def execute(runnable: Runnable): Unit = underlying.execute(new RunnableContextAware(runnable)) | |
override def reportFailure(cause: Throwable): Unit = underlying.reportFailure(cause) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//see original SO answer: https://stackoverflow.com/a/49184270/1927425 | |
import shapeless.ops.hlist._ | |
import shapeless.{::, _} | |
trait EmptyOf[T <: HList, AsList <: HList] { | |
def empty(): AsList | |
} | |
object EmptyOf { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait GameCreationError extends Product with Serializable | |
object GameCreationError { | |
case class NameTooShort(min: Int) extends GameCreationError | |
case class NameTooLong(max: Int) extends GameCreationError | |
case class PlatformNotFound(platformSlug: String) extends GameCreationError | |
case class PlayersFromTooLow(min: Int) extends GameCreationError | |
case object PlayersToSmallerThanFrom extends GameCreationError | |
case object NameNotUnique extends GameCreationError | |
case object NoPlatformsSupplied extends GameCreationError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.circe.generic.JsonCodec | |
@JsonCodec(decodeOnly = true) | |
case class GameToCreate(name: String, platforms: List[String], players: Players) | |
@JsonCodec(decodeOnly = true) | |
case class Players(from: Int, to: Option[Int]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object map { | |
/** | |
* Allows a Map to become total forever. | |
* It's isomorphic to `Map.withDefaultValue`, but has a more specific return type, in particular: | |
* <ul> | |
* <li>it leaves out the information that the returned value is a Map</li> | |
* <li>it manifests that the returned value is a total function.</li> | |
* </ul> | |
* | |
* This is useful for a single reason - Maps by default aren't total functions, so `map.apply` can potentially |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//works with Kleisli/ReaderT | |
implicit class NestedToUnary[F[_[_], _, _], G[_], I, O](private val value: F[G, I, O]) extends AnyVal { | |
type H[A] = F[G, I, A] | |
def unaryKind: H[O] = value | |
} | |
//works with fs2.Stream, Free/AuthedService from http4s | |
implicit class NestedToUnary2[F[_[_], _], G[_], I](private val value: F[G, I]) extends AnyVal { | |
type H[A] = F[G, I] | |
def unaryKind: H[O] = value |