Inner classes: Difference between revisions

m
julia example
(→‎{{header|Wren}}: Added some remarks to preamble about accessing outer class methods.)
m (julia example)
Line 164:
vec[0] = 1
</pre>
 
 
=={{header|Julia}}==
 
Julia allows inner classes, but because such classes are always public,
there is little or no utility to be had nesting classes in Julia. Private
methods in Julia are best simulated via modules which limit their exports.
 
Anything that can be done with inner classes can be accomplished in Julia just as
well with the structs not nested, and the non-nested versions may be easier to
understand. Furthermore, the new() function used for inner constructors in Julia
refers only to the outermost struct, so the inner class must have any non-implict
constructors that may be needed defined outside the struct.
<syntaxhighlight lang = "julia">
""" see the Outer class in C++ example """
struct Outer
m_privateField::Int
 
""" Inner class in example """
struct Inner
m_innerValue::Int
end
end
 
""" adds the values from the outer and inner class objects """
addouter(inner::Inner, outer::Outer) = outer.m_privateField + inner.m_innerValue
 
"""
Test the functions. Iterables in Julia are structs for which the iterate()
function is defined, so no need for inner classes for that
"""
function main()
inner = Inner(42)
outer = Outer(1)
outplusin = addouter(inner, outer)
println("sum: $outplusin")
end
 
main()
</syntaxhighlight>
 
=={{header|Raku}}==
4,102

edits