structural-typing

Understanding Structural Equivalence

亡梦爱人 提交于 2019-12-20 03:08:33
问题 I have two types of structural equivalence ideas I am struggling to understand. VAR_1 = int VAR_2 = pointer to VAR_1 So here, I feel like they are structurally equivalent because the types would technically be both pointing to an integer type, correct? but then if you had something like VAR_3 = pointer to int Does the pointer to an int versus an integer declaration make them inequivalent? I am thinking that VAR_1 and VAR_2 are structually equivalent because they contain the same data in the

Duck typing, must it be dynamic?

让人想犯罪 __ 提交于 2019-12-17 22:34:12
问题 Wikipedia used to say* about duck-typing: In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. (* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".) It says about structural typing: A structural

Namespace scoped aliases for generic types in C#

时光怂恿深爱的人放手 提交于 2019-12-09 10:49:05
问题 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

Why do compile-time generative techniques for structural typing prevent separate compilation?

雨燕双飞 提交于 2019-12-07 13:21:52
问题 I was reading (ok, skimming) Dubochet and Odersky's Compiling Structural Types on the JVM and was confused by the following claim: Generative techniques create Java interfaces to stand in for structural types on the JVM. The complexity of such techniques lies in that all classes that are to be used as structural types anywhere in the program must implement the right interfaces. When this is done at compile time, it prevents separate compilation. (emphasis added) Consider the autoclose example

Classmethods in Generic Protocols with self-types, mypy type checking failure

≯℡__Kan透↙ 提交于 2019-12-07 00:22:03
问题 A little background, I essentially need to define an int wrapper type, say MyInt (among some other classes), and another generic Interval type which can accept MyInt objects as well as other types of objects. Since the types acceptable by the Interval do not fall into a neat hierarchy, I thought this would be a perfect use-case for the experimental Protocol , which in my case would require a couple of methods and a couple of @classmethod s. All the methods return a "self-type", i.e., MyInt.my

accept multiple types for a parameter in scala

泪湿孤枕 提交于 2019-12-06 19:07:51
问题 I have two objects, ObjectA and ObjectB, both with a method update(). I want to write a function that accepts either ObjectA or ObjectB (but no other types). Conceptually, this is what I am trying to do: def doSomething[T <: ObjectA | T <: ObjectB](obj: T) = { obj.update } I realize there are other ways to solve this problem (eg, structural typing of the update() method, common base class, etc) but my question is it is possible to do it this way in Scala and if so what is the syntax? And what

Why do compile-time generative techniques for structural typing prevent separate compilation?

放肆的年华 提交于 2019-12-05 21:47:40
I was reading (ok, skimming) Dubochet and Odersky's Compiling Structural Types on the JVM and was confused by the following claim: Generative techniques create Java interfaces to stand in for structural types on the JVM. The complexity of such techniques lies in that all classes that are to be used as structural types anywhere in the program must implement the right interfaces. When this is done at compile time, it prevents separate compilation. (emphasis added) Consider the autoclose example from the paper: type Closeable = Any { def close(): Unit } def autoclose(t: Closeable)(run: Closeable

Why doesn't type inference work here?

試著忘記壹切 提交于 2019-12-05 07:44:40
This problem arose in a module I'm writing, but I have made a minimal case that exhibits the same behaviour. class Minimal[T](x : T) { def doSomething = x } object Sugar { type S[T] = { def doSomething : T } def apply[T, X <: S[T]] (x: X) = x.doSomething } object Error { val a = new Minimal(4) Sugar(a) // error: inferred [Nothing, Minimal[Int]] does not fit the bounds of apply Sugar[Int, Minimal[Int]](a) // works as expected } The problem is that the compiler manages to figure out the inner parameter for Minimal ( Int ), but then sets the other occurrence of T to Nothing , which obviously does

Result type in structural refinement may not refer to a user-defined value class

半城伤御伤魂 提交于 2019-12-05 05:59:50
When I define Wrapper as value class(extending AnyVal): class Wrapper(val string: String) extends AnyVal def wrapperHolder(w: Wrapper): {def wrapper: Wrapper} = new { def wrapper: Wrapper = w } I have following compile error for wrapperHolder: Error:(5, 22) Result type in structural refinement may not refer to a user-defined value class def wrapper: Wrapper = w Why it doesn't work for value class? 来源: https://stackoverflow.com/questions/52678323/result-type-in-structural-refinement-may-not-refer-to-a-user-defined-value-class

Classmethods in Generic Protocols with self-types, mypy type checking failure

江枫思渺然 提交于 2019-12-05 03:49:24
A little background, I essentially need to define an int wrapper type, say MyInt (among some other classes), and another generic Interval type which can accept MyInt objects as well as other types of objects. Since the types acceptable by the Interval do not fall into a neat hierarchy, I thought this would be a perfect use-case for the experimental Protocol , which in my case would require a couple of methods and a couple of @classmethod s. All the methods return a "self-type", i.e., MyInt.my_method returns a MyInt . Here is a MCVE: from dataclasses import dataclass from typing import Union,