Skip to content

Instantly share code, notes, and snippets.

View sptz45's full-sized avatar
🏠
Working from home

Spiros Tzavellas sptz45

🏠
Working from home
View GitHub Profile
@milessabin
milessabin / gist:1705644
Created January 30, 2012 17:47
Access to companion object of Foo via implicit resolution
trait Companion[T] {
type C
def apply() : C
}
object Companion {
implicit def companion[T](implicit comp : Companion[T]) = comp()
}
object TestCompanion {
@ymasory
ymasory / GetVersion.scala
Created April 17, 2011 00:25
detect Scala version at runtime
/** Use with a default value, for example:
* runningScalaVersion getOrElse "2.8.0"
*/
lazy val runningScalaVersion = {
val matcher = """version (\d+\.\d+\.\d+).*""".r
util.Properties.versionString match {
case matcher(versionString) => Some(versionString)
case _ => None
}
}
@jorgeortiz85
jorgeortiz85 / DynamicImpl.scala
Created January 17, 2011 20:16
Method calls & XML traversal with Scala's new Dynamic type
class DynamicImpl(x: AnyRef) extends Dynamic {
def _select_(name: String): DynamicImpl = {
new DynamicImpl(x.getClass.getMethod(name).invoke(x))
}
def _invoke_(name: String)(args: Any*) = {
new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
}
override def typed[T] = x.asInstanceOf[T]
override def toString = "Dynamic(" + x.toString + ")"
}
@razie
razie / scalaskilset.markdown
Created December 9, 2010 17:42
Scala skill levels
@etorreborre
etorreborre / gist:730097
Created December 6, 2010 10:17
An implicit pearl in Scala
/**
* An answer from Jason Zaugg on the scala-internals mailing list
*
* Implicit parameters can have defaults.
*/
scala> implicit def OptionalImplicit[A <: AnyRef](implicit a: A = null) = Option(a)
OptionalImplicit: [A <: AnyRef](implicit a: A)Option[A]
scala> implicitly[Option[Ordering[Int]]]
@razie
razie / scala-profiles.markdown
Created September 24, 2010 15:35
Scala abuses

Scala is a wonderful language, with a specification of a size comparable with that of Java. Overall, the specification is simpler than C++. Why then do some feel intimidated by its expressiveness?

Scala can generate complex constructs. There are some features of the language that, if not used properly, can negatively influence one's perception of its simplicity and generate a waste of many an hour of a frustrated developer.

Newcomers from simply typed, object-oriented, structured languages do not have a full grasp of all the features of scala and their effects or benefits, and they may be very surprised if others on their team use them.

One thing to have when starting or using scala is a readily available bible. I recommend "Programming in Scala", the .pdf version, an easily searcheable language reference.

The scala style guide is very good. Here we will describe the advanced features of the language, which are aimed at library-developers and should not b