implicit

Pimp my function in scala - applying implicit conversions on functions

I have some problems when I want to use implicit methods to convert a function to something else. I'm implementing a small DSL in Scala 2.8 for testing purposes. It should support various checks (assertions if you like) on instances. The whole DSL is a bit complex, but the following simplified example shows my problem: object PimpMyFun...

Deleting a nested struct with void pointers as members?

I have the following class: class Stack { struct Link { void* data; Link* next; void initialize(void* dat, Link* nxt); }* head; public: void initialize(); void push(void* dat); void* peek(); void* pop(); void cleanup(); }; The pop method is: void* Stack::pop() { if(head == 0) return 0; void* result = hea...

Scala: convert a return type into a custom trait

I've written a custom trait which extends Iterator[A] and I'd like to be able to use the methods I've written on an Iterator[A] which is returned from another method. Is this possible? trait Increment[+A] extends Iterator[A]{ def foo() = "something" } class Bar( source:BufferedSource){ //this ain't working def getContents(...

Strange behavior with implicits

I'm using the Scalacheck library to test my application. In that library there's a Gen object that defines implicit conversions of any object to a generator of objects of that class. E.g., importing Gen._ lets you call methods such as sample on any object, through its implicit conversion to Gen: scala> import org.scalacheck.Gen._ impor...

Implicit conversion to instantiate a sealed class

I have this inheritance sealed abstract class MyValue case class MyString(s:String) extends MyValue case class MyBoolean(b:Boolean) extends MyValue case class MyR(m1:MyValue, m2:MyValue) extends MyValue case class MyU(m1:MyValue, m2:MyValue) extends MyValue /* ... */ and implicit def string2myString(s:String) = MyString(s) implicit ...

ASP.NET: explicit vs implicit localization?

To my mind the advantage of implicit localization over explicit localization is that if you have more than one property to localize for a given control, it's a more economical syntax. In the case where you just need to localize some text I use the asp:Localize control which only has a single property (Text) that renders to the UI. Is t...

Equivalent implicit operators: why are they legal?

Update! See my dissection of a portion of the C# spec below; I think I must be missing something, because to me it looks like the behavior I'm describing in this question actually violates the spec. Update 2! OK, upon further reflection, and based on some comments, I think I now understand what's going on. The words "source type" in t...

Default constructor

struct Base{ Base(Base &){} // suppress default constructor }; struct Derived : Base{ }; int main(){ Derived d; } The code shown gives error because the default constructor (implicit) of 'Base' is suppressed. Indeed the standard says in $12.1 "If there is no user-declared constructor for class X, a default constructor ...

Implicit conversion without assignment?

Preserved question - see Edit at the bottom I'm working on a small functional library, basically to provide some readability by hiding basic cyclomatic complexities. The provider is called Select<T> (with a helper factory called Select), and usage is similar to public Guid? GetPropertyId(...) { return Select .Either(TryToGe...

implicit value not found

class Test { import scala.collection._ class Parent class Child extends Parent implicit val children = mutable.Map[String, Child]() def createEntities[T <: Parent](names: String*) = names.foreach(createEntity[T]) def createEntity[T <: Parent](name: String)(implicit map: mutable.Map[String, T]): Unit = map.get...

VB.NET namespace issue regarding explict (named) vs. implicit (global or root) namespaces

I have a solution that contains many projects all using the same root namespace. No code files explicitly name a namespace. So lets say the root namespace is ExampleRootNamespace. Now a problem comes into play when I want to add an explicitly named namespace to one of the code files I am working on. I want to be able to isolate this cod...

implicit cast through contructor with multiple arguments

If I have these 2 constructors for MyClass: MyClass(int n1); MyClass(int n1, int n2); and an overloaded (non-member) operator+: MyClass operator+(MyClass m1, const MyClass& m2); This enables me to write code like this: MyClass m; 5 + m: which I guess uses implicit cast through the defined constructor, correct? Is there anyway t...

Scala: Do implicit conversions work on Any?

I would like to store some objects from different type hierarchy into List[Any] or similar container, but perform implicit conversions on them later on to do something like type class. Here is an example: abstract class Price[A] { def price(a: A): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChic...

WPF: Implicit Infragistics Ribbon Styling using BasedOn

Hello, I'm using the WPF infragistics ribbon and I'm trying to work off their styles using the BasedOn attribute. This work well when the x:Key is defined however in the case where I want to override the property on a border located inside an implicit style, what's the best way to do that without duplicating the entire style into my cust...

Scala: Generic implicit converters?

I would like to define a generic implicit converter that works for all subtypes of type T. For example: abstract class Price[A] { def price(a: Any): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChicken() extends Food object Def { implicit def carToPrice[A <: Car](car: A): Price[A] = new Price[...

How can I get around this limitation of overload resolution in Scala?

Hi, all, In Scala, the interaction of overloading and implicit argument resolution seem to make it impossible to make the following code usable. trait Bijection[A, B] extends Function1[A, B] with Unapply[A, B] { self => def apply(a: A): B def unapply(b: B): A } sealed trait Unapply[A, B] { def unapply(b: B): A } object Bijectio...

Can C#'s delegate methods take implicitly typed arguments?

I'm curious if it's possible to create a delegate method with implicitly typed arguments. Here's what I'm trying to accomplish. This sort of thing won't compile for me, but it should give you an idea of what I'm trying to accomplish. here's the functions I want to call using delegates: class MyClass { ArrayList function1(int param1...

Why cannot this case of implicit conversions be optimized?

Why cannot Scala optimize the following: a. implicit def whatever[A](a: A) = new { ... } to: b. class some$generated$name(a: A) { ... } implicit def whatever[A](a: A) = new some$generated$name(a) ? Why does it have to use structural typing in this case? I would like Scala compiler to perform this optimization as writing in sty...

Type class pattern in Scala doesn't consider inheritance?

I am designing an API using type classes in some cases however I have encountered a problem with implicit resolution. As shown below, if there is an implicit object for type A but an object of type B extends A is passed to the method, then an implicit object cannot be found. Is there a way to make this work or do callers have to put im...

Nested Dictionaries in Python, with implicit creation of non-existing intermediate containers?

I want to create a polymorphic structure that can be created on the fly with minimum typing effort and be very readable. For example: a.b = 1 a.c.d = 2 a.c.e = 3 a.f.g.a.b.c.d = cucu a.aaa = bau I do not want to create an intermediate container such as: a.c = subobject() a.c.d = 2 a.c.e = 3 My question is similar to this one: http...