structural-typing

Using Scala structural types with abstract types

余生长醉 提交于 2019-12-04 18:45:42
问题 I'm trying to define a structural type defining any collection that has an "add" method (for instance, a java collection). Using this, I want to define a few higher order functions that operate on a certain collection object GenericTypes { type GenericCollection[T] = { def add(value: T): java.lang.Boolean} } import GenericTypes._ trait HigherOrderFunctions[T, CollectionType[X] <: GenericCollection[X]] { def map[V](fn: (T) => V): CollectionType[V] .... } class RichJList[T](list: List[T])

Weird nested structural type in generics

一笑奈何 提交于 2019-12-04 12:12:24
Can someone explain weird construction of structural type nested in generics: implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] = new Functor[({type λ[α]=(R) => α})#λ] .... This example comes from Scalaz library: Functor.scala Why this construction is needed there? Wouldn't be simpler to write: implicit def Function1Functor[R,A]: Functor[R =>A] or implicit def Function1Functor[R,A]: Functor[Function1[R,A]] The signature of the Functor type constructor shows that it is parameterised with another, unary, type constructor F : trait Functor[F[_]] extends InvariantFunctor[F]

Why implicit non-pointer methods not satisfy interface?

雨燕双飞 提交于 2019-12-03 17:19:26
Assuming we have an understanding that, For explicit method definition for type X , GO compiler implicitly defines the same method for type *X and vice versa , if I declare, func (c Cat) foo(){ //do stuff_ } and declare, func (c *Cat) foo(){ // do stuff_ } then GO compiler gives error, Compile error: method re-declared which indicates that, pointer method is implicitly defined and vice versa In the below code, package main type X interface{ foo(); bar(); } type Cat struct{ } func (c Cat) foo(){ // do stuff_ } func (c *Cat) bar(){ // do stuff_ } func main() { var c Cat var p *Cat var x X x = p

Why scala uses reflection to call method on structural type?

北慕城南 提交于 2019-12-03 16:56:27
问题 If function accepts structural type, it can be defined as: def doTheThings(duck: { def walk; def quack }) { duck.quack } or type DuckType = { def walk; def quack } def doTheThings(duck: DuckType) { duck.quack } Then, you can use that function in following way: class Dog { def walk { println("Dog walk") } def quack { println("Dog quacks") } } def main(args: Array[String]) { doTheThings(new Dog); } If you decompile (to Java) the classes generated by scalac for my example, you can see that

Namespace scoped aliases for generic types in C#

空扰寡人 提交于 2019-12-03 13:47:11
Let's have a following example: public class X { } public class Y { } public class Z { } public delegate IDictionary<Y, IList<Z>> Bar(IList<X> x, int i); public interface IFoo { // ... Bar Bar { get; } } public class Foo : IFoo { // ... public Bar Bar { get { return null; //... } } } void Main() { IFoo foo; //= ... IEnumerable<IList<X>> source; //= ... var results = source.Select(foo.Bar); // <- compile error here } The compiler says: The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try

Using Scala structural types with abstract types

对着背影说爱祢 提交于 2019-12-03 12:17:42
I'm trying to define a structural type defining any collection that has an "add" method (for instance, a java collection). Using this, I want to define a few higher order functions that operate on a certain collection object GenericTypes { type GenericCollection[T] = { def add(value: T): java.lang.Boolean} } import GenericTypes._ trait HigherOrderFunctions[T, CollectionType[X] <: GenericCollection[X]] { def map[V](fn: (T) => V): CollectionType[V] .... } class RichJList[T](list: List[T]) extends HigherOrderFunctions[T, java.util.List] This does not compile with the following error error:

Clojure Protocols vs Scala Structural Types

谁都会走 提交于 2019-12-03 05:14:00
问题 After watching the interview with Rich Hickey on Protocols in Clojure 1.2, and knowing very little about Clojure, I have some questions on Clojure Protocols: Are they intended to do the same thing as Structural Types in Scala? What benefits do Protocols have over Structural Types (performance, flexibility, code clarity, etc.)? Are they implemented through reflections? Questions on interoperability with Scala: Can Protocols be used instead of Structural Types in Scala? Can they be extended (if

Why scala uses reflection to call method on structural type?

烈酒焚心 提交于 2019-12-03 05:12:34
If function accepts structural type, it can be defined as: def doTheThings(duck: { def walk; def quack }) { duck.quack } or type DuckType = { def walk; def quack } def doTheThings(duck: DuckType) { duck.quack } Then, you can use that function in following way: class Dog { def walk { println("Dog walk") } def quack { println("Dog quacks") } } def main(args: Array[String]) { doTheThings(new Dog); } If you decompile (to Java) the classes generated by scalac for my example, you can see that argument of doTheThings is of type Object and the implementation uses reflection to call methods on the

Scala: difference between a typeclass and an ADT?

感情迁移 提交于 2019-12-03 01:13:51
问题 What are the differences between typeclasses and Abstract Data Types? I realize this is a basic thing for Haskell programmers, but I come from a Scala background, and would be interested in examples in Scala. The best I can find right now is that typeclasses are "open" and ADT's are "closed". It would also be helpful to compare and contrast typeclasses with structural types. 回答1: ADTs (which in this context are not Abstract Data Types, which is even another concept, but Algebraic Data Types)

Clojure Protocols vs Scala Structural Types

心已入冬 提交于 2019-12-02 18:33:27
After watching the interview with Rich Hickey on Protocols in Clojure 1.2, and knowing very little about Clojure, I have some questions on Clojure Protocols: Are they intended to do the same thing as Structural Types in Scala? What benefits do Protocols have over Structural Types (performance, flexibility, code clarity, etc.)? Are they implemented through reflections? Questions on interoperability with Scala: Can Protocols be used instead of Structural Types in Scala? Can they be extended (if 'extension' term can be applied to Protocols) in Scala? Totally unrelated. Scala is a statically typed