spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Array of elements of compound type


What is happening exactly behind scene here? The compound types are actually instances of the first type?

Looking at the newArray method as defined for ClassTag:

override def newArray(len: Int):
Array[T] =
   runtimeClass match {
     case java.lang.Byte.TYPE      => new
Array[Byte](len).asInstanceOf[Array[T]]
     case java.lang.Integer.TYPE   => new
Array[Int](len).asInstanceOf[Array[T]]
     /* snip */
     case _                        =>
java.lang.reflect.Array.newInstance(runtimeClass,
len).asInstanceOf[Array[T]]
   }

And then your type:

scala> classTag[Int with String with
Double].runtimeClass
res4: Class[_] = int

It seems pretty clear how it's arriving at the conclusion to use the first type. The runtimeClass of Int with String with Double is Int, and the newArray method uses the runtimeClass to construct the new Array. So the Array is filled with default Int values. Likewise for the other order combinations.

Why is the runtime class Int? Well, Int with String with Double isn't an actual class, but Int is. The compiler's choice of which class to use can't just be arbitrary, so why not the first?

Can a compound type be instanced explicitly?

I'm not sure what you mean by this. If you think about it, the compiler kind of has to favor one type in this composition. What would an explicit Int with String with Double look like? What about Int with String with Double with Product with Process ? I don't know, either.

Each of the array elements has access to the methods of the three types: Int, String and Double

They do, and yet they don't.

scala> val arr = classTag[Int with
Process].newArray(3)
arr: Array[Int with Process] = Array(0, 0, 0)

scala> arr(0).exitValue()
java.lang.ClassCastException: java.lang.Integer
cannot be cast to java.lang.Process

You're really just tricking the compiler into believing you have an Array of these types, when in fact you don't (and can't, because nothing can be a sub-type of both Int and Process).

Their use case is only for when the types composing the compound are related through inheritance and so on?

Pretty much, yes. I can't think if any situation in which you'd ever want to try to build these types directly. Their main use case is to guarantee that a type inherits from other specific types.

For example, a method that requires that a parameter inherit methods from two traits:

trait Hello { def hello =
println("Hello") }

trait Bye { def bye = println("Bye") }

def greet(hb: Hello with Bye): Unit = { hb.hello;
hb.bye }

class A extends Hello

class B extends Hello with Bye

scala> greet(new A)
<console>:15: error: type mismatch;
  found   : A
  required: Hello with Bye
          greet(new A)
                ^

scala> greet(new B)
Hello
Bye

Categories : Scala

Related to : Array of elements of compound type
Filling/Populating a multidimensional array,, with elements of another array, and adding/incrementing values?
I read the documentation on Array more carefully. An array can also be created by explicitly calling ::new with zero, one (the initial size of the Array) or two arguments (the initial size and a default object). ary = Array.new #=> [] Array.new(3) #=> [nil, nil, nil] Array.new(3, true) #=> [true, true, true] Note that the second argument populates the array with references to

Categories : Ruby
How do you copy over an array of no elements to an output file so that it contains an empty array?
fopen( bobby.txt , "wb" ); This is wrong. fopen( "bobby.txt" , "wb" ); The array has nothing in it and your are trying to copy nothing to a file? This sounds contradictory. When there is nothing in the array don't write it to a file. If you want to check whether the file exists or not check the below link. What's the best way to check if a file exists in C? (cross platform)

Categories : C
compare elements positions in first array with another array
Its just a loop. function checkArrays( arrA, arrB ){ for(var i = 0; i < arrA.length; i++) { if(arrA[i] !== arrB [i]) return arrB [i] }; } http://jsfiddle.net/hL257bh6/ If you're allowed to use ECMAScript 6 a.find(function(v, i){ return v !== d[i] })

Categories : Javascript
Compound Interest Quarterly in C
I'm not sure if I had understood your single line problem statement correctly, but IMO, you are not updating the principal for the next iteration. you should use for(x = 1; x <= time; x++) { i = rate / 400; total_amount = principal * pow(1 + (i), x); //total_amount = total_end interest = total_amount - principal; printf("%d %.2f %.2f %.2f ", x, princ

Categories : C
How to add vertical rule on a compound TextView
Solved it! Included a <view> after TextView and styled it to display a border on the right. Here's how: <TextView android:id="@+id/searchRadiusText" android:layout_width="70dp" android:layout_height="match_parent" android:paddingTop="2dp" android:paddingBottom="2dp" android:textColor="@android:color/white" android:textAppearance="?android:attr/textAppearanceM

Categories : Android
Recently Add
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching
java.io.IOException: Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
Pattern Match on Scala `class`
Type mismatch when using higher-kinded types
Scala List match last element
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.