Constrained genericity: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
Line 678:
<lang java5>test.<EatableClass>bar();</lang>
The <code>foo</code> method from before can figure out <code>T</code> from its parameter, but this <code>bar</code> method needs to be told what T is.
 
=={{header|Julia}}==
Julia allows user defined types with inheritance. Misuse of a type generally produces a compile time error message.
<lang julia>
abstract type Edible <: AbstractString end
 
mutable struct FoodBox
food::Edible
function FoodBox(provisions)
this = new()
this.food = provisions
this
end
end
 
mutable struct Carrot <: Edible
variety::String
function Carrot(v)
this = new()
this.variety = v
this
end
end
 
mutable struct Car
make::String
model::String
year::Integer
function Car(mak, mod, yr)
this = new()
this.make = mak
this.model = mod
this.year = yr
this
end
end
 
function eat(food::Edible)
println("Yum")
end
carrot = Carrot("Baby")
car = Car("Honda", "Civic", 1975)
foodbox1 = FoodBox(carrot)
foodbox2 = FoodBox(car)
</lang>
{{output}}
ERROR: LoadError: MethodError: Cannot `convert` an object of type Car to an object of type Edible
 
=={{header|Kotlin}}==
4,103

edits